đĒ The PARTITION BY clause is used with SQL window functions to divide a result set into logical groups (partitions). Each partition is processed independently, allowing calculations such as rankings, running totals, averages, and comparisons to restart for every group while still returning every row.
đ What is PARTITION BY?
PARTITION BY is part of the OVER() clause used by window functions. Instead of combining rows like GROUP BY, it keeps every row in the result set and performs calculations separately within each partition.
Information
đ¯ Why Use PARTITION BY?
PARTITION BY makes analytical queries more flexible by allowing calculations within specific groups.
- đ Restart rankings for each group.
- đ Calculate department-wise averages.
- đ Generate running totals per category.
- đ Compare rows within the same group.
- đ Preserve every row in the result.
đ Sample Table
| EmployeeID | EmployeeName | Department | Salary |
|---|---|---|---|
| 101 | Alice | Sales | 70000 |
| 102 | Bob | Sales | 65000 |
| 103 | Charlie | IT | 80000 |
| 104 | David | IT | 75000 |
| 105 | Emma | HR | 60000 |
đ Basic Syntax
PARTITION BY Syntax
SELECT
column_name,
window_function() OVER (
PARTITION BY partition_column
ORDER BY order_column
) AS result
FROM table_name;The PARTITION BY clause defines how rows are grouped, while ORDER BY determines the processing order within each partition.
đĄ Example: Rank Employees Within Each Department
ROW_NUMBER() with PARTITION BY
SELECT
EmployeeName,
Department,
Salary,
ROW_NUMBER() OVER (
PARTITION BY Department
ORDER BY Salary DESC
) AS DepartmentRank
FROM Employees;The row numbering restarts from 1 for every department.
đ Example Output
| Employee | Department | Salary | DepartmentRank |
|---|---|---|---|
| Alice | Sales | 70000 | 1 |
| Bob | Sales | 65000 | 2 |
| Charlie | IT | 80000 | 1 |
| David | IT | 75000 | 2 |
| Emma | HR | 60000 | 1 |
đĄ Example: Department Average Salary
AVG() with PARTITION BY
SELECT
EmployeeName,
Department,
Salary,
AVG(Salary) OVER (
PARTITION BY Department
) AS DepartmentAverage
FROM Employees;Every employee row displays the average salary of its department while keeping all rows in the result.
đ Example Output
| Employee | Department | Salary | DepartmentAverage |
|---|---|---|---|
| Alice | Sales | 70000 | 67500 |
| Bob | Sales | 65000 | 67500 |
| Charlie | IT | 80000 | 77500 |
| David | IT | 75000 | 77500 |
| Emma | HR | 60000 | 60000 |
đĄ Example: Running Total Per Department
SUM() with PARTITION BY
SELECT
EmployeeName,
Department,
Salary,
SUM(Salary) OVER (
PARTITION BY Department
ORDER BY Salary
) AS RunningTotal
FROM Employees;The running total starts over whenever the department changes.
đĄ Example: Compare with Previous Salary
LAG() with PARTITION BY
SELECT
EmployeeName,
Department,
Salary,
LAG(Salary) OVER (
PARTITION BY Department
ORDER BY Salary
) AS PreviousSalary
FROM Employees;Each employee is compared only with previous employees in the same department.
đ Common Window Functions That Use PARTITION BY
| Function | Typical Purpose |
|---|---|
| ROW_NUMBER() | Unique numbering within each partition. |
| RANK() | Ranking with gaps. |
| DENSE_RANK() | Ranking without gaps. |
| LAG() | Access previous row in a partition. |
| LEAD() | Access next row in a partition. |
| SUM() | Running totals by group. |
| AVG() | Group averages. |
| COUNT() | Count rows in each partition. |
âī¸ PARTITION BY vs GROUP BY
| PARTITION BY | GROUP BY |
|---|---|
| Keeps every row. | Returns one row per group. |
| Used with window functions. | Used with aggregate functions. |
| Performs calculations within groups. | Summarizes each group. |
| Does not collapse rows. | Collapses rows into grouped results. |
Important
đŧ Real-World Applications
- đĸ Rank employees within each department.
- đ Calculate regional sales totals.
- đĻ Analyze account transactions per customer.
- đ Compare product performance within each category.
- đ Rank students within each class.
- đ Generate business intelligence reports.
đī¸ Database Compatibility
| Database System | PARTITION BY Support |
|---|---|
| MySQL | â Supported in MySQL 8.0 and later. |
| PostgreSQL | â Fully supported. |
| SQL Server | â Fully supported. |
| Oracle | â Fully supported. |
| SQLite | â Supported in SQLite 3.25.0 and later. |
â ī¸ Common Mistakes
- â Confusing PARTITION BY with GROUP BY.
- â Forgetting ORDER BY when row sequence affects the calculation.
- â Partitioning by the wrong column.
- â Assuming partitions change the number of returned rows.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ PARTITION BY divides rows into independent groups.
- đ It is used inside the OVER() clause.
- đ It works with SQL window functions.
- đ Every row remains in the final result.
- đ It differs from GROUP BY, which aggregates rows.
- đ It is essential for rankings, running totals, comparisons, and analytical reporting.