SQL > Window Functions

What is a window function in SQL?

With aggregate functions, you usually get a single result for each group that you look at (as specified by the columns in the GROUP BY clause). However, there are times when you want to apply the aggregate function to each row, and this is where window functions come in.

Window functions are called such because they operate on each row within a window. A window is defined by the OVER() clause. There are two major components in the OVER() construct:

  • PARTITION BY: Defines the window partition into groups of rows. This is similar to the GROUP BY clause. Note that this is optional. You do not need to specify PARTITION BY if your window covers the entire data set.
  • ORDER BY: Orders the value within each window partition. This is optional, although in most of the use cases, it makes sense to order your results in some way.

There are a number of aggregate functions that can act as a window function. The following are the common aggregate functions used as part of a window function:

We go through each type of functions below:

Common Aggregate Functions: SUM, AVG, COUNT

We use the following table for our examples.

Table Store_Sales

Store_IDSalespersonSales
1Aaron374
1Beatrice492
1Cathy430
2Dan462
2Elmo747
2Frank1332
2Gina898
2Harry603
3Isabel247
3Jimmy1030
3Kara1030
3Lamar1314
3Mary462

To list the average sales amount of the store each salesperson belongs to, we use the following window function:

SELECT Store_ID, Salesperson, Sales, AVG(Sales) OVER (PARTITION BY Store_ID) Avg_Store
FROM Store_Sales;

Result:

Store_IDSalespersonSalesAvg_Store
1Aaron374432
1Beatrice492432
1Cathy430432
2Dan462808.4
2Elmo747808.4
2Frank1332808.4
2Gina898808.4
2Harry603808.4
3Isabel247816.6
3Jimmy1030816.6
3Kara1030816.6
3Lamar1314816.6
3Mary462816.6

Ranking Functions: RANK, DENSE_RANK, ROW_NUMBER

ROW_NUMBER(), RANK(), and DENSE_RANK() are all functions that show the rank of a row within the window. These three functions are different in how they handle ties:

RANK(): When there are ties, this function will consider the number of rows that are tied and assign the rank to the subsequent rows. For example, if two rows are tied as rank 1, the next row will get rank 3.

DENSE_RANK(): When there are ties, this function will simply assign the next rank to the next row after the tie. For example, if two rows are tied as rank 1, the next row will get rank 2.

ROW_NUMBER(): Does not consider ties. This simply returns the row number of the result set within the window.

When there is no tie among the values, these three functions all return the same result set.

Here we use the same Store_Sales table as above. To list the rank of each salesperson by sales amount within each store, we use the following window function:

SELECT Store_ID, Salesperson, Sales, RANK() OVER (PARTITION BY Store_ID ORDER BY Sales DESC) Sales_Rank
FROM Store_Sales
ORDER BY Store_ID, Sales_Rank;

Result:

Store_IDSalespersonSalesSales_Rank
1Beatrice4921
1Cathy4302
1Aaron3743
2Frank13321
2Gina8982
2Elmo7473
2Harry6034
2Dan4624
3Lamar13141
3Jimmy10302
3Kara10302
3Mary4624
3Isabel2475

If we replace RANK() with DENSE_RANK() above, we get the following:

SELECT Store_ID, Salesperson, Sales, DENSE_RANK() OVER (PARTITION BY Store_ID ORDER BY Sales DESC) Sales_Dense_Rank
FROM Store_Sales
ORDER BY Store_ID, Sales_Rank;

Result:

Store_IDSalespersonSalesSales_Dense_Rank
1Beatrice4921
1Cathy4302
1Aaron3743
2Frank13321
2Gina8982
2Elmo7473
2Harry6034
2Dan4624
3Lamar13141
3Jimmy10302
3Kara10302
3Mary4623
3Isabel2474

If we replace RANK() with ROW_NUMBER() above, we get the following:

SELECT Store_ID, Salesperson, Sales, ROW_NUMBER() OVER (PARTITION BY Store_ID ORDER BY Sales DESC) Sales_Row_Number
FROM Store_Sales
ORDER BY Store_ID, Sales_Rank;

Result:

Store_IDSalespersonSalesSales_Row_Number
1Beatrice4921
1Cathy4302
1Aaron3743
2Frank13321
2Gina8982
2Elmo7473
2Harry6034
2Dan4624
3Lamar13141
3Jimmy10302
3Kara10303
3Mary4624
3Isabel2475

