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 BYsets the groups,ORDER BYthe order inside each.ROW_NUMBER,RANK,LAGand runningSUMare 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.
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.