Querydrill

Window functions in six sessions

Window functions decide most SQL interviews above entry level. This plan takes them one idea at a time: ranking, running totals, the previous and next row, the first row of a group, shares of a total, and finally the questions that combine them.

Eighteen exercises, three per session. Do them in order; each session reuses what the one before it taught.

0 of 18 solvedStart session 1

Session 1: RANK, DENSE_RANK and NTILE

  • Rank within departmentReturn each employee's name, dept_id and their salary rank inside their own department, where 1 is the highest salary.
  • Dense rankReturn every employee's name, salary and DENSE_RANK of the salary across the whole company, where 1 is the highest.
  • Salary quartilesSplit the employees into four groups of as equal size as possible by salary, highest salaries in group 1. Return each employee's name and their group number.

Session 2: Running totals

  • Running revenue totalFor every paid order, return the order id and the running total of paid revenue, ordered by order date and then by id.
  • Running total per customerFor every order, return the order id, the customer_id and the running total of that customer's order amounts, in order of date and then id. Count every order, whatever the status.
  • Cumulative share of revenueReturn each sold product with its revenue (quantity times price) and the cumulative share of total revenue in percent, rounded to 1 decimal, going from the product with the most revenue to the least. Order by revenue descending, then by product name. The last row shows 100.

Session 3: LAG and LEAD

  • Change from the previous orderFor every paid order, return the id and the difference between its amount and the amount of the previous paid order, ordered by order date then id. The first order shows NULL.
  • Days until the next orderFor every order, return the order id and the number of days until the same customer's next order (by date, then id), as a whole number. A customer's last order shows NULL. Count every order, whatever the status. In PostgreSQL, subtracting one date from another gives the number of days.
  • Month over monthReturn each month (like 2025-01), its paid revenue, and the change from the previous month's paid revenue. The first month shows NULL for the change.

Session 4: First rows and moving frames

  • Each customer's first orderFor each customer, return the customer_id and the id of their first order (earliest date, then lowest id). Count every order, whatever the status.
  • Compared to the first orderFor every order, return the order id, the customer_id and the difference between its amount and the amount of that customer's first order (earliest date, then lowest id). Count every order, whatever the status. The first order itself shows 0.
  • Moving averageGo through the paid orders by date and then id. For each one, return the id and the average amount of that order and the two paid orders before it, rounded to 2 decimals. The first two rows average over fewer orders.

Session 5: Shares and comparisons

  • Share of revenueFor each country, return the country and its share of total paid revenue in percent, rounded to 1 decimal.
  • Share of the customer's totalFor every paid order, return the id and its share of that customer's total paid amount in percent, rounded to 1 decimal. A customer with one paid order shows 100.
  • Orders above the customer's averageReturn the id of every paid order whose amount is above the average amount of that same customer's paid orders.

Session 6: Putting it together

  • Top two per categoryFor each category, return the category, product name and revenue (quantity times price) of its two best-selling products by revenue.
  • Second highest salary per departmentFor each department with at least two different salaries, return the dept_id and its second highest salary. Ignore employees without a department.
  • Runs of the same statusGo through the orders by id and group consecutive orders that share the same status into runs. For each run, return the status, the first order id, the last order id and the number of orders in the run.

Other plans and cases

Your first 8 exercises are free, with no account, from any plan or level. Progress is kept in this browser; a free account keeps it on every device.