Advanced SQL Challenge: Using LEVEL and CONNECT BY again

365 Days of Daily Coding: Day 107

I would have liked to say that I had a very happy time today but I won’t cause I am feeling extremely frustrated and angry right now. I used to think these emotions were invalid and unnecessary and cause more harm than good. Naturally, being an avid reader and one that reads a lot of self-books, you get accustomed to “Be positive” or “Always be happy no matter what” or “Always smile” messages. These are absolutely crap. One must acknowledge their feelings and mediate the source. I would have loved to reveal the source of my unhappiness: one being my sister which I am ok to declare and the other needs to be kept secret. It’s quite not appropriate to reveal in a public site like this. Perhaps, I will reveal it when the appropriate time arrives.

I solved the second part of the advanced sql challenge. The first part is definitely tricky and will really consume my brain power.

You could try the below solution in live oracle site.

WITH temp_1 AS (
Select 7 AS Seat FROM Dual Union All
Select 13 AS Seat FROM Dual Union All
Select 14 AS Seat FROM Dual Union All
Select 15 AS Seat FROM Dual Union All
Select 27 AS Seat FROM Dual Union All
Select 28 AS Seat FROM Dual Union All
Select 29 AS Seat FROM Dual Union All
Select 30 AS Seat FROM Dual Union All
Select 31 AS Seat FROM Dual Union All
Select 32 AS Seat FROM Dual Union All
Select 33 AS Seat FROM Dual Union All
Select 34 AS Seat FROM Dual Union All
Select 35 AS Seat FROM Dual Union All
Select 52 AS Seat FROM Dual Union All
Select 53 AS Seat FROM Dual Union All
Select 54 AS Seat FROM Dual 
), temp_2 AS (
SELECT level lvl FROM DUAL
CONNECT BY level <= (SELECT MAX(Seat) FROM temp_1))

SELECT COUNT(*) AS count FROM temp_2
WHERE lvl NOT IN (SELECT Seat FROM temp_1)

Leave a comment