Window Functions 1721585676
Window Functions 1721585676
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.
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.
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.
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
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
Kanchan Prajapati