SQL window functions: 10 patterns for real analysis
Window functions keep your rows while you calculate across them. Learn ten patterns: ranking, deduplication, running totals, LAG, cohorts and streaks.
Short answer: Most SQL interviews for analyst roles rotate through the same handful of patterns: joins and why rows duplicate, filtering vs aggregating with
GROUP BY/HAVING, window functions for ranking or running totals, finding duplicate rows, and one open-ended query where the interviewer cares more about your reasoning out loud than the exact syntax. Knowing the pattern behind a question matters more than memorizing a specific query.
Interviewers reuse the same underlying patterns across companies because they’re testing the same underlying skills, not because they’ve run out of ideas. Here’s what those patterns actually are.
This tests whether you understand LEFT JOIN versus INNER JOIN, and specifically whether you know that filtering a left-joined table’s column in WHERE (WHERE orders.id IS NULL) only works because the join already preserved the unmatched rows. See SQL joins explained for the mechanics if this pattern isn’t automatic yet.
This is a window function question wearing a plain-English disguise. The interviewer wants to see whether you reach for RANK() or DENSE_RANK() with PARTITION BY department rather than a nested subquery with MAX() that only works for the first-highest and gets awkward fast for “second.” See SQL window functions for the pattern this question is really testing.
Another window function question, this time testing SUM() OVER (ORDER BY ... ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) for a running total, or LAG() for comparing a row to the previous period. Interviewers use this to check whether you can keep every row while still calculating across them, instead of collapsing everything with GROUP BY when the question actually needed row-level detail preserved.
Tests GROUP BY with HAVING COUNT(*) > 1 for finding which values are duplicated, or ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...) for identifying and removing specific duplicate rows rather than just duplicate values. The distinction between “which values appear more than once” and “which exact rows should I delete, keeping one” is exactly the kind of clarifying detail a strong candidate confirms before writing the query. See how to find and fix duplicate rows in SQL for the full pattern.
A conceptual question, not a coding one, but it’s asked constantly because it reveals whether you understand SQL’s actual execution order — WHERE filters rows before grouping, HAVING filters groups after aggregation. See GROUP BY vs HAVING vs WHERE for the full execution order and why it matters.
This tests whether you can combine conditional aggregation (COUNT(CASE WHEN step = 'X' THEN 1 END)) or self-joins to compare counts at different funnel stages, and whether you think to check for the trap of double-counting a user who repeated a step, or the reverse — dropping a legitimate user because the join didn’t handle a missing step gracefully.
The exact syntax matters less than most candidates assume. What interviewers are actually watching for:
NULL values, ties, empty groups, and duplicate join keys are the recurring edge cases across almost every pattern above — mentioning them before being asked is a strong signal.NULL handling and ties explicitly, even when the interviewer doesn’t ask.For the reference patterns themselves, the SQL cheat sheet and CTE vs subquery vs temp table cover the building blocks most of these interview questions are assembled from.
Part of: SQL & Local Analytics, Analyst Craft & Authority
Free tools for this topic: RICE / ICE Prioritizer