Interview Problems: Hard
Hard SQL problems combine two or three techniques: top N per group, medians, running totals, gaps and islands, filtered rates, pivots and recursive hierarchies.
Overview
Hard SQL problems rarely need an exotic feature. They need two or three ordinary techniques in the right order: rank, then filter; number the rows, then group; join, filter, then aggregate. They come up in senior backend, data engineering and analytics interviews.
The way through is to build the query in steps, each step a named CTE (a WITH query) that you can run and check on its own. A typical hard query has this shape.
- Start from the base tables.
- CTE 1 cleans and joins them.
- CTE 2 adds a rank, a row number or a running total.
- CTE 3 groups or filters on that new column.
- The final SELECT only orders and formats the result.
Each CTE does one job, so when the answer is wrong you can run them one at a time and find the step that broke.
The wording of the question usually names the technique for the middle step.
- The top N rows in each group: a window rank per group, then a filter on it.
- A median or percentile: percentile_cont, or row numbers counted from both ends.
- A running or moving total: SUM() OVER (ORDER BY ...) with the right frame.
- Streaks or consecutive rows: gaps and islands, where value minus row number labels each run.
- A rate with exclusions: join to filter, then AVG(CASE ...).
- Rows turned into columns: conditional aggregation, or FILTER.
- A tree of any depth: WITH RECURSIVE.
The problems below are written in our own words. Each block creates its own tiny tables and runs in PostgreSQL.
Top three salaries in each department
Return, for each department, every employee whose salary is among the three highest distinct salaries in that department.
The words 'distinct salaries' pick the ranking function. The three ranking functions differ only on ties, so start by running all three over one department.
- Ann — 120 — 1 — 1 — 1
- Bo — 110 — 2 — 2 — 2
- Cal — 110 — 3 — 2 — 2
- Dee — 100 — 4 — 4 — 3
- Eli — 90 — 5 — 5 — 4
Read the rows for Bo, Cal and Dee, where the three functions part ways.
- ROW_NUMBER breaks the tie arbitrarily: Bo and Cal get 2 and 3, and could swap. Use it when you want exactly N rows per group.
- RANK gives tied rows the same rank, then skips numbers: Bo and Cal are both 2, and Dee jumps to 4. Use it for the top N people, ties included.
- DENSE_RANK gives tied rows the same rank with no gaps: Bo and Cal are both 2, and Dee is 3. Use it for the top N distinct values, which is what this problem asks for.
Window functions run after WHERE, so you cannot filter on the rank in the same query. Rank in a CTE, then filter outside it.
For Eng it returns Ann at 120, Bo at 110, Cal at 110 and Dee at 100. For Ops it returns Fay at 80 and Gus at 70, since Ops has only two people. Eng returns four people, because Bo and Cal share the second-highest salary, and Eli at 90 is left out with dense rank 4.
Sign up free to read the full topic
More Databases & SQL topics
Start free on Cariara · Coding problems · System design studio