PARTITION BY
The PARTITION BY clause is one of the three available clauses used in the
OVER() statement when implementing any window function. It is used to define the subset of rows
on which the window
function can operate.
To demonstrate it's use let's query a sample employee data and use the ROW_NUMBER
function
to order the salaries. Within the same query, we will implement the PARTITION BY clause and examine the
differences in output
SELECT
first_name,
last_name,
department_id,
salary,
ROW_NUMBER() OVER( ORDER BY salary DESC) AS overall_rank,
ROW_NUMBER() OVER( PARTITION BY department_id ORDER BY salary DESC) as partitioned_rank
FROM employees
WHERE salary IS NOT NULL
LIMIT 30;
We can notice the difference in the partitioned_rank column that the rank is order by the department id. In this
way,
it offers the ranked paid salary for each employee by department id.
The PARTITION BY clause is a useful grouby feature that allows for performance of
aggregation at a colum category level. More complex computations can be performed this way