Notice the different values for Store_ID = 3. For this store, two salespeople are tied for the second most sales. Under RANK(), the five values are 1,2,2,4,5. Under DENSE_RANK(), the five values are 1,2,2,3,4. Under ROW_NUMBER(), the five values are 1,2,3,4,5.

Grouping Function: NTILE

The NTILE() function divides all rows in a window into a pre-defined number of groups of approximately the same size. The first group is assigned 1, the second group is assigned 2, etc.

For example, if there are 100 rows in a window and we use NTILE(5), then each NTILE value would correspond to 20 rows.

If the number of rows is not divisible by the number of groups, then the size of each group can vary by 1, with the earlier groups having the larger count.

For example, if there are 99 rows in a window and we use NTILE(5), then the first 4 groups will have 20 rows, while the last group will have 19 rows.

Let's look at an example. If we want to divide all salesperson in our example into four groups based on the amount of sales each generates regardless of which store the salesperson belongs to, we would use the following query:

SELECT Store_ID, Salesperson, Sales, NTILE(4) OVER (ORDER BY Sales DESC) Sales_Group
FROM Store_Sales
ORDER BY Sales DESC;

Result:

Store_IDSalespersonSalesSales_Group
2Frank13321
3Lamar13141
3Jimmy10301
3Kara10301
2Gina8982
2Harry6032
2Elmo7472
1Beatrice4923
2Dan4623
3Mary4623
1Cathy4304
1Aaron3744
3Isabel2474

Here we have a total of 13 rows and we are trying to group them into four groups. So Group 1 gets 4 rows, and Groups 2-4 each get 3 rows.

Before- and After-Functions: LEAD, LAG

There are instances where you are interested in doing a calculation based on the row(s) prior to the particular row you are interested in or based on the row(s) after the particular row you are interested in. In these cases, the LEAD() and LAG() functions can be used.

The LAG() function takes the value of the row(s) above the current row. The syntax of the LAG() function is as follows:

LAG(expression [, interval [, default_value]]) OVER ()

The interval argument is optional, and it specifies the number of rows to skip. If this is not specified, then it is set to 1. The default_value argument is only used when interval is specifies, and you can set the default value to be returned if the function generates NULL.

The LEAD() function takes the value of the row(s) below the current row. The syntax of the LEAD() function is as follows:

LAG(expression [, interval [, default_value]]) OVER ()

The interval argument is optional, and it specifies the number of rows to skip. If this is not specified, then it is set to 1. The default_value argument is only used when interval is specifies, and you can set the default value to be returned if the function generates NULL.

Let's look at an example with the following query:

SELECT Store_ID, Salesperson, Sales, LEAD(Sales) OVER (PARTITION BY Store_ID ORDER BY Sales DESC) Sales_Lead, LAG(Sales) OVER (PARTITION BY Store_ID ORDER BY Sales DESC) Sales_Lag
FROM Store_Sales
ORDER BY Store_ID, Sales DESC;

Result:

Store_IDSalespersonSalesSales_LeadSales_Lag
1Beatrice492430NULL
1Cathy430374492
1Aaron374NULL430
2Frank1332898NULL
2Gina8987471332
2Elmo747603898
2Harry603462747
2Dan462NULL603
3Lamar13141030NULL
3Jimmy103010301314
3Kara10304621030
3Mary4622471030
3Isabel247NULL462

Let's look at the data for Cathy. In the Sales_Lead column, the amount is 374, which corresponds to the Sales amount of Aaron, who ranks right below Cathy. In the Sales_Lag column, the amount is 492, which corresponds to the Sales amount of Beatrice, who ranks right above Cathy.

The above query itself may not look very useful. However, the usefulness becomes more evident if we are trying to see how much difference there is between each person and the person who ranks right above. In this case, we would use the following query:

SELECT Store_ID, Salesperson, Sales, LAG(Sales,1,Sales) OVER (PARTITION BY Store_ID ORDER BY Sales DESC) - Sales Sales_Difference FROM Store_Sales
ORDER BY Store_ID, Sales DESC;

Result:

Store_IDSalespersonSalesSales_Difference
1Beatrice4920
1Cathy43062
1Aaron37456
2Frank13320
2Gina89834
2Elmo747151
2Harry603144
2Dan462141
3Lamar13140
3Jimmy1030284
3Kara10300
3Mary462568
3Isabel247215

Next: SQL DECODE

Udemy Course: SQL for Beginners: Learn SQL using MySQL and Database Design icon




Copyright © 2021   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact