Stop 15 · Interview PreparationTopics · 2/10

Nth Highest Value Problems

Part of the SQL Roadmap

In short

'Find the second highest price' is one of the most asked SQL interview questions. The query is short. What interviewers really check is whether you think about ties.

Questions like 'find the second highest value' can be solved with DENSE_RANK, a subquery with MAX, or ORDER BY with LIMIT and OFFSET, and interviewers like to discuss ties.

The jaadu arranges golden mangoes on a small wooden staircase display, the biggest on the top step and two identical mangoes sitting side by side on the second step.

Think of it like this

Mangoes on the display steps

A fruit seller in Ratnagiri lines up mangoes on wooden steps, heaviest on the top step. Two mangoes weigh exactly the same, so they share the second step. Is the 'third heaviest' the next mango along, or the one on the third step? That choice is exactly ROW_NUMBER versus DENSE_RANK.

Watch it happen

Prices sorted high to low

1299
0
999
1
999
2
749
3
499
4

1/5Five product prices, already sorted from highest to lowest. Two products cost ₹999.

The big ideas

What you need to know

DENSE_RANK for 'Nth highest value'

Ties share a rank and no numbers are skipped, so rank N is the Nth distinct value.

LIMIT and OFFSET

SELECT DISTINCT price ORDER BY price DESC LIMIT 1 OFFSET N-1. Short, but awkward per group.

Subquery with MAX

MAX(price) WHERE price < (SELECT MAX(price)) gives the second highest. It doesn't scale to N well.

Ask about ties and empties

What if two items tie? What if there's no second value? Saying this out loud earns marks.

See it in code

Walk through the code

sql
1-- 1. DENSE_RANK: handles ties, works for any N and per group2WITH ranked AS (3  SELECT product_name,4         category,5         price,6         DENSE_RANK() OVER (ORDER BY price DESC) AS rnk7  FROM products8)9SELECT product_name, price10FROM ranked11WHERE rnk = 2;12 13-- 2. LIMIT / OFFSET on distinct values14SELECT DISTINCT price15FROM products16ORDER BY price DESC17LIMIT 1 OFFSET 1;18 19-- 3. Classic subquery, second highest only20SELECT MAX(price) AS second_highest21FROM products22WHERE price < (SELECT MAX(price) FROM products);

1/4Rank every product by price. For 'Nth highest per category', add PARTITION BY category inside OVER.

Runs in PostgreSQL and MySQL 8. SQL Server uses OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY instead of LIMIT. Option 3 returns NULL when there's no second value, which interviewers often ask about.

Save yourself the pain

Common mistakes

Oops

Using ORDER BY price DESC LIMIT 1 OFFSET 1 without DISTINCT.

Do this instead

If two products share the top price, you get the top price again. Add DISTINCT, or use DENSE_RANK.

Oops

Using ROW_NUMBER for 'Nth highest value'.

Do this instead

ROW_NUMBER breaks ties arbitrarily. Use DENSE_RANK when the question is about values, not rows.

Your mission

Three ways, one answer

~20 min

Quick check

Prices are 900, 900, 750, 600. Which product price does DENSE_RANK() = 2 return?

Go deeper

  1. 1Solve the same problem three different ways.
  2. 2Explain what each method returns when there are ties or fewer than N rows.
  3. 3Extend your answer to 'Nth highest per department'.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge