Three SQL questions show up in almost every developer interview: the second highest salary, finding duplicates and finding gaps. Each one has an answer that looks right and fails on real data. Every query below was run on PostgreSQL 19 and SQLite 3.51 with identical results.
Video version:
Q1: The second highest salary
CREATE TABLE employees (id int, name text, salary int);
INSERT INTO employees VALUES
(1, 'Ana', 100), (2, 'Ben', 100), (3, 'Chen', 90), (4, 'Dev', 80);
The answer most people write:
SELECT salary FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
-- 100
Ana and Ben are tied at 100, so skipping one row lands on Ben, and you get the highest salary again. (SELECT DISTINCT salary … LIMIT 1 OFFSET 1 gives 90, but returns no rows when there's no second salary.)
Answer A: the max below the max
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
-- 90
If everyone earns the same, this returns NULL instead of an empty result, which is what most interview versions (LeetCode's included) expect.
Answer B: DENSE_RANK()
SELECT name, salary FROM (
SELECT name, salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) r
WHERE rnk = 2;
-- Chen | 90
This also gives you who earns it, and generalizes to "Nth highest".
The follow-up: why not RANK()?
name | salary | RANK | DENSE_RANK
Ana | 100 | 1 | 1
Ben | 100 | 1 | 1
Chen | 90 | 3 | 2
Dev | 80 | 4 | 3
RANK() skips numbers after a tie. There is no rank 2, so WHERE rnk = 2 returns nothing.
Q2: Find duplicate emails
INSERT INTO users VALUES
(1, 'ana@x.io'), (2, 'ben@x.io'), (3, 'ana@x.io'), (4, 'Ben@x.io'), (5, 'chen@x.io');
SELECT LOWER(email) AS email, COUNT(*)
FROM users
GROUP BY LOWER(email)
HAVING COUNT(*) > 1;
-- ana@x.io | 2
-- ben@x.io | 2
Two details interviewers look for:
-
HAVING, notWHERE.WHERE COUNT(*) > 1fails. PostgreSQL saysaggregate functions are not allowed in WHERE, SQLite saysmisuse of aggregate: COUNT(). -
LOWER(email). With a plainGROUP BY email,ben@x.ioandBen@x.iocount as different, and you only find theanaduplicate.
The follow-up: show the duplicate rows
GROUP BY collapses rows, so you lose the IDs you need for a cleanup. A window COUNT keeps every row:
SELECT id, email FROM (
SELECT id, email,
COUNT(*) OVER (PARTITION BY LOWER(email)) AS n
FROM users
) d
WHERE n > 1
ORDER BY LOWER(email), id;
-- 1 | ana@x.io
-- 3 | ana@x.io
-- 2 | ben@x.io
-- 4 | Ben@x.io
Q3: Find gaps in invoice numbers
Invoices 1, 2, 3, 6, 7, 10. Which numbers are missing?
SELECT id + 1 AS gap_start,
next_id - 1 AS gap_end
FROM (
SELECT id, LEAD(id) OVER (ORDER BY id) AS next_id
FROM invoices
) g
WHERE next_id - id > 1;
-- 4 | 5
-- 8 | 9
LEAD(id) puts the next ID on the same row. If it's more than one higher, everything in between is missing.
Cheat sheet
- Second highest:
MAX(salary) WHERE salary < (SELECT MAX(salary) …), orDENSE_RANK() = 2. -
LIMIT 1 OFFSET 1breaks on ties;RANK()skips numbers after ties. - Duplicates:
GROUP BY LOWER(col) HAVING COUNT(*) > 1. - Duplicate rows with IDs:
COUNT(*) OVER (PARTITION BY …). - Gaps:
LEAD(id) OVER (ORDER BY id), keepnext_id - id > 1.
Which SQL question did you get in your last interview? 👇
I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.
Top comments (0)