Window Functions in SQL (Slides)
Window Functions in SQL (Slides)
| Let’s have a closer look at some specific examples of window functions, and how to use them.
2
Window functions in SQL
Data overview
We will use the following table called Employee that contains information about employees in a company located
in South Africa. We assume the database is selected, so we don’t specify it.
| Suppose we want to see the minimum salary in each department. We can use MIN() as a
window function and return the minimum value to each row.
SELECT
Department,
First_name,
Salary, Using ORDER BY here
MIN(Salary) OVER ( sorts the result set by
Query department. This has no
PARTITION BY Department) AS Min_salary
effect on how the
FROM
window function works.
Employee
ORDER BY
Department;
4
Window functions in SQL
Finance Joshua 35760 35760 The minimum value resets for Finance and Marketing
since each department is a different partition/window.
Finance Lily 35760 35760
5
Window functions in SQL
|
Suppose we want to see the total salary budget per department as a running total. Using
SUM() as a window function we can add up Salary in each partition separately. Adding
ORDER BY will give a running total in each row.
SELECT
Department,
First_name,
Salary, Adding ORDER BY here
SUM(Salary) OVER ( enables the running sum
Query calculation by date. All
PARTITION BY Department
aggregate functions can
ORDER BY Date_Started) AS Dept_salary_budget be modified like this.
FROM
Employee
ORDER BY
Department;
6
Window functions in SQL
2014-05-01 Finance Joshua 35760 84260 02. The total salary budget in 2012 was R48500,
01.
which was only for Martha.
2015-06-01 Finance Lily 35760 120020 03.
2017-06-15 Data_analytics Emily 37800 37800 04. In 2014 we hired Joshua and the total salaries
02.
increased to R84260.
2019-05-01 Data_analytics Alex 36200 74000
2022-07-15 Marketing Sophia 36900 98400 Note that the sum resets for each department and
04.
calculates a new running total.
7
Window functions in SQL
|
Ranking window functions assign a rank or row number to each row within a specified window
or subset of rows. Ranking window functions typically need an ORDER BY clause in order to
work as intended.
Ranks rows in each Ranks rows in each Assigns a unique Divides sorted partitions
specified partition. specified partition. number to each row into n-number of equal
within each partition, groups. Each row in a
Duplicate rows are Duplicate rows are even if values in the partition is assigned a
assigned the same rank assigned the same rank partitioned column(s) group number 1, 2, 3…
and the next rank is but the ranks are are duplicated.
skipped, taking sequential regardless e.g. if you have 100 rows
duplicates into account. of duplicates. e.g. The sequence is 1, of data, NTILE(4) will
2, 3, ..., regardless of divide the data into
e.g. 1, 2, 2, 4. e.g. 1, 2, 2, 3. duplicates. 4 x 25 row groups.
8
Window functions in SQL
| The RANK() function assigns a rank to each row based on the order specified within the
window. Rows with the same values receive the same rank, and the next rank is skipped.
9
Window functions in SQL
Lily Gauteng 3
Rank 2 is skipped because Maryam
02.
fell under rank 1.
Sophia Gauteng 3
David Western_Cape 6
Rank 4 and 5 are skipped because
03.
there are 5 rows above.
Gabriel Western_Cape 6
Joshua Western_Cape 6
10
Window functions in SQL
| The DENSE_RANK() function operates similarly to the RANK() function except it does not skip
any ranks even if rows have the same values.
11
Window functions in SQL
Maryam Free_State 1
Sophia Gauteng 2
Since there are three unique values
03.
Alex Western_Cape 3 03.
for Province, our rank goes up to 3.
David Western_Cape 3
Use DENSE_RANK() to avoid rank gaps
Gabriel Western_Cape 3 and potential confusion. Use RANK()
when maintaining relative differences
Joshua Western_Cape 3 between ranks is essential.
12
Window functions in SQL
|
The ROW_NUMBER() function assigns a unique sequential number to each row within a partition,
regardless of the column values. It makes sure that no two rows can have the same row number
within a division.
13
Window functions in SQL
Sophia Gauteng 3
Gabriel Western_Cape 3
Each employee has a unique row number in their
Joshua Western_Cape 4 respective departments.
14
Window functions in SQL
| NTILE() divides sorted partitions into n-number of equal groups. Each row in a partition is
assigned a group number.
15
Window functions in SQL
Joshua Western_Cape 2
16
Window functions in SQL
| The LAG(column, n) function allows access of a value within a column from the previous
nth-row relative to the current row.
17
Window functions in SQL
18
Window functions in SQL
| The LEAD(column, n) function allows access of a value within a column from the following
nth-row relative to the current row. It is the counterpart of the LAG() function.
19
Window functions in SQL
Data_analytics Maryam 46200 36200 01. Values from the Next_salary column
are the Salary values from the next
01.
Data_analytics Alex 36200 37800 row (n =1) which is the reverse of
LAG().
Data_analytics Emily 37800 NULL 03.
02. Finance Joshua 35760 35760 Since we used PARTITION BY, each
02. department is a separate partition. The
Finance Lily 35760 48500 function applies to each separately.
20
Window functions in SQL
| The FIRST_VALUE() function allows the retrieval of the value of a column from the first row
within a partition.
21
Window functions in SQL
2022-03-01 Data_analytics Maryam Emily Only the first value or the first hired
01. employee Emily will be the output in
the Data_analytics partition.
2012-01-01 Finance Martha Martha 02.
22
Window functions in SQL
| The LAST_VALUE() function allows the retrieval of the value of a column from the last row
within a window frame.
23
Window functions in SQL
Emily
2017-06-15 Data_analytics Emily
Alex
2019-05-01 Data_analytics Alex
Maryam
2022-03-01 Data_analytics Maryam
Lily
2015-06-01 Finance Lily
David
2016-01-01 Marketing David
Gabriel
2020-08-01 Marketing Gabriel
Sophia
2022-07-15 Marketing Sophia
24