OVER() and Partitioning in sql
The OVER() clause function in SQL database management systems is an essential function for applying built-in SQL database window functions such as ROW_NUMBER(), RANK(), DENSE_RANK(), and NTILE(), as well as aggregate numeric functions like SUM() and AVG(). It provides database users with the ability to apply multiple numeric function calculations to the output of table rows related to the current table row, without combining them in the table result output. This feature is particularly helpful for database users in generating or calculating rankings, cumulative sums, and moving averages.

The partitioning concept in SQL database tables is an advanced feature applied with the OVER() clause statement, which divides and displays the database table result output into smaller groups or partitions, allowing the window functions used in the database table to be applied separately or individually to each partition.
OVER() clause function in SQL databases.
The OVER() clause function in SQL database tables defines the window on which a window function operates. The OVER clause function can have two main component elements:
Elements of the OVER() clause function.
- PARTITION BY – This divides the table data into group partitions or subsets, and the window function is applied separately to each table partition group.
- ORDER BY – This indicates the order of table rows within each partition to specify the calculation behavior in the database table. Users can use this for window functions like ROW_NUMBER() or RANK().
Syntax of the OVER() clause function.
SELECT column_name,
window_function() OVER (PARTITION BY column_name ORDER BY column_name) AS window_function_output
FROM table_name;
Explanation of the OVER() clause function.
- window_function() – Here, any window function can be applied by the database user, such as ROW_NUMBER(), SUM(), RANK(), etc.
- PARTITION BY (Optional) – This is used to divide the table data into partitions; it is primarily used to group the data for the window function.
- ORDER BY (Optional) – This indicates the arrangement or order of the table data within the OVER clause, specifying the order or sequence in which the window function operates within each partition.
Partitioning with the OVER() clause in SQL databases.
When a database user applies a partitioning function with the OVER() clause in a table, the user divides the result output into smaller groups or subset partitions based on the data values in one or more database table columns. This allows the user to perform calculations separately for each table data partition using the applied window function.
Syntax of partitioning in the OVER() clause.
SELECT column_name,
window_function() OVER (PARTITION BY column_name ORDER BY column_name) AS window_function_output
FROM table_name;
Elements of partitioning in the OVER() clause.
- PARTITION BY column_name – This defines the partition grouping table data column values with the OVER clause, by which the table data is divided before applying the window function to the table.
Using the ORDER BY concept within the OVER() clause.
The ORDER BY clause in a database table helps the user determine how the applied window function calculates the table output column results within a partition. It controls the order or sequence of rows for functions in those database tables where data columns require sequential or ordered processing, such as ROW_NUMBER(), RANK(), NTILE(), and other database table window functions.
ROW_NUMBER() example with ORDER BY. So, let’s assign row numbers to employees in the employee table based on their salary within each department.
SELECT employee_id, department_id, salary,
ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS row_num
FROM employee;
Here, the ROW_NUMBER() window function restarts the numbering for each employee department. The PARTITION BY department_id clause orders or arranges the employees within each department in descending order based on their salary.
Example of NTILE() with ORDER BY.
Let’s divide and display the employees in the employee table into 3 quartiles based on their salary.
SELECT employee_id, department_id, salary,
NTILE(3) OVER (PARTITION BY department_id ORDER BY salary DESC) AS quartile
FROM employee;
Explanation of NTILE() with ORDER BY.
- Here, the NTILE(3) window function divides and displays the employees in each department into 3 quartiles based on their salary, with the highest-paid employees appearing in the first quartile and the lowest-paid employees in the last quartile.
- The table data is ordered by salary in descending order within each department to ensure that the highest-paid employees are displayed in the first quartile.
Conclusion of the OVER() and Partitioning window functions.
- The OVER() clause is a powerful SQL window function that enables database users to perform window-based calculations such as data ranking, total sums, and moving averages without reducing the table column output.
- In database tables, partitioning with PARTITION BY in the OVER() clause statement is necessary to allow database users to combine and divide table column data into multiple groups and calculate the output individually for each table column group. This is especially useful when working with grouped or hierarchical data, such as employees in employee departments or sales by area.
- When applying the ORDER BY clause statement with the OVER() clause window function, it allows database users to specify how the table rows are arranged within the table group partitions. This makes it possible and easier for database users to rank them in a proper order or method, or to perform cumulative value calculations.
