SQL Window Functions Interview Questions
What Interviewers Are Testing
Window function interview questions assess your ability to perform complex analytics — ranking, running totals, moving averages — without collapsing rows the way GROUP BY does.
Interview Questions & Model Answers
What is the difference between ROW_NUMBER, RANK, and DENSE_RANK?
All three assign a number to rows in a partition ordered by a sort column, but they handle ties differently. ROW_NUMBER: always assigns unique sequential numbers, no ties (arbitrary order for tied values). RANK: gives tied rows the same number but skips the next rank(s). DENSE_RANK: gives tied rows the same number but does NOT skip the next rank.
-- Given salaries: 90k, 80k, 80k, 70k -- ROW_NUMBER: 1, 2, 3, 4 (arbitrary for ties) -- RANK: 1, 2, 2, 4 (skips 3) -- DENSE_RANK: 1, 2, 2, 3 (no skip) SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num, RANK() OVER (ORDER BY salary DESC) AS rnk, DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rnk FROM employees;
Potential Follow-up Questions:
- When would you use DENSE_RANK over RANK?
- How do you get the top N records per group?
How do you calculate a running total in SQL?
Use SUM() as a window function with ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. This accumulates the sum from the first row to the current row in the specified order.
SELECT order_date, amount,
SUM(amount) OVER (
ORDER BY order_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM orders;Potential Follow-up Questions:
- What is the difference between ROWS and RANGE in window frame?
How do you compare a row to its previous row using SQL?
Use the LAG() window function. LAG(col, n) returns the value of col from n rows before the current row in the window order. LEAD(col, n) does the same for rows ahead. This is ideal for calculating period-over-period changes.
SELECT month, revenue,
LAG(revenue) OVER (ORDER BY month) AS prev_month_revenue,
revenue - LAG(revenue) OVER (ORDER BY month) AS change,
ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
/ LAG(revenue) OVER (ORDER BY month), 2) AS pct_change
FROM monthly_sales;Potential Follow-up Questions:
- What does LAG return for the first row where there is no previous row?
How do you get the top 1 row per group in SQL?
Use ROW_NUMBER() with PARTITION BY in a CTE or subquery, then filter WHERE rn = 1. This is one of the most common window function interview patterns.
-- Top earner per department
WITH ranked AS (
SELECT name, department, salary,
ROW_NUMBER() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS rn
FROM employees
)
SELECT name, department, salary
FROM ranked
WHERE rn = 1;Potential Follow-up Questions:
- What if there are ties for the top position?
- How would you get the top 3 per group instead?
Frequently Asked Questions
Are window functions commonly asked in SQL interviews?
What is PARTITION BY in SQL window functions?
Related Interview Topics
Practice Answering Live
Use our Interview Arena to practice SQL challenges under real interview conditions. Free.