SQL Window Functions

Problem Write a SQL query that uses window functions — ROW_NUMBER, RANK / DENSE_RANK, or a running aggregate via OVER / PARTITION BY — to rank rows within groups or compute a running total without collapsing the result set.

Schema

  • sales(sale_id, category, product, value, sold_on) — many rows per category

Example

  • sales = {(A, gizmo, 50, Jan), (A, widget, 30, Feb), (B, bolt, 20, Jan)}
  • Produce, for each row, its rank within its category by value, and a running total of value within each category ordered by sold_on — every original row must be preserved.
asked …
LeaderboardSalaryAccount