SQL window function interview questions with worked answers
By InterviewKit · Updated · 9 practice questions
Use this small sales dataset to explain not just the query, but the result. The examples use PostgreSQL-compatible SQL. A row represents one transaction; sale_id is unique, and amount is non-null. These are original practice exercises.
1–3 years: ranking and the order of rows
1. How do ROW_NUMBER, RANK and DENSE_RANK handle ties?
For amounts 100, 100 and 50 ordered from largest to smallest, RANK produces 1, 1, 3 and DENSE_RANK produces 1, 1, 2. ROW_NUMBER assigns separate positions. Add a unique tie-breaker when you need predictable row numbers; leave it out of the ranking expression when equal amounts must share a rank.
2. What does PARTITION BY change?
It starts a separate calculation for each group without collapsing the detail rows. PARTITION BY customer_id gives each customer an independent sequence. Without it, the calculation spans all customers.
3. Does ORDER BY inside OVER sort the final output?
It defines the order used by the window calculation. Add an outer ORDER BY as well when the displayed result needs a guaranteed order. Keep both orders clear when explaining your answer.
4–7 years: running totals and comparisons
4. How do you calculate a running total when two transactions share a date?
Decide whether the business wants a total after each transaction or one total for the whole day. For transaction-by-transaction totals, use a unique sequence such as sale_date, sale_id and an explicit ROWS frame. For daily totals, aggregate by day first. The worked example below uses the first interpretation.
5. How do you compare each sale with the preceding sale?
Use LAG with the same customer partition and transaction order. The first row has no previous row, so the default result is NULL. Do not replace it with zero unless the business wants that assumption.
SELECT sale_id, customer_id, amount,
amount - LAG(amount) OVER (
PARTITION BY customer_id ORDER BY sale_date, sale_id
) AS change_from_previous
FROM sales
ORDER BY customer_id, sale_date, sale_id;6. How do you filter for the two highest distinct amounts per customer?
Rank by amount with DENSE_RANK inside a CTE, then filter the rank in the outer query. A tie can produce more than two rows. If you need exactly two transactions, use ROW_NUMBER with a deterministic tie-breaker instead.
WITH ranked AS (
SELECT sale_id, customer_id, amount,
DENSE_RANK() OVER (
PARTITION BY customer_id ORDER BY amount DESC
) AS amount_rank
FROM sales
)
SELECT * FROM ranked WHERE amount_rank <= 2;7+ years: correctness before optimisation
7. Why can adding a join change a running total?
A one-to-many join can repeat each transaction before the window runs. Check the input grain and count rows after the join. Aggregate the many side or calculate at the intended grain before attaching additional detail. A window function does not repair duplicated input.
8. When can the default window frame surprise you?
With an ordered aggregate, the default frame can include peers with the same ordering values. A running sum may therefore advance for a whole peer group. State the frame explicitly and test tied values. LAST_VALUE also depends on the frame, which may stop at the current peer group rather than the end of the partition.
9. What would you test before releasing a window query?
Use fixtures with ties, one-row customers, missing values and multiple transactions on one date. Check the expected output by hand, inspect the execution plan on realistic data, and measure sorts and resource use. An index is a hypothesis to test, not a guaranteed fix.
Worked example: a running total for each customer
Customer A has two transactions on 1 October and another on 2 October. Customer B has one transaction. Keep every transaction and calculate its cumulative amount. Insert these rows into sales(sale_id, customer_id, sale_date, amount).
| sale_id | customer_id | sale_date | amount |
|---|---|---|---|
| 1 | A | 2026-10-01 | 100 |
| 2 | A | 2026-10-01 | 100 |
| 3 | A | 2026-10-02 | 50 |
| 4 | B | 2026-10-01 | 70 |
SELECT sale_id, customer_id, sale_date, amount,
SUM(amount) OVER (
PARTITION BY customer_id
ORDER BY sale_date, sale_id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_amount
FROM sales
ORDER BY customer_id, sale_date, sale_id;| sale_id | customer_id | running_amount |
|---|---|---|
| 1 | A | 100 |
| 2 | A | 200 |
| 3 | A | 250 |
| 4 | B | 70 |
The two transactions on the first date are separate steps because sale_id breaks the tie. The partition resets for B. Follow-up: how would the query change if the interviewer wanted one row per customer per day?
Related practice guides
How to use this guide
Attempt each question before reading its answer. Explain your assumptions, show a small example and test an edge case. Use the experience bands as a guide to depth, rather than a promise about what an employer will ask.
These are independently written practice questions, not verified questions from particular employers. Free guide answers do not change the contents of the paid PDFs.
Compare available interview PDFsReady for a focused SQL window functions question bank?
Choose a PDF edition for deeper, topic-by-topic practice. Compare its sample questions, experience level, answer coverage and current price before buying.
View SQL window functions PDF optionsOne-time purchase. SQL PDFs include model answers; DAX and Power BI Service PDFs contain questions only.
Further reading
Check the official documentation when you need exact behaviour or current platform requirements.
Continue your preparation
All free interview guides · Power BI preparation · SQL PDFs · DAX PDFs