Advanced SQL: Sorting by a pre-specified sort order in Oracle SQL

365 Days of Daily Coding: Day 160

Today, I am starting fairly early. I was quite motivated to finish this advanced sql puzzle since it is the last one. Upon checking the git repo of advanced sql, I noticed that there were 15 puzzles more that have been added. For now, I will wrap this up and focus on Danny Ma’s serious sql course which I also had purchased few months back.

WITH temp AS 
(
SELECT 'Atlanta' AS City FROM DUAL UNION ALL
SELECT 'Baltimore' AS City FROM DUAL UNION ALL
SELECT 'Chicago' AS City FROM DUAL UNION ALL
SELECT 'Denver' AS City FROM DUAL
)

Select City from temp
ORDER BY 
CASE 
WHEN City = 'Atlanta' THEN 3 
WHEN City = 'Baltimore' THEN 1 
WHEN City = 'Chicago' THEN 4 
WHEN City = 'Denver' THEN 2 
END

The solution was taken from the git repo here.

Leave a comment