SQL • LESSON 27
PARTITION BY
Learn how to divide query results into groups while applying window functions independently to each group.
🔴 Advanced⏱ 3 Minutes⭐ 460 XP
🤔 THE QUESTION
How can we rank employees separately within each department?
💡 WHAT IS IT?
PARTITION BY divides the rows into groups before the window function performs its calculation.
🎯 WHAT IS IT USED FOR?
It is useful when you need rankings, totals, averages, or other calculations separately for each category, department, customer, or group.
💻 EXAMPLE
SELECT name,
department,
salary,
RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS department_rank
FROM employees;Mission Brief
Practice using PARTITION BY to rank employees separately within each department.
Objectives
- ✓ SELECT
- ✓ RANK()
- ✓ PARTITION BY
- ✓ ORDER BY
- ✓ Window functions