Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 June SQL Server Interview: Advance SQL Query – Find Permutations and Combinations of a String Column

SQL Server Interview: Advance SQL Query – Find Permutations and Combinations of a String Column

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing an advanced SQL Query to find a Permutations and Combinations of given string data of the table. The question is look like very complex query, but the solution is very simple.

You can apply self-joins and can prepare all available combinations. The T-SQL or Database Developer can practice this query before an interview.

Create a table with sample data:

1
2
3
4
5
6
7
8
9
CREATE TABLE tbl_StringAllCombinations (ID INT,TextData CHAR(1))
GO
 
INSERT INTO tbl_StringAllCombinations VALUES
(1,'A')
,(2,'B')
,(3,'C')
,(4,'D')
GO

The Solution: (This is my work around solution, Interviewer may ask for any other alternative solution so please also try to prepare it and share your comments…)

1
2
3
4
5
6
SELECT a.TextData, b.TextData, c.TextData, d.TextData
FROM tbl_StringAllCombinations a
join tbl_StringAllCombinations b on b.TextData not in (a.TextData)
join tbl_StringAllCombinations c on c.TextData not in (a.TextData, b.TextData)
join tbl_StringAllCombinations d on d.TextData not in (a.TextData, b.TextData, c.TextData)
order by 1, 2, 3, 4

The Result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
TextData TextData TextData TextData
-------- -------- -------- --------
A B C D
A B D C
A C B D
A C D B
A D B C
A D C B
B A C D
B A D C
B C A D
B C D A
B D A C
B D C A
C A B D
C A D B
C B A D
C B D A
C D A B
C D B A
D A B C
D A C B
D B A C
D B C A
D C A B
D C B A
(24 row(s) affected)

Jun 2, 2017Anvesh Patel
SQL Server Interview: Advance SQL Query - Find a count of repeated character in a StringSQL Server Interview: Advance SQL Query - Don't use pivot and Do Row aggregation into Column
Comments: 1
  1. Anuj
    April 3, 2019 at 8:27 am

    select a.TextData, b.TextData, c.TextData, d.TextData from
    tbl_StringAllCombinations a join tbl_StringAllCombinations b on a.TextDatab.TextData
    join tbl_StringAllCombinations c on a.TextDatac.TextData and b.TextDatac.TextData
    join tbl_StringAllCombinations d on a.TextDatad.TextData and b.TextDatad.TextData and c.TextDatad.TextData

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

June 2, 2017 1 Comment SQL PuzzleAnvesh Patel, database, database developer interview, database research and development, dbrnd, permutations combinations, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Interview, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, T-SQL Complex query, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....