SQL Quest › SQL Exercises › Window Functions
SQL Window Functions Practice
64 Challenges (23 Free)
64 hands-on challenges counted from the live bank — 3 Easy, 14 Medium, 47 Hard — covering every window function pattern: ranking, LAG/LEAD, running totals and frame clauses. 23 are free to play, including 6 free Hard previews; the rest are Pro. The most-tested topic in FAANG SQL interviews. Real datasets. AI tutoring.
Start Practicing Free →
What are SQL window functions?
A window function performs a calculation across a set of rows related to the current row — without collapsing them the way GROUP BY does. The window is defined by an OVER clause.
Example — rank employees by salary within each department
SELECT
employee_id,
department,
salary,
RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS salary_rank
FROM employees;
💡 This returns every row alongside each employee's salary rank within their department — something you cannot do with GROUP BY alone.
Window functions covered in this track
Ranking functions
ROW_NUMBER() — unique sequential number per row
RANK() — rank with gaps for ties
DENSE_RANK() — rank without gaps
NTILE(n) / PERCENT_RANK() — buckets and percentiles
26 challenges · 8 free
See the ranking challenges →
Offset functions
LAG(col, n) — access a previous row
LEAD(col, n) — access a future row
- Month-over-month, year-over-year, sessionization
14 challenges · 4 free
See the LAG/LEAD challenges →
Running totals and frame clauses
SUM() OVER (ORDER BY …) — running totals
AVG() OVER (ROWS BETWEEN N PRECEDING AND CURRENT ROW) — moving averages
FIRST_VALUE / LAST_VALUE, RANGE vs ROWS frames
9 challenges · 3 free
See the running-total challenges →
Ranking — ROW_NUMBER, RANK, DENSE_RANK, NTILE: 26 challenges (8 free)
Every challenge below carries a ranking-function tag. ROW_NUMBER for "the latest order per customer" and de-duplication, RANK vs DENSE_RANK for ties, NTILE and PERCENT_RANK for buckets and percentiles. Start with Rank Everyone by Salary, the Easy one; the Medium ones are free and the Hard ones are Pro.
Counted from the challenge bank, September 2026. Easy first, then Medium, then Hard.
Medium Free
Uses: RANK DENSE_RANK
Try it →
Medium Free
Uses: DENSE_RANK CTE
Try it →
Medium Free
Uses: ROW_NUMBER CTE
Try it →
Medium Free
Uses: ROW_NUMBER CTE PARTITION BY
Try it →
Medium Free
Uses: ROW_NUMBER PARTITION BY GROUP BY
Try it →
Medium Free
Uses: RANK ROW_NUMBER GROUP BY
Try it →
Hard Pro
Uses: JOIN Subquery NTILE GROUP BY
Try it →
Hard Pro
Uses: RANK DENSE_RANK ROW_NUMBER CTE
Try it →
Hard Pro
Uses: ROW_NUMBER PARTITION BY CTE Window Functions + CTE
Try it →
Hard Pro
Uses: PERCENT_RANK ROUND
Try it →
Hard Pro
Uses: CTE ROW_NUMBER PARTITION BY GROUP BY
Try it →
Hard Pro
Uses: ROW_NUMBER COUNT Subquery CTE
Try it →
Hard Pro
Uses: CTE ROW_NUMBER GROUP BY CASE
Try it →
Hard Pro
Uses: NTILE PARTITION BY CASE
Try it →
Hard Pro
Uses: DENSE_RANK PARTITION BY CTE Window Functions + CTE
Try it →
Hard Pro
Uses: ROW_NUMBER PARTITION BY Subquery
Try it →
Hard Pro
Finance & Banking track · Uses: RANK PARTITION BY
Try it →
Hard Pro
Real Estate track · Uses: RANK PARTITION BY
Try it →
Hard Pro
Manufacturing & Industry track · Uses: RANK PARTITION BY
Try it →
Hard Pro
Uses: ROW_NUMBER LEAD CTE
Try it →
Hard Pro
Uses: ROW_NUMBER CTE CASE Subquery
Try it →
Hard Pro
Uses: SELECT CTE Window Functions NTILE JOIN SUM
Try it →
Hard Pro
Uses: SELECT CTE Window Functions ROW_NUMBER JOIN GROUP BY
Try it →
LAG and LEAD — the previous and next row: 15 challenges (4 free)
Offset functions read a neighbouring row without a self-join: last month's revenue for a growth rate, the previous order's timestamp for a session gap, next quarter's assets for a trend. Start with The Previous Order's Total (LAG); Year-over-Year Growth is the free Hard preview.
Counted from the challenge bank, September 2026. Medium first, then Hard, free previews before Pro.
Medium Free
Uses: GROUP BY Aggregation LAG Date Functions
Try it →
Medium Free
Uses: LAG Date Functions
Try it →
Medium Free
Uses: LAG CTE strftime
Try it →
Hard Free preview
Uses: Subquery GROUP BY Aggregation ORDER BY
Try it →
Hard Pro
Uses: LAG LEAD PARTITION BY
Try it →
Hard Pro
Uses: CTE LAG CASE SUM
Try it →
Hard Pro
Uses: CTE LAG Running Total GROUP BY
Try it →
Hard Pro
Uses: LAG Date Functions GROUP BY CTE
Try it →
Hard Pro
Uses: LAG GROUP BY CTE
Try it →
Hard Pro
Finance & Banking track · Uses: LAG JOIN
Try it →
Hard Pro
Real Estate track · Uses: LAG Date Functions GROUP BY
Try it →
Hard Pro
Finance & Banking track · Uses: LAG JOIN
Try it →
Hard Pro
Finance & Banking track · Uses: LAG Date Functions
Try it →
Hard Pro
Uses: LEAD ROW_NUMBER CTE
Try it →
Hard Pro
Uses: SELECT CTE Window Functions LAG strftime NULL Handling
Try it →
Running totals, moving averages and frame clauses: 9 challenges (3 free)
An aggregate with an ORDER BY inside its OVER becomes cumulative; a ROWS BETWEEN frame turns it into a rolling window. Start with Running Total of Orders, then the free Hard preview 7-Day Rolling Revenue Average. For the walkthrough, read running totals in SQL.
Counted from the challenge bank, September 2026. Medium first, then Hard, free previews before Pro.
Medium Free
Uses: SUM Running Total
Try it →
Medium Free
Uses: SUM Frame Clause CTE
Try it →
Hard Free preview
Uses: CTE Frame Clause GROUP BY
Try it →
Hard Pro
Uses: FIRST_VALUE LAST_VALUE Frame Clause
Try it →
Hard Pro
Uses: Frame Clause CTE GROUP BY HAVING
Try it →
Hard Pro
Uses: Frame Clause ROWS BETWEEN ORDER BY
Try it →
Hard Pro
Uses: CTE LAG Running Total GROUP BY
Try it →
Hard Pro
Uses: MAX Frame Clause
Try it →
Hard Pro
Uses: Frame Clause CTE JULIANDAY
Try it →
Why window functions matter for interviews
Window functions appear in approximately 70% of hard SQL interview questions at Meta, Google, Amazon, and other top tech companies. Mastering them is the single highest-ROI thing you can do to prepare for a data role interview.
They're also highly practical — window functions power retention cohorts, rankings, running totals, and period-over-period comparisons that data teams run every day.
Ready to master window functions?
64 challenges, 23 free to play. Real datasets, AI tutoring.
Start Practicing Free →