0% found this document useful (0 votes)
19 views50 pages

Lecture 14 - Window Rank Functions

The document covers SQL window rank functions, including Common Table Expressions (CTEs) and their advantages, such as improved readability. It explains various ranking functions like RANK(), DENSE_RANK(), and ROW_NUMBER(), and how to use the OVER() and PARTITION BY clauses to define windows for calculations. Additionally, it includes practical use cases and SQL query examples for ranking orders and analyzing sales data.

Uploaded by

ssinghme22
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)
19 views50 pages

Lecture 14 - Window Rank Functions

The document covers SQL window rank functions, including Common Table Expressions (CTEs) and their advantages, such as improved readability. It explains various ranking functions like RANK(), DENSE_RANK(), and ROW_NUMBER(), and how to use the OVER() and PARTITION BY clauses to define windows for calculations. Additionally, it includes practical use cases and SQL query examples for ranking orders and analyzing sales data.

Uploaded by

ssinghme22
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/ 50

SQL Lecture 13: Window Rank

Functions
RECALL QUESTIONS

Q1
What is a Common Table Expression (CTE) in SQL?

a. A temporary table that exists during the execution of a query.


b. A permanent table in the database.
c. A method to write complex sub-queries.
RECALL QUESTIONS

Q1
What is a Common Table Expression (CTE) in SQL?

a. A temporary table that exists during the execution of a query.


b. A permanent table in the database.
c. A method to write complex sub-queries.
RECALL QUESTIONS

Q2
What is the main advantage of using a CTE?

a. Faster execution speed.


b. Reduced memory usage.
c. Improved readability and maintainability of the code.
RECALL QUESTIONS

Q2
What is the main advantage of using a CTE?

a. Faster execution speed.


b. Reduced memory usage.
c. Improved readability and maintainability of the code.
RECALL QUESTIONS

Q3
Scenario: In your company's database, there are complex queries used frequently
to retrieve employee information from different tables. To simplify the querying
process, you decide to create a virtual table that combines data from various tables.
What database feature would you use?

Question: To simplify querying by creating a virtual table that combines data from
various tables, you would create a:

a. Views
b. CTE (Common Table Expression)
c. Subquery
d. Materialized View
RECALL QUESTIONS

Q3
Scenario: In your company's database, there are complex queries used frequently
to retrieve employee information from different tables. To simplify the querying
process, you decide to create a virtual table that combines data from various tables.
What database feature would you use?

Question: To simplify querying by creating a virtual table that combines data from
various tables, you would create a:

a. Views
b. CTE (Common Table Expression)
c. Subquery
d. Materialized View
RECALL QUESTIONS

Q4
How many times can a CTE be referenced in a single SQL query?

a. Only once.
b. Exactly twice.
c. As many times as needed within the query.
d. CTEs cannot be referenced in a query.
RECALL QUESTIONS

Q4
How many times can a CTE be referenced in a single SQL query?

a. Only once.
b. Exactly twice.
c. As many times as needed within the query.
d. CTEs cannot be referenced in a query.
RECALL QUESTIONS

Q5
When is a View evaluated in SQL?

a. At the time of creation.


b. At the time it is queried.
c. During the database maintenance.
d. Views are never evaluated.
RECALL QUESTIONS

Q5
When is a View evaluated in SQL?

a. At the time of creation.


b. At the time it is queried.
c. During the database maintenance.
d. Views are never evaluated.
SESSION AGENDA:
WINDOW RANK FUNCTIONS

❖ Rank Functions
❖ Introduction to Window Functions
❖ Types of RANK Functions
❖ OVER() clause to define windows
❖ PARTITION BY within OVER()
ECOMMERCE DATABASE

Ecommerce Database
RANK FUNCTIONS?

Finance team - Is it
possible to rank the
orders on the basis of
their order amount ?

Yes, using RANK functions


How do you think we can rank the orders on the basis of order amount?
RANK FUNCTIONS
We have three different types of RANK functions as follows:

● RANK() assigns ranks, but if there's a tie (two or


more participants with the same time), it skips
the next rank. For example, if two people finish
first, the next person is considered third.

● DENSE_RANK() also assigns ranks, but it


doesn't skip ranks in case of a tie. If two people
finish first, the next person is considered
second.

● ROW_NUMBER() doesn't care about how fast


or slow participants are. It simply numbers them
one after the other, starting from the beginning.
RANK FUNCTIONS -> WINDOW FUNCTIONS

Rank functions are window functions. With Window Functions, we can calculate a value for each
row keeping the whole table in context (for example ranking on the basis of order amount)

What exactly is a Window Function?


Window Functions operate on a set of rows related to the current row in a specified order, rather
than the entire result set (different to what’s done in aggregation functions). They let you perform
calculations across a set of table rows that are somehow related to the current row.
● If you want to just rank the orders on the basis of order amount

● If you want to Dense rank the orders on the basis of order amount
● If you want to want to add the row number and arrange them in
descending order of the order amount
But what’s this over() that we
are using everytime while
ranking?

The over() clause is used in window functions to


apply the function over the defined windows in
a certain manner (for example in the decreasing
order of amount).

We will understand how to define the windows


later as we move on the in the lecture, but here
consider that the whole table is a window and
we are applying a function on that window.
Write a query to rank the orders
for each customer separately
on the basis of order amount.

In order to solve this problem, we need to use


partition by clause to limit the context of each
row to the same customer id while ranking the
orders.
Implementation in SQL :

