This repository contains examples of using the PARTITION BY
clause in SQL to perform various analytical functions. These examples demonstrate how to leverage PARTITION BY
to group data within partitions and perform calculations or rankings over those partitions.
The PARTITION BY
clause in SQL is used to divide the result set into partitions and perform computations on each partition independently. It is commonly used with window functions to calculate aggregations, rankings, and other operations across partitions of data.
- Partitioning: The data is divided into segments (partitions) based on one or more columns.
- Window Functions: Functions like
ROW_NUMBER()
,RANK()
,DENSE_RANK()
,SUM()
,AVG()
,COUNT()
,LAG()
,LEAD()
,FIRST_VALUE()
, andLAST_VALUE()
can be used withPARTITION BY
.
Assigns a unique sequential integer to rows within a partition, ordered by the specified column.
Description: This example assigns a unique row number to each employee within their department based on their salary in descending order.
Assigns a rank to each row within a partition, with gaps in the ranking for ties.
Description: This example ranks employees within their department based on their salary in descending order. Employees with the same salary receive the same rank, with a gap following ties.
Assigns a rank to each row within a partition, without gaps in the ranking for ties.
Description: Similar to RANK()
, but without gaps in the ranking sequence. Employees with the same salary receive the same rank, and the next rank continues sequentially.
Calculates the sum of a column within each partition.
Description: This example calculates the total salary for each department.
Calculates the average of a column within each partition.
Description: This example calculates the average salary for each department.
Counts the number of rows within each partition.
Description: This example counts the number of employees in each department.
Accesses data from a previous row within the same partition.
Description: This example retrieves the salary of the previous employee within the same department based on hire date. If there is no previous employee, it returns 0.
Accesses data from a subsequent row within the same partition.
Description: This example retrieves the salary of the next employee within the same department based on hire date. If there is no next employee, it returns 0.
Returns the first value in an ordered partition.
Description: This example retrieves the first salary in each department based on hire date.
Returns the last value in an ordered partition.
Description: This example retrieves the last salary in each department based on hire date.
- Ensure you have a SQL environment set up.
- Create the sample data table and insert the sample data.
- Run the provided SQL scripts to see the results of each example.
Feel free to explore these examples to gain a deeper understanding of how PARTITION BY
works and how you can use it in your SQL queries.