Lecture 14 - Window Rank Functions
Lecture 14 - Window Rank Functions
Functions
RECALL QUESTIONS
Q1
What is a Common Table Expression (CTE) in SQL?
Q1
What is a Common Table Expression (CTE) in SQL?
Q2
What is the main advantage of using a CTE?
Q2
What is the main advantage of using a CTE?
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?
Q5
When is a View evaluated in SQL?
❖ 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 ?
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)
● 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?
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?
Running count of
something as you go
along a list or a sequence
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
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
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()