Skip to content
CodeItRaw
Path

Computing across rows without grouping them: ranks, running totals and comparing with the previous row.

Key ideas

  • Window functions add a value to every row without merging rows.
  • PARTITION BY sets the groups, ORDER BY the order inside each.
  • ROW_NUMBER, RANK, LAG and running SUM are the usual ones.

Pattern

SELECT name, dept_id,
  RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS r
FROM employees;

Finish these first:SQL: Subqueries

Lessons

Each lesson explains one idea from zero and ends with a few tasks. Lessons open in order; the explanation can be read at any time.

  1. 01

    OVER: calculating without losing the rows

    0/3 tasks

Boss

  • 02

    Top two per department

    SQL query

  • The boss opens when every lesson of the topic is finished. Solve it too and the topic is complete, and the topics after it open.