Skip to main content
SQL โ€ข LESSON 27

SQL PARTITION BY

How can we compute rankings or window metrics independently within each department?

Advancedโฑ 3 Minutesโญ 460 XP
๐Ÿค” THE QUESTION

How can we compute rankings or window metrics independently within each department?

๐Ÿ’ก WHAT IS IT?

PARTITION BY divides the result set into window partitions before applying analytical functions.

๐ŸŽฏ WHAT IS IT USED FOR?

Ranking employees within their department, calculating customer-level metrics in event logs.

๐Ÿ’ป EXAMPLE
SELECT name,
department,
salary,
RANK() OVER (
  PARTITION BY department
  ORDER BY salary DESC
) AS department_rank
FROM employees;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for SQL PARTITION BY.

  • โœ“ PARTITION BY
  • โœ“ Scoped window
  • โœ“ Group ranking
  • โœ“ OVER (PARTITION BY)