What is the purpose of the OVER clause in SQL?

Learn SQL with Udemy

For an instructor lead, in-depth look at learning SQL click below.


SQL (Structured Query Language) is a ubiquitous language in the field of Data Analysis used to communicate with a database. Amongst the numerous functionalities in SQL is the OVER clause. To excel in data analysis, understanding enigmatic SQL functions such as the OVER clause is crucial. So, what is the purpose of the OVER clause?

Understanding the OVER clause

The OVER clause in SQL is used with functions to provide aggregated data based on a column or set of columns. It helps to perform calculations across rows that are related to the current row, rather than just on the set of rows returned to the user. Thus, the OVER clause shapes up the way the result of the function is calculated. Categories of functions used with the OVER clause include: Ranking functions (such as RANK, DENSE_RANK), Value functions (such as LAG, LEAD), and Aggregate functions (such as SUM, AVG, COUNT).

Basic Syntax

Examples of OVER clause in SQL

Example 1: Using OVER with Ranking function

Suppose we have a table named ‘Sales’ with columns for ‘SalesPerson’, ‘Region’ and ‘SalesValue’. We want to rank each ‘SalesPerson’ by their ‘SalesValue’in each ‘Region’. SQL code would be:

Example 2: Using OVER with Value function

Assume we have a table ‘Stocks’ with columns ‘Date’ and ‘Price’. To find the price difference from the previous day, we use the LAG function with the OVER clause. SQL code would be:

Example 3: Using OVER with Aggregate function

Consider a table ‘Orders’ with columns ‘OrderDate’ and ‘Amount’. To get a running total of ‘Amount’, we use the SUM function with the OVER clause. SQL code would be:

In conclusion, the OVER clause in SQL enhances the flexibility and power of SQL queries by allowing us to manage data sets without having to self-join the tables or create temporary tables. This makes data analysis tasks more streamlined and efficient.

Leave a Comment