Advanced SQL: Using multiple methods to gauge the optimum solution

365 Days of Daily Coding: Day 146

Starting fairly late today for today’s blog as lot of things have been going on in my head these days. I don’t know if things will turn out well like I have been thinking but I know that I cannot and should not stay where I am. This is going to be detrimental to myself and my career. Had missed a very good opportunity and I feel quite stupid for not having gone with it. Oh well! Hoping that I land something good very soon.

Puzzle #31: Second Highest

https://advancedsqlpuzzles.com/advanced-sql-puzzles/

I tried the solution in the live oracle environment here.

--Method 1
SELECT * FROM (
SELECT IntegerValue
FROM INTEGER
ORDER BY IntegerValue)
WHERE rownum = 2

--Method 2
SELECT * FROM (
SELECT IntegerValue, DENSE_RANK() OVER (ORDER BY IntegerValue DESC) AS rnk
FROM INTEGER
ORDER BY IntegerValue)
WHERE rnk = 2

Leave a comment