This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing an advanced SQL Query question and solution which may ask in the interview of Database Developer.
The question is: Find an available Seats(Status – ‘y’) which are adjacent to each other.
You can achieve this using simple self-join conditions. Check the below solution.
Create a table with sample data:
1 2 3 4 5 6 7 8 9 |
CREATE TABLE tbl_MoviePlex (Seatid INT, Status CHAR(1)) INSERT INTO tbl_MoviePlex VALUES (1,'y') ,(2,'n') ,(3,'y') ,(4,'y') ,(5,'n') ,(6,'y') |
The solution:
1 2 3 4 5 6 7 |
SELECT a.Seatid ,b.Seatid FROM tbl_MoviePlex a INNER JOIN tbl_MoviePlex b on a.Seatid = b.Seatid + 1 and a.Status = b.Status |
The Result:
1 2 3 4 5 |
Seatid Seatid ----------- ----------- 4 3 (1 row(s) affected) |