0% found this document useful (0 votes)
30 views4 pages

SQL Quiz Part 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

SQL Quiz Part 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL QUIZ-

Part 2
DAY 1
You have one table Queue, People are sitting in wrong

Input : QUEUE order, Those who are sitting on Odd Seatno should
replace with next even no seat positions and if last one is
odd then he should be at same position

Seatno NAME
1 Megan fox
2
3
John Cena
Ronaldo
OUTPUT :
4 Alyse perry Seatno NAME
5 Sachin Tendulkar 1 John Cena
6 Salman Khan 2 Megan Fox
3 Alyse Perry
7 Vishal Kaushal
4 Ronaldo
5 Salman Khan
6 Sachin Tendulkar
7 Vishal Kaushal
SQL by VISHAL KAUSHAL
SOLUTION 1 : Using LEAD & LAG

Select Seatno, Case when Seatno%2 =0 THEN


LAG(Name) over(order by Seatno asc) ELSE
IFNULL(LEAD(Name)over (order by Seatno asc),Name)
END as Name from Queue

SQL by VISHAL KAUSHAL


SOLUTION 2 : Using CTE

WITH CTE AS (
SELECT COUNT(*) AS Total FROM QUEUE )
SELECT CASE WHEN Seatno%2 =0 AND Seatno < Total THEN Seatno-1
WHEN Seatno%2 =1 AND Seatno <Total THEN Seatno+1
ELSE Seatno END Seatno, Name From Queue, CTE
ORDER by Seatno

SQL by VISHAL KAUSHAL

You might also like