SELECT
o.customer_id,
o.order_id,
o.total_amount,
rank() OVER (partition by customer_id order by total_amount desc) AS
`rank`
FROM
Orders o
OVER() AND PARTITION BY CLAUSE

OVER clause defines the window or set of rows, over which a function operates.
PARTITION BY is used inside the OVER() to form groups of row
OVER() AND PARTITION BY CLAUSE

In simple terms, "OVER" is like looking at the big picture (here the whole table), and
"PARTITION BY" is like zooming in to focus on specific groups within your data.

So, let’s try to use the window function to solve our problem.
Finance team - Is it possible to rank
the orders in each category
separately?

Yes, using RANK functions with


over and partition by clause
This is the ranking of order amount on the basis of category
Implementation in SQL :
Sales Database
Identify the salespeople who
are constantly making good
sales by checking their day
by day progress
What’s
running total?

Breaking the problem statement:


● What's the total sales for each of these
salespeople, day by day?(this is called calculating
the running total)
RUNNING TOTAL

Running count of
something as you go
along a list or a sequence

How can you get the running total of something?


GROUP BY
No!
– Query to calculate total sales and running total using the OVER clause

SELECT
S.SalespersonID,
S.SaleDate,
S.Amount,
SUM(S.Amount) OVER (PARTITION BY S.SalespersonID ORDER BY
S.SaleDate) AS RunningTotal
FROM
Sales S
ORDER BY
S.SalespersonID, S.SaleDate;
SELECT
S.SalespersonID,
SP.Name AS SalespersonName,
S.SaleDate,
S.Amount,
SUM(S.Amount) OVER (PARTITION BY S.SalespersonID
Salesperson's name is ORDER BY S.SaleDate) AS RunningTotal
missing in this. Can you FROM
please include it? Sales S
INNER JOIN
Salespersons SP ON S.SalespersonID = SP.SalespersonID
ORDER BY
S.SalespersonID, S.SaleDate;
CURIOSITY QUESTION
What if we don't specify anything inside the window?
-- Create a universal window that includes every row.
-- Thus the aggregate if calculated will be calculated for the overall data.
-- Lets understand this by looking at this example.

SELECT
S.SalespersonID,
S.SaleDate,
S.Amount,
SUM(S.Amount) OVER () AS
RunningTotal
FROM
Sales S
ORDER BY
S.SalespersonID, S.SaleDate;
BRAIN BUSTERS
BRAIN BUSTERS

Use Case 1: Top Selling Products by Revenue

Write a SQL query using the RANK() window function to determine the top selling products
based on their total revenue. In your query, ensure that each product is assigned a rank based
on its total revenue, and in cases where multiple products have the same revenue, they should
be assigned the same rank.
BRAIN BUSTERS

Answer:

SELECT
p.product_name,
SUM(oi.price * oi.quantity) AS total_revenue,
RANK() OVER (ORDER BY SUM(oi.price * oi.quantity) DESC) AS product_rank
FROM
products p
JOIN
order_items oi ON p.product_id = oi.product_id
GROUP BY
p.product_name
ORDER BY
product_rank;
BRAIN BUSTERS

Use Case 2: Unique Customer Purchase Order Ranking

Write a SQL query using the RANK() window functions to create a ranking of unique purchase
orders based on their order dates. Ensure that there are no gaps in the ranking sequence, even
in cases where multiple orders have the same order date (ties).
BRAIN BUSTERS

Answer:

SELECT
o.order_id,
o.order_date,
c.first_name,
c.last_name,
DENSE_RANK() OVER (ORDER BY o.order_date) AS order_rank
FROM
orders o
JOIN
customers c ON o.customer_id = c.customer_id
ORDER BY
order_rank, o.order_id;
BRAIN BUSTERS
Use Case 3: Order History with Row Numbers

Write a SQL query using the Rank window function to display the order history for each
customer. You need to assign a unique row number to each order for every customer,
based on the sequence of their order dates (i.e., earlier orders should have lower row
numbers).
BRAIN BUSTERS
Answer:

SELECT
c.first_name,
c.last_name,
o.order_id,
o.order_date,
ROW_NUMBER() OVER (PARTITION BY c.customer_id ORDER BY o.order_date) AS order_row_number
FROM
customers c
JOIN
orders o ON c.customer_id = o.customer_id
ORDER BY
c.customer_id, o.order_date;
BRAIN BUSTERS
Use Case 4: Top Selling Products by Revenue

You are tasked with analyzing the frequency of customer orders in the Ecommerce database.
Generate a report that ranks customers based on the number of orders they have placed,
segmented by each year. The report should include the customer's first name, last name, the
year of the orders, the total number of orders they placed that year, and their rank in order
frequency for that year.
BRAIN BUSTERS
Answer:
BRAIN BUSTERS
Use Case 5: Top Selling Products by Revenue

Analyze the efficiency of different payment methods used in the Ecommerce database. Create
a report that shows the average amount of money spent per order for each payment method,
along with the total number of transactions and the average order amount per method. The
report should also include the maximum transaction amount for each method. Rank the
payment methods based on the average order amount.
BRAIN BUSTERS
Answer:
Ensure to go through more similar window functions in this post read: LINK
SESSION SUMMARY:
WINDOW RANK FUNCTIONS

❖ Rank Functions
❖ Introduction to Window Functions
❖ Types of RANK Functions
❖ OVER() clause to define windows
❖ PARTITION BY within OVER()

You might also like