SQL+&+Databases +Window+Functions
SQL+&+Databases +Window+Functions
with examples
What is a Window Function?
A Window Function is used to perform a calculation on an aggregate value based on a set of rows and return multiple rows for each group.
Revenue
state city client_id sales
California Los Angeles 1001 10000
California Los Angeles 1002 10000
California San Diego 1003 30000
California San Diego 1004 40000
Texas Houston 1005 60000
Texas Houston 1006 50000
Texas Austin 1007 60000
Texas Austin 1008 50000
Let's first partition the data by states and count the number of rows in each partitio
state no_of_clients
California 4
California 4
California 4
California 4
Texas 4
Texas 4
Texas 4
Texas 4
Now, let's partition the data by cities and count the number of rows in each partitio
city no_of_clients
Los Angeles 2
Los Angeles 2
San Diego 2
San Diego 2
Houston 2
Houston 2
Austin 2
Austin 2
Revenue
state city client_id sales
California Los Angeles 1001 10000
California Los Angeles 1002 10000
California San Diego 1003 30000
California San Diego 1004 40000
Texas Houston 1005 60000
Texas Houston 1006 50000
Texas Austin 1007 60000
Texas Austin 1008 50000
Let's first partition the data by states and find the sum of sales in each partition
state state_sales
California 90000
California 90000
California 90000
California 90000
Texas 220000
Texas 220000
Texas 220000
Texas 220000
Now, let's partition the data by cities and find the sum of sales in each partition
city city_sales
Los Angeles 20000
Los Angeles 20000
San Diego 70000
San Diego 70000
Houston 110000
Houston 110000
Austin 110000
Austin 110000
Revenue
state city client_id sales
California Los Angeles 1001 10000
California Los Angeles 1002 10000
California San Diego 1003 30000
California San Diego 1004 40000
Texas Houston 1005 60000
Texas Houston 1006 50000
Texas Austin 1007 60000
Texas Austin 1008 50000
Let's first partition the data by states and find the average amount of sales in each
state avg_state
California 22500
California 22500
California 22500
California 22500
Texas 55000
Texas 55000
Texas 55000
Texas 55000
Now, let's partition the data by cities and find the average amount of sales in each
city avg_city
Los Angeles 10000
Los Angeles 10000
San Diego 35000
San Diego 35000
Houston 55000
Houston 55000
Austin 55000
Austin 55000
Revenue
state city client_id sales
California Los Angeles 1001 10000
California Los Angeles 1002 10000
California San Diego 1003 30000
California San Diego 1004 40000
Texas Houston 1005 60000
Texas Houston 1006 50000
Texas Austin 1007 60000
Texas Austin 1008 50000
Let's first partition the data by states and assign row numbers to each row in the p
state row_no
California 1
California 2
California 3
California 4
Texas 1
Texas 2
Texas 3
Texas 4
Revenue
state city client_id sales
California Los Angeles 1001 10000
California Los Angeles 1002 10000
California San Diego 1003 30000
California San Diego 1004 40000
Texas Houston 1005 60000
Texas Houston 1006 50000
Texas Austin 1007 60000
Texas Austin 1008 50000
Let's rank the data by partitioning the states and sorting sales in ascending order
Revenue
state city client_id sales
California Los Angeles 1001 10000
California Los Angeles 1002 10000
California San Diego 1003 30000
California San Diego 1004 40000
Texas Houston 1005 60000
Texas Houston 1006 50000
Texas Austin 1007 60000
Texas Austin 1008 50000
Let's rank the data by partitioning the states and sorting sales in ascending order
NOTE:
) as dense_rnk
Unlike the RANK() function, the
DENSE_RANK() function returns
consecutive rank values.
8 Write a query to bucket the data into 3 buckets based on the range of sales
Revenue
state city client_id sales
California Los Angeles 1001 10000
California Los Angeles 1002 10000
California San Diego 1003 30000
California San Diego 1004 40000
Texas Houston 1005 60000
Texas Houston 1006 50000
Texas Austin 1007 60000
Texas Austin 1008 50000
Let's first order the data by sales and then group them into three groups based on
Revenue
state month sales
California 4 30000
California 1 10000
California 3 40000
California 2 10000
Texas 1 50000
Texas 4 60000
Texas 3 60000
Texas 2 50000
Let's first partition the states and then sort the data by month
Revenue
state month sales
California 4 30000
California 1 10000
California 3 40000
California 2 10000
Texas 1 50000
Texas 4 60000
Texas 3 60000
Texas 2 50000
Let's first partition the states and then sort the data by month
Revenue
state month sales
California 4 30000
California 1 10000
California 3 40000
California 2 10000
Texas 1 50000
Texas 4 60000
Texas 3 60000
Texas 2 50000
Let's first partition the states and then sort the data by month
Revenue
state month sales
California 4 30000
California 1 10000
California 3 40000
California 2 10000
Texas 1 50000
Texas 4 60000
Texas 3 60000
Texas 2 50000
Let's first partition the states and then sort the data by month