Nth Highest Value (Overall & Per Group)
The question
"Find the 2nd highest expense amount." (Classic version: the 2nd highest salary.)
Traps the interviewer is checking
- Ties/duplicates: if two expenses share the top amount, is the "2nd highest" that same amount or the next distinct value? Ask! Usually they want the next distinct value →
DISTINCT/dense_rank - Not enough rows: if there is no 2nd value, return
NULLrather than nothing - Per group: "2nd highest in each department/category" → window functions
Method 1: DISTINCT + ORDER BY + OFFSET
SELECT DISTINCT amount FROM expenses ORDER BY amount DESC LIMIT 1 OFFSET 1;
Wrap in a scalar subquery to get NULL when missing: SELECT (SELECT … ) AS second_highest;
Method 2: MAX below the MAX
SELECT max(amount) FROM expenses WHERE amount < (SELECT max(amount) FROM expenses);
Works only for the 2nd; returns NULL automatically.
Method 3: dense_rank() ⭐ (generalizes to N and to groups)
SELECT DISTINCT amount FROM (
SELECT amount, dense_rank() OVER (ORDER BY amount DESC) AS r FROM expenses
) t WHERE r = 2;
Method 4: correlated count
"Exactly N-1 distinct values are greater than this one" – works anywhere, but is slow on big tables.
Per group
dense_rank() OVER (PARTITION BY category ORDER BY amount DESC) = 2
How to answer in an interview
- Ask about ties and missing values
- Write the dense_rank solution
- Mention the alternatives and their trade-offs (performance, NULL handling)
- Test with edge cases: duplicates, one row, empty table
Example
-- Method 1: DISTINCT + OFFSET (wrapped so a missing value returns NULL)
SELECT (SELECT DISTINCT amount FROM expenses ORDER BY amount DESC LIMIT 1 OFFSET 1) AS second_highest;
-- 7800.00
-- Method 2: max below the max
SELECT max(amount) AS second_highest
FROM expenses
WHERE amount < (SELECT max(amount) FROM expenses); -- 7800.00
-- Method 3: dense_rank — works for any N
SELECT DISTINCT amount AS third_highest FROM (
SELECT amount, dense_rank() OVER (ORDER BY amount DESC) AS r FROM expenses
) t WHERE r = 3; -- 6500.00
-- Ties matter: rank vs dense_rank on duplicate amounts
SELECT amount,
rank() OVER (ORDER BY amount DESC) AS rnk,
dense_rank() OVER (ORDER BY amount DESC) AS dense
FROM (VALUES (900), (900), (800), (700)) t(amount);
-- 900 1 1 | 900 1 1 | 800 3 2 | 700 4 3 → "2nd highest distinct" = 800 (dense_rank = 2)
-- Per group: the 2nd biggest expense of every user
SELECT user_id, title, amount FROM (
SELECT user_id, title, amount,
dense_rank() OVER (PARTITION BY user_id ORDER BY amount DESC) AS r
FROM expenses
) t
WHERE r = 2
ORDER BY user_id;
-- 1 Monthly groceries 5200 | 2 Jacket 4200 | 3 Laptop bag 3500 | 4 Groceries 1750
-- A reusable function: Nth highest, NULL if it doesn't exist
CREATE FUNCTION nth_highest_expense(n INT) RETURNS NUMERIC LANGUAGE sql STABLE AS $$
SELECT DISTINCT amount FROM (
SELECT amount, dense_rank() OVER (ORDER BY amount DESC) AS r FROM expenses
) t WHERE r = n;
$$;
SELECT nth_highest_expense(1) AS first, nth_highest_expense(2) AS second, nth_highest_expense(100) AS hundredth;
-- 8500.00 | 7800.00 | NULL
-- Method 4: correlated count (portable but slow on large tables)
SELECT DISTINCT e1.amount FROM expenses e1
WHERE 1 = (SELECT count(DISTINCT e2.amount) FROM expenses e2 WHERE e2.amount > e1.amount); -- 7800.00