0% found this document useful (0 votes)
11 views9 pages

Window Functions 1721585676

The document covers SQL window functions including ROW_NUMBER(), RANK(), and DENSE_RANK(), explaining their usage in assigning unique numbers and ranks to rows based on specified criteria. It also discusses the ROWS BETWEEN clause for defining window frames in calculations, as well as the LAG() and LEAD() functions for accessing data from preceding and following rows. Examples are provided to illustrate the application of these functions in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Window Functions 1721585676

The document covers SQL window functions including ROW_NUMBER(), RANK(), and DENSE_RANK(), explaining their usage in assigning unique numbers and ranks to rows based on specified criteria. It also discusses the ROWS BETWEEN clause for defining window frames in calculations, as well as the LAG() and LEAD() functions for accessing data from preceding and following rows. Examples are provided to illustrate the application of these functions in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SQL Learnings

Window Functions
Row Number
Rank
Dense Rank
Rows Between Preceding and Current Row
Rows Between Preceding and Following
Lag function using Rows Between
Lead function using Rows Between

Kanchan Prajapati
Row Number

ROW_NUMBER() is a window function that assigns a unique sequential integer to each row within a
partition of a result set, based on the specified ORDER BY clause.
In this query, ROW_NUMBER() OVER (ORDER BY salary DESC) assigns a unique number (Row_num) to
each row in the result set, ordered by salary in descending order.

SELECT emp_id, emp_name, dept_id, salary,


ROW_NUMBER() OVER (ORDER BY salary DESC) AS Row_num
FROM employee;
Rank

RANK() is a window function that assigns a rank to each row within the partition of a result set, with
gaps in rank values when there are ties.
In the query, RANK() OVER (ORDER BY salary DESC) assigns a rank (rank_) to each row in the result
set, where rows with the same salary get the same rank, and the next row gets a rank incremented by
the number of tied rows.

SELECT emp_id, emp_name, dept_id, salary,


RANK() OVER (ORDER BY salary DESC) AS Rank_
FROM employee;
Dense Rank

DENSE_RANK() is a window function that assigns a rank to each row within the partition of a result
set, without any gaps in rank values when there are ties.
In the query, DENSE_RANK() OVER (ORDER BY salary DESC) assigns a dense rank (dens_rank) to each
row in the result set, where rows with the same salary get the same rank, and the next row gets a rank
incremented by 1 regardless of ties.

SELECT emp_id, emp_name, dept_id, salary,


DENSE_RANK() OVER (ORDER BY salary DESC) AS dens_rank
FROM employee;
Rows Between
The ROWS BETWEEN PRECEDING AND CURRENT ROW clause allows you to define a window frame for
window functions like SUM(), specifying how many rows before and after the current row should be
included in the calculation.

For example: salesperson_id = 8, the ROWS BETWEEN 2 PRECEDING AND CURRENT ROW clause
includes the two rows before the current row and the current row itself. Therefore, the sum for the
current row would include the amounts from the two preceding rows (150 and 720) and the current row
(1800), resulting in a total of 150 + 720 + 1800 = 2670.
SELECT salesperson_id, order_number, order_date, amount,
SUM(amount) OVER (ORDER BY order_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
AS total_amount
FROM int_orders;
Rows Between
The ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING clause in SQL window functions defines a window
frame that includes the current row, the row immediately preceding it (one row before), and the row
immediately following it (one row after).
For example salesperson_id = 2, the `ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING` clause includes
the row immediately before the current row, the current row itself, and the row immediately after the
current row. Therefore, the sum for the current row would include the amounts from the preceding row
(460), the current row (540), and the following row (2400), resulting in a total of 460 + 540 + 2400 =
3400.
SELECT salesperson_id, order_number, order_date, amount,
SUM(amount) OVER (ORDER BY order_date ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS
total_sale
FROM int_orders;
Lag Function

The LAG() function in SQL allows you to access data from a row that comes before the
current row in the result set.
LAG() function using Rows Between

SELECT salesperson_id, order_number, order_date, amount,


SUM(amount) OVER (ORDER BY order_date ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS
lagged_amount
FROM int_orders;
Lead Function

The LEAD() function in SQL is used to access data from a row that comes after the current
row in the result set.
LEAD() function using Rows Between

SELECT salesperson_id, order_number, order_date, amount,


SUM(amount) OVER (ORDER BY order_date ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS
next_amount
FROM int_orders;
Thank You
Found Useful, Feel Free to Repost

Kanchan Prajapati

You might also like