PARTITION BY
Learn how to use PARTITION BY with SQL Server Window Functions to perform calculations separately for different groups.
What is PARTITION BY?
PARTITION BY is used with SQL Server Window Functions to divide the result set into separate groups.
The Window Function then performs its calculation separately within each group.
Unlike GROUP BY, PARTITION BY does not combine rows into a single row. All original rows remain in the result.
Syntax
WindowFunction()
OVER
(
PARTITION BY column_name
ORDER BY column_name
)
PARTITION BY defines the groups, while ORDER BY determines the order within each group.
Sample Employee Data
| EmployeeId | EmployeeName | Department | Salary |
|---|---|---|---|
| 101 | Amit | IT | 70000 |
| 102 | Rahul | IT | 80000 |
| 103 | Priya | HR | 60000 |
| 104 | Neha | HR | 65000 |
ROW_NUMBER() with PARTITION BY
We can use PARTITION BY with ROW_NUMBER() to assign row numbers separately within each department.
SELECT
EmployeeName,
Department,
Salary,
ROW_NUMBER() OVER
(
PARTITION BY Department
ORDER BY Salary DESC
) AS RowNumber
FROM Employees;
ROW_NUMBER() Output
| EmployeeName | Department | Salary | RowNumber |
|---|---|---|---|
| Rahul | IT | 80000 | 1 |
| Amit | IT | 70000 | 2 |
| Neha | HR | 65000 | 1 |
| Priya | HR | 60000 | 2 |
Notice that numbering starts again from 1 for each department.
RANK() with PARTITION BY
PARTITION BY can also be used with RANK() to rank employees separately within each department.
SELECT
EmployeeName,
Department,
Salary,
RANK() OVER
(
PARTITION BY Department
ORDER BY Salary DESC
) AS DepartmentRank
FROM Employees;
DENSE_RANK() with PARTITION BY
DENSE_RANK() can also calculate rankings separately for each group.
SELECT
EmployeeName,
Department,
Salary,
DENSE_RANK() OVER
(
PARTITION BY Department
ORDER BY Salary DESC
) AS DepartmentRank
FROM Employees;
SUM() with PARTITION BY
PARTITION BY is not limited to ranking functions. It can also be used with aggregate functions such as SUM().
SELECT
EmployeeName,
Department,
Salary,
SUM(Salary) OVER
(
PARTITION BY Department
) AS DepartmentTotalSalary
FROM Employees;
This calculates the total salary for each department while keeping every employee row in the result.
AVG() with PARTITION BY
SELECT
EmployeeName,
Department,
Salary,
AVG(Salary) OVER
(
PARTITION BY Department
) AS DepartmentAverageSalary
FROM Employees;
This calculates the average salary separately for every department.
LEAD() and LAG() with PARTITION BY
PARTITION BY can also be combined with LEAD() and LAG() to compare rows within each group.
SELECT
EmployeeName,
Department,
Salary,
LAG(Salary) OVER
(
PARTITION BY Department
ORDER BY Salary
) AS PreviousSalary
FROM Employees;
The previous salary is calculated separately within each department.
PARTITION BY vs GROUP BY
| Feature | PARTITION BY | GROUP BY |
|---|---|---|
| Groups data | Yes | Yes |
| Original rows remain | Yes | No |
| Used with Window Functions | Yes | No |
| Returns one row per group | No | Usually yes |
Practical Uses of PARTITION BY
- Department-wise employee ranking.
- Department-wise salary calculations.
- Category-wise product ranking.
- Customer-wise transaction analysis.
- Group-wise running totals.
- Comparing rows within individual groups.
Important Points
- PARTITION BY divides the result into groups.
- Window calculations are performed separately for each partition.
- PARTITION BY does not remove or combine rows.
- It can be used with ROW_NUMBER(), RANK(), DENSE_RANK(), LEAD(), LAG() and aggregate functions.
- ORDER BY can be used inside OVER() to define the order within each partition.
Summary
PARTITION BY divides rows into logical groups and allows Window Functions to perform calculations separately for each group.
It is commonly used with ROW_NUMBER(), RANK(), DENSE_RANK(), LEAD(), LAG(), SUM(), AVG() and other Window Functions.