SQL Window Functions Interview Checklist
If a SQL interview question says "for each," "top N per group," "running total," or "compared to the previous row," it's almost always a window function question. Candidates who know window functions solve these in a few lines. Candidates who don't end up with fragile self-joins and nested subqueries that eat the clock.
This SQL window functions interview checklist covers the functions you'll actually use, how to choose between them, and the mistakes that cost the most time.
The core syntax, in one line
function_name(...) OVER (PARTITION BY group_cols ORDER BY sort_cols frame_clause)
- PARTITION BY splits rows into groups, like GROUP BY, but without collapsing them.
- ORDER BY defines the order within each group.
- Frame clause (optional) decides which rows around the current one are included.
Say this out loud in the interview: "A window function computes over a group of rows but keeps every row in the output." That one sentence shows you understand the difference from GROUP BY.
Ranking: pick the right function
| Function | Ties | Gaps after ties | Use when |
|---|---|---|---|
ROW_NUMBER() | Broken arbitrarily | n/a | You need exactly one row per rank |
RANK() | Same rank | Yes (1, 1, 3) | Competition-style ranking |
DENSE_RANK() | Same rank | No (1, 1, 2) | "Second highest salary" style questions |
Ask the interviewer how ties should be handled before you pick. It's one of the best clarifying questions in a SQL round.
Top N per group
SELECT *
FROM (
SELECT e.*,
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rnk
FROM employees e
) ranked
WHERE rnk <= 3;
Remember: you can't filter on a window function in the same query's WHERE clause, because WHERE runs before window functions are computed. Wrap it in a subquery or CTE. Some databases support QUALIFY for this; mention it only if you know the engine supports it.
Running totals and moving averages
SELECT order_date,
amount,
SUM(amount) OVER (ORDER BY order_date) AS running_total,
AVG(amount) OVER (ORDER BY order_date
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS avg_7_rows
FROM orders;
Key point to explain: when you add ORDER BY to an aggregate window, many engines default to a frame of RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With duplicate dates, RANGE treats tied rows as peers and includes all of them, which can surprise you. If you want strict row-by-row behavior, say ROWS explicitly.
Comparing to previous or next rows
LAG and LEAD replace most self-joins:
SELECT user_id,
login_date,
LAG(login_date) OVER (PARTITION BY user_id ORDER BY login_date) AS prev_login
FROM logins;
Common uses:
- Day-over-day change:
revenue - LAG(revenue) OVER (ORDER BY day) - Gaps between events: compare a date to the previous date
- Consecutive streaks: combine
LAGorROW_NUMBERwith date math
Mention the default: LAG returns NULL for the first row in each partition, and you can pass a default value as the third argument.
Other functions worth knowing
FIRST_VALUE()andLAST_VALUE()— watch the frame forLAST_VALUE; by default it often stops at the current row.NTILE(n)— split rows into n roughly equal buckets (quartiles, deciles).COUNT(*) OVER (PARTITION BY ...)— attach a group size to every row.SUM(...) OVER (PARTITION BY ...)with no ORDER BY — group total on every row, useful for percent-of-total.
A classic: consecutive days
"Find users who logged in three days in a row" is a common pattern. One approach:
- Assign
ROW_NUMBER()per user ordered by date. - Subtract that number (as days) from the date. Consecutive dates share the same result.
- Group by user and that derived value, then filter groups with count of three or more.
Explaining the trick before writing it is worth as much as the query itself.
Mistakes that cost the most time
- Filtering a window result in WHERE. Use a subquery or CTE.
- Forgetting PARTITION BY. You get one ranking across the whole table instead of per group.
- Using ROW_NUMBER when ties matter. Ask about ties first.
- Ignoring the frame default. Running totals with duplicate sort values behave differently under RANGE and ROWS.
- Mixing GROUP BY and windows carelessly. Windows run after GROUP BY, so you can only window over grouped columns or aggregates.
How to talk through a SQL window answer
- Restate the output: "One row per department with its top three salaries."
- Name the tool: "That's top N per group, so a ranking window with PARTITION BY."
- Clarify ties: "Should equal salaries share a rank?"
- Write the inner query, then the outer filter.
- Walk through a tiny example of three or four rows.
Practice it under a clock
Window functions click fast once you've written a dozen of them, but under time pressure the syntax details slip. If you want a calm, structured way to rehearse out loud before your interview, Luna Interview is built for practice and prep—not for use during a live assessment.
Checklist
- [ ] Can explain window functions vs GROUP BY in one sentence
- [ ] Knows ROW_NUMBER vs RANK vs DENSE_RANK and asks about ties
- [ ] Writes top-N-per-group with a subquery or CTE
- [ ] Uses LAG/LEAD instead of self-joins
- [ ] Knows the ROWS vs RANGE frame difference
- [ ] Can explain the consecutive-days trick
Master these and most "tricky" SQL interview questions become routine.
Rehearse with an AI interview copilot, then browse commonly reported interview questions.