Skip to main content
Documents
Clauses StatementsKeywordsExpressionsFunctionsIndexesPerformance Optimization

MySQL OVER Clauses

The `OVER` clause in MySQL is used with window functions to define the set of rows for calculations like ranking, cumulative totals, or moving averages. It enhances aggregate functions by allowing them to process rows in relation to each other within a specified range.

Usage

The `OVER` clause is utilized with window functions to specify the partitioning, ordering, and framing of rows. It provides a framework for aggregate calculations over a defined window of rows.

sql
window_function(args) OVER ([PARTITION BY expr] [ORDER BY expr] [frame_clause])

In this syntax, `OVER` defines the window of rows the function should operate on, with optional `PARTITION BY` and `ORDER BY` clauses to refine the range. The `frame_clause` specifies the subset of rows within the partition to consider for each calculation and can be configured using `ROWS` or `RANGE`.

Examples of window functions that can be used with the `OVER` clause include `LEAD`, `LAG`, `FIRST_VALUE`, and `LAST_VALUE`, among others.

Examples

1. Basic Row Number

sql
SELECT employee_id, salary, ROW_NUMBER() OVER () AS row_num
FROM employees;

This example assigns a sequential row number to each row in the `employees` table without any specific ordering or partitioning.

2. Partitioning with Rank

sql
SELECT department_id, employee_id, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank
FROM employees;

Here, the `RANK()` function assigns a rank to each employee within their department based on salary, partitioning the data by `department_id`.

3. Cumulative Sum with Partition and Order

sql
SELECT employee_id, salary, SUM(salary) OVER (PARTITION BY department_id ORDER BY employee_id) AS cumulative_salary
FROM employees;

This example calculates a cumulative salary for employees within each department, ordered by `employee_id`.

Tips and Best Practices

  • Use partitions wisely. Partition data logically using `PARTITION BY` to ensure calculations are relevant to each group.
  • Order for accuracy. Use `ORDER BY` within `OVER` to define the sequence of row processing for precise results.
  • Frame it right. Understand and utilize the `frame_clause` to control the specific range of rows considered in calculations, such as `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`.
  • Optimize performance. Limit the size of the window, especially with large datasets, to maintain query performance. Ensure proper indexing and consider query optimization techniques.
  • Combine with WHERE. Filter data before using window functions to reduce the number of processed rows, improving efficiency.

SQL Upskilling for Beginners

Gain the SQL skills to interact with and query your data.
Start Learning for Free