SQL practice

SQL window functions practice questions

33 problems, ordered from easiest. They run in your browser, and 2 are free.

Start with 3-day moving average of transactions

The idea in a minute

A window function calculates across a set of related rows but keeps every row. That is the difference from GROUP BY, which squashes rows into one per group.

You write the function, then OVER with a PARTITION BY (the groups) and an ORDER BY (the sequence). Ranking, running totals and comparing a row with the previous one are all this one idea.

Example: Rank orders inside each city

CREATE TABLE orders AS SELECT * FROM (VALUES
 (1,'Asha','Pune',450),(2,'Ravi','Pune',300),(3,'Meera','Delhi',900),(4,'Asha','Delhi',150),(5,'Ravi','Pune',250)
) t(order_id, customer, city, amount);
SELECT city, order_id, amount,
       RANK() OVER (PARTITION BY city ORDER BY amount DESC) AS rank_in_city
FROM orders
ORDER BY city, rank_in_city

What it prints

city   order_id  amount  rank_in_city
Delhi  3         900     1
Delhi  4         150     2
Pune   1         450     1
Pune   2         300     2
Pune   5         250     3

What to watch for

  • RANK leaves gaps after a tie, DENSE_RANK does not, and ROW_NUMBER never repeats.
  • A window function cannot be used directly in WHERE. Put it in a subquery or CTE first, then filter.
  • Without ORDER BY inside OVER, a running total is the same on every row.

Window Functions problems

  1. 3-day moving average of transactionsintermediate · Free
  2. Sessionize events with 30-minute gapsintermediate · Free
  3. Second highest salary per departmentintermediate · Locked
  4. Running total by customerintermediate · Locked
  5. Salary percent of department totalintermediate · Locked
  6. Top three products per categoryintermediate · Locked
  7. Month-over-month revenue with LAGintermediate · Locked
  8. Year-over-year revenue growthintermediate · Locked
  9. RANK vs DENSE_RANK vs ROW_NUMBERintermediate · Locked
  10. Highest and lowest salary per departmentintermediate · Locked
  11. Salary percentile within departmentintermediate · Locked
  12. Time since previous loginintermediate · Locked
  13. Cumulative orders per productintermediate · Locked
  14. Top three salaries with tiesintermediate · Locked
  15. New versus repeat customers per dayintermediate · Locked
  16. Tournament winners per groupintermediate · Locked
  17. Products making 80% of revenueintermediate · Locked
  18. Employee swipe in / swipe outintermediate · Locked
  19. How many products fit the budget?intermediate · Locked
  20. Exchange seatsintermediate · Locked
  21. Cities where cases rose every dayintermediate · Locked
  22. Session count and length per userintermediate · Locked
  23. Longest consecutive login streakadvanced · Locked
  24. Yearly resetting running totaladvanced · Locked
  25. Customers with month-over-month spend declineadvanced · Locked
  26. Rolling 7-day active users and DAU/MAUadvanced · Locked
  27. Forward-fill the last known readingadvanced · Locked
  28. Human Traffic of Stadiumadvanced · Locked
  29. Is the second item sold the seller's favourite brand?advanced · Locked
  30. Median salary per company without MEDIAN()advanced · Locked
  31. Quiet students in all examsadvanced · Locked
  32. Repeated payments within 10 minutesadvanced · Locked
  33. Merge overlapping subscription periodsadvanced · Locked

Want a guided order across every topic? Open the practice sheets or the SQL hub.