0% found this document useful (0 votes)
7 views

Lesson_09__Window_Functions_in_SQL

Uploaded by

Sakshi Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lesson_09__Window_Functions_in_SQL

Uploaded by

Sakshi Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 95

Window Functions in SQL

Learning Objectives

By the end of this lesson, you will be able to:

Explain window functions and various clauses

List the aggregate window functions

Classify ranking window functions

Categorize the miscellaneous window functions


Introduction to Window Functions
Window Functions

The window function is like an SQL function that takes input values from a window of one or more
rows of a SELECT statement’s result set.

The window functions perform various operations on a group of rows and provide an aggregated
value for each row with a unique identity.
General Syntax

Syntax
window_function_name(expression)OVER([partition_definition]
[order_definition] [frame_definition] )

• Window function specifies the window function name


with an expression.

• OVER clause can comprise partition definition, order definition, and


frame definition.
Types of Clauses

Partition
Clause

Clauses
Frame Order By
Clause Clause
Partition Clause

The partition clause is used to divide or split the rows into partitions, and
the partition boundary is used to split two partitions.

Syntax
PARTITION BY
<expression>[{,<expression>...}]
Order By Clause

Order By clause is an arrangement of rows inside a partition. It performs


partitions using multiple keys where each key has an express

Syntax
ORDER BY <expression> [ASC|DESC],
[{,<expression>...}]
Frame Clause

Frame clause is defined as subset of the current position. It allows to move the subset
within a partition based on the position of the current row in its partition.

Syntax

frame_unit

{<frame_start>|<frame_between>}

The frame unit can be a row or range that specifies the kind of relationship between
the current row and frame row.
Frame Clause

Keywords Meaning

Frame unit Rows: It assigns row number for offset of current and frame row.

Frame unit Range: It assigns row values for offset of current and frame row.

Frame start Frame start specifies the frame boundary.

Frame between Frame between specifies the frame boundary.


Use Case for Window Functions

Problem Scenario:

The HR of a company wants to calculate the performance of employees department-wise based


on the employee ratings.

Objective:

You are required to retrieve the employee ID, first name, role, department, and employee rating by
calculating the maximum employee rating using PARTITION BY and MAX function on department
and employee rating fields respectively.

Instructions:

Refer to the employee dataset given in the course resource section in LMS and create an employee
table using fields mentioned in dataset. Insert the values accordingly to perform the above
objectives.
Use Case for Window Functions
Use Case for Window Functions

Field Name Description

EXP Experience of the employee

COUNTRY Country where the employee lives

CONTINENT Continent based on the country

SALARY Salary of the employee per month in dollars

Rating for the employee (1: Not achieved any goals, 2: Below expectation,
EMP_RATING 3: Meeting expectation, 4: Excellent performance,
5: Overachiever)

MANAGER_ID Employee ID for the manager


Use Case for Window Functions

Solution:

/* SELECT EMP_ID, FIRST_NAME,ROLE,DEPT,EMP_RATING and calculate the maximum EMP_RATING


in a department from the employee table using partition clause on department, Max
function. */

SELECT EMP_ID,FIRST_NAME,ROLE,DEPT,EMP_RATING,MAX(EMP_RATING)OVER(PARTITION BY DEPT) AS


MAX_EMP_RATING FROM emp_table;

By executing this query, the HR can identify the maximum rating of the
employee in a department.
Use Case for Window Functions

Output:
Aggregate Window Functions
Aggregate Window Functions

The aggregate window functions perform on a particular set of rows and


provide the result in a single row.

Syntax
window_function ( [ ALL ] expression )
OVER ( [ PARTITION BY expr_list ] [
ORDER BY order_list frame_clause ] )
Arguments in Aggregate Window Functions

Keywords Meaning

Window function It can be any aggregate window function.

ALL ALL helps to maintain all duplicate values from the expression.

OVER It distinguishes window aggregation from general aggregation functions.

PARTITION BY PARTITION BY provides a window if there are one or more expressions.

ORDER BY ORDER BY is used to sort the rows within each partition.


Types of Aggregate Window Functions

AVG()

01 MIN()
02

COUNT() 05
03
0 MAX()

4 04

SUM()
Use Case for MIN and MAX

Problem Scenario:

The HR of a company wants to identify the minimum and the maximum salary of the employees in
a role.

Objective:

You are required to display the employee’s ID, first name, role, and salary by finding the minimum
and maximum salary of the employees using PARTITION BY clause, MIN, and MAX functions on
role and salary fields respectively.

Instructions:

Refer to the employee table which is created and perform the above objectives.
Use Case for MIN and MAX

Solution:

/* SELECT EMP_ID, FIRST_NAME, ROLE, SALARY and calculate minimum, maximum salary of the
employees using PARTITION CLAUSE on the role field, MIN , MAX function. */

SELECT EMP_ID,FIRST_NAME,ROLE,SALARY, MAX(SALARY) OVER (PARTITION BY ROLE)


MAX_SALARY,MIN(SALARY) OVER (PARTITION BY ROLE) MIN_SALARY FROM emp_table

By executing this query, the HR can identify the maximum and the
minimum salary for a given role.
Use Case for MIN and MAX

Output:
Use Case for AVG and COUNT

Problem Scenario:

The HR of a company wants to identify the average performance of the employee's department-
wise and also find the total number of records in a department.

Objective:

You are required to display the employee’s ID, first name, department, and employee rating by
calculating the average employee rating and the total number of records in a department using
PARTITION BY clause, AVG, and COUNT functions on department and employee rating fields
respectively.

Instructions:

Refer to the employee table which is created and perform the above objectives.
Use Case for AVG and COUNT

Solution :

/* SELECT EMP_ID, FIRST_NAME,DEPT,EMP_RATING and calculate the average employee rating


and total no of records in a department using AVG,COUNT function. */

SELECT EMP_ID,FIRST_NAME,DEPT,EMP_RATING, AVG(EMP_RATING)OVER(PARTITION BY DEPT)


AVG_EMP_RATING_IN_DEPT, COUNT(*) OVER(PARTITION BY DEPT) NO_OF_RECORDS_IN_DEPT FROM
emp_table

By executing this query, the HR can identify the average performance


of the department and the total number of records in a department.
Use Case for AVG and COUNT

Output:
Use Case for SUM

Problem Scenario:

The HR of a company wants to calculate the total employee rating in a department.

Objective:

You are required to display the employee’s Id, first name, department, and employee rating by
calculating the total employee rating in a department using PARTITION BY clause and SUM
function on the department and the employee rating fields respectively.

Instructions:

Refer to the employee table which is created and perform the above objectives.
Use Case for SUM

Solution:

/* SELECT EMP_ID, FIRST_NAME, DEPT,EMP RATING and calculate the total employee rating in
a department using PARTITION CLAUSE on a dept and SUM function. */

SELECT EMP_ID,FIRST_NAME,DEPT,EMP_RATING, SUM(EMP_RATING) OVER (PARTITION BY DEPT)


TOTAL_EMP_RATING_IN_DEPT FROM emp_table

By executing this query, the HR can identify the total employee


rating in a department .
Use Case for SUM

Output:
Assisted Practice: Aggregate Window Functions

Duration: 20 min

Problem Statement: You are required to calculate the total, average, maximum, and minimum salary
of the employee by grouping the departments from the employee table.
Assisted Practice: Aggregate Window Functions

Steps to be performed:
Step 1: Creating the employee table and inserting values in it:

CREATE
CREATE TABLE lep_7.employee ( emp_id int NOT NULL, f_name varchar(45) NULL,
l_name varchar(45) NOT NULL, job_id varchar(45) NOT NULL, salary
decimal(8,2) NOT NULL, manager_id int NOT NULL, dept_id varchar(45) NOT
NULL, PRIMARY KEY(emp_id));

INSERT
INSERT INTO lep_7. employee
(emp_id,f_name,l_name,job_id,salary,manager_id,dept_id) VALUES
('103','krishna','gee','125','500000','05','44');
Assisted Practice: Aggregate Window Functions

Step 2: Querying to perform aggregate window functions:

QUERY

SELECT dept_id,salary,SUM(salary) OVER (PARTITION BY dept_id) salary_total,


AVG(salary) OVER(PARTITION BY dept_id) score_avg , MAX(salary) OVER (PARTITION
BY dept_id) max_salary , MIN(salary) OVER (PARTITION BY dept_id) min_salary
FROM employee
Assisted Practice: Aggregate Window Functions

Output:
Ranking Window Functions
Ranking Window Functions and Its Types

Ranking window functions specify the rank for individual fields as per the
categorization.

Dense rank Rank

Ranking
window
functions

Row number Percent rank


Dense Rank

Definition Syntax

DENSE_RANK() OVER (
• It assigns a rank to every row in a
partition based on the ORDER BY PARTITION BY
clause. <expression>[{,<expression>...}]

ORDER BY <expression>
• It assigns the same rank for equal [ASC|DESC], [{,<expression>...}])
values.

• It has no gaps if two or more rows


have a similar rank.
Rank

Definition Syntax

RANK() OVER (
• Rank helps to assign a rank to all PARTITION BY
rows within every partition. <expr1>[{,<expr2>...}]
ORDER BY <expr1>
[ASC|DESC], [{,<expr2>...}]
• The first row of the rank will be 1. )

• Same rank for the same value.

• There will be a gap if two or more


rows have the same rank.
Use Case for Rank and Dense Rank

Problem Scenario:

The HR of a company wants to assign a rank for each employee based on their employee rating.

Objective:

You are required to display the employee’s ID, first name, department, and employee rating by
assigning a rank to all the employees based on their employee rating using ORDER BY clause,
RANK, and DENSE RANK functions on the employee rating field.

Instructions:

Refer to the employee table which is created and perform the above objectives.
Use Case for Rank and Dense Rank

Solution:

/* SELECT EMP_ID, FIRST_NAME, DEPT,EMP RATING and assign a rank to all the employee
based on their employee rating using Rank and Dense Rank. */

SELECT EMP_ID,FIRST_NAME,DEPT,EMP_RATING, RANK() OVER (ORDER BY EMP_RATING)


EMP_RATING_RANK, DENSE_RANK() OVER (ORDER BY EMP_RATING) EMP_RATING_DENSE_RANK FROM
emp_table;

By executing this query, the HR can identify the rank of the


employees based on their employee rating.
Use Case for Rank and Dense Rank

Output:
Row Number

Definition Syntax

• Row number retrieves the ROW_NUMBER() OVER


(<partition_definition>
unique sequential number of <order_definition>)
each row for the specified
data.
• Similar values will have
different ranks.
Use Case for Row Number

Problem Scenario:

The IT department of a company wants to assign an asset number for each employee based on
their employee ID in ascending order.

Objective:

You are required to display the employee’s ID, first name, role, and department by assigning a
number to each employee in ascending order of their employee ID using ORDER BY clause and
ROW NUMBER function on the employee ID field.

Instructions:

Refer to the employee table which is created and perform the above objective.
Use Case for Row Number

Solution:

/* SELECT EMP_ID, FIRST_NAME, ROLE, DEPT and assign assetnumber to all the employee in
ascending order of their employee ID. */

SELECT EMP_ID,FIRST_NAME,ROLE,DEPT,ROW_NUMBER() OVER(ORDER BY EMP_ID)


EMP_ID_ASC_ROWNUMBER FROM emp_table;

By executing this query, the IT department can identify an asset number


assigned to each employee in ascending order of their employee ID.
Use Case for Row Number

Output:
Percent Rank

Definition Syntax

• Percent rank helps to PERCENT_RANK()OVER (


PARTITION BY expr,...
evaluate the percentile rank ORDER BY expr
[ASC|DESC],...)
of a value in a partition or
result set.

• It returns a value between


zero to one.
Use Case for Percent Rank

Problem Scenario:

The HR of a company wants to calculate the overall percentile of the employee rating in a
department.

Objective:

You are required to display employee’s ID, first name, role, department, and employee rating by
calculating the percentile of the employee rating in a department using ORDER BY clause and
PERCENT RANK function on an employee rating field.

Instructions:

Refer to the employee table which is created and perform the above objective.
Use Case for Percent Rank

Solution:

/* SELECT EMP_ID,FIRST_NAME,ROLE,DEPT,EMP_RATING and calculate the percentile of the


employee rating using ORDERBY and PERCENT RANK function. */

SELECT EMP_ID,FIRST_NAME,ROLE,DEPT,EMP_RATING,PERCENT_RANK() OVER(ORDER BY EMP_RATING)


PERCENTILE_EMP_RATING FROM emp_table;

By executing this query, the HR can identify the percentile score of


each employee in their respective department.
Use Case for Percent Rank

Output:
Miscellaneous Window Functions
Types of Miscellaneous Window Functions

FIRST VALUE ()

LAST VALUE () NTILE ()

LEAD () NTH VALUE ()

LAG () CUME DIST ()


First Value Function

Definition Syntax

FIRST_VALUE (expression) OVER


First value function returns the ( [partition_clause]
value of the expression from the [order_clause] [frame_clause]
)
first row of the window frame.
Use Case for First Value Function

Problem Scenario:

The HR department of an organization aims to find the employee ID of the employee with the
highest experience by sorting their experience in descending order.

Objective:

You are required to display the employee ID, first name, and experience, as well as identify the
employee ID of the first employee by sorting the experience in descending order using the
ORDER BY clause and first value function on the experience and employee ID fields respectively.

Instructions:

Refer to the employee table which is created and perform the above objective.
Use Case for First Value Function

Solution:

/* SELECT EMP_ID,FIRST_NAME,EXP and determine the highest experience in the EMP_ID based
on descending order of the experience. */

SELECT EMP_ID, FIRST_NAME, EXP, FIRST_VALUE(EMP_ID) OVER (ORDER BY EXP DESC)


highest_exp_of_emp_id FROM emp_table;

By executing this query, the HR can identify the employee ID with the
highest experience.
Use Case for First Value Function

Output:
Last Value Function

Definition Syntax

LAST_VALUE (expression) OVER


Last value function returns the last ( [partition_clause]
[order_clause] [frame_clause]
value of a specific column in an )
ordered sequence.
Use Case for Last Value Function

Problem Scenario:

The HR of a company wants to determine the last employee ID by sorting the experience in
ascending order.

Objective:

You are required to display the employee’s ID, first name, and experience and determine the last
employee ID by sorting the experience in ascending order using ORDER BY clause and last value
function on the experience and employee ID field respectively.

Instructions:

Refer to the employee table which is created and perform the above objective.
Use Case for Last Value Function

Solution:

/* SELECT EMP_ID,FIRST_NAME,EXP and determine the last value in the EMP_ID based on
ascending order of the experience. */

SELECT EMP_ID,FIRST_NAME,EXP, LAST_VALUE(EMP_ID) OVER (ORDER BY EXP RANGE


BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) LastValue
FROM emp_table

By executing this query, the HR can identify the last value of the
employee ID based on their experience.
Use Case for Last Value Function

Output:
NTH Value Function

Definition Syntax

NTH_VALUE(expression, N)
The NTH value function acquires FROM FIRST
OVER (
a value from the Nth row of an partition_clause
ordered group of rows. order_clause
frame_clause
)
Use Case for NTH Value Function

Problem Scenario:

The HR of a company wants to identify the third-highest experience among employees in the company.

Objective:

You are required to display the employee’s ID, first name, and experience by calculating the third-highest
experience among employees using ORDER BY clause and NTH value function in descending order of
experience field.

Instructions:

Refer to the employee table which is created and perform the following objective.
Use Case for NTH Value Function

Solution:

/* SELECT EMP_ID,FIRST_NAME,EXP and calculate the third highest experience among


employees using order by on experience in descending order and N_TH VALUE function. */

SELECT EMP_ID,FIRST_NAME,EXP, NTH_VALUE(EXP,3) OVER(ORDER BY EXP DESC)


THIRD_HIGHEST_EXPERIENCE FROM emp_table

By executing this query, the HR can identify the employee's third-highest


level of experience.
Use Case for NTH Value Function

Output:
NTILE Function

Definition Syntax
NTILE(n) OVER (
NTILE(n) PARTITION
OVER (
NTILE function breaks the rows PARTITION BY
BY
<expression>[{,<expression>..
into a sorted partition in a <expression>[{,<expression
.}]
certain number of groups. >...}] ORDER BY <expression>
ORDER BY <expression>
[ASC|DESC],
[{,<expression>...}]
[ASC|DESC],
)
[{,<expression>...}]
)

N_TH Value

The nth value function


helps you to acquire a
value from the Nth row of
a ordered group of rows.
Use Case for NTILE Function

Problem Scenario:

The HR of a company wants to sort the employee table in ascending based on their
experience in four partitions.

Objective:

You are required to display all the details by sorting the experience into four partitions in
ascending order using the ORDER BY clause and NTILE function on an experience field.

Instructions:

Refer to the employee table which is created and perform the above objective.
Use Case for NTILE Function

Solution:

/* SELECT all the details in the employee table by sorting in ascending order of the
experience into four partitions using ORDER BY on EXP and NTILE function. */

SELECT * , NTILE(4)OVER(ORDER BY EXP) PARTITION_BY_EXP FROM emp_table

By executing this query, the HR can view the experience in ascending


order into four partitions.
Use Case for NTILE Function

Output:
Cume Dist Function

Definition Syntax

CUME_DIST( ) OVER ( [
The Cume Dist function partition_by_clause ]
calculates the cumulative order_by_clause )
distribution of a number in a
group of values.
Use Case for Cume Dist Function

Problem Scenario:

The HR of a company wants to sort the employee data based on their experience in ascending
order and calculate the cumulative distribution on the employee table.

Objective:

You are required to display the employee’s ID, first name, and experience by calculating the
cumulative distribution of the experience with the help of ROW NUMBER using ORDER BY, ROW
NUMBER, and CUME DIST function on an experience field.

Instructions:

Refer to the employee table which is created and perform the above objective.
Use Case for Cume Dist Function

Solution:

/* SELECT EMP_ID,FIRST_NAME,EXP and calculate the cumulative distribution of the


experience with the help of Row number using ODERBY and ROW_NUMBER, CUME_DIST function.
*/

SELECT EMP_ID,FIRST_NAME,EXP, ROW_NUMBER() OVER (ORDER BY EXP) ROW_NUMBER_EXP,


CUME_DIST() OVER (ORDER BY EXP) CUME_DIST_EXP FROM emp_table;

By executing this query, the HR can identify the cumulative distribution of


the experience with the help of row number.
Use Case for Cume Dist Function

Output:
Lead Function

Definition Syntax

LEAD OVER() ( PARTITION BY


Lead function is used to retrieve (expr) ORDER BY (expr) )

the values from the next N rows.


Lag Function

Definition Syntax

LAG () OVER ( PARTITION BY


• Lag function is used to expr,... ORDER BY expr
[ASC|DESC],... )
retrieve the values from
previous N rows.

• It is the reverse of lead


function.
Use Case for Lead and Lag Function

Problem Scenario:

The HR of a company wants to ignore the two lowest and highest experiences of the employees.

Objective:

You are required to display the employee’s ID, first name, experience and sort the employees in
ascending order of their experience. Ignore the two lowest experiences using LEAD and two
highest experiences using LAG to determine the median of the employee experience.

Instructions:

Refer to the employee table which is created and perform the above objective.
Use Case for Lead and Lag Function

Solution:

/* SELECT EMP_ID,FIRST_NAME, EXP and ignore the two lowest and highest experience using
LEAD and LAG function. */

SELECT EMP_ID,FIRST_NAME,EXP, LEAD(EXP,2) OVER (ORDER BY EXP) LEAD_2_LOWEST_EXP,


LAG(EXP,2) OVER (ORDER BY EXP) LAG_2_HIGHEST_EXPFROM emp_table

By executing this query, the HR can identify the median


experience of the employee.
Use Case for Lead and Lag Function

Output:
Assisted Practice: Ranking and Miscellaneous
Window Functions

Duration:15 min

Problem Statement: You are required to identify the rank and row number and calculate the
cumulative distribution and percentile score based on the student score from the marksheet table.
Assisted Practice: Ranking and Miscellaneous
Window Functions

Steps to be performed:
Step 1: Creating the marksheet table and inserting values in it:

CREATE

CREATE TABLE marksheet ( score INT NOT NULL, year INT NULL, class
varchar(45) NULL, ranking varchar(45) NULL, s_id INT NOT NULL );

INSERT

INSERT INTO marksheet (score,year,class,ranking,s_id) VALUES


('989','2014','10','1','1');
Assisted Practice: Ranking and Miscellaneous
Window Functions

Step 2: Querying to perform window functions:

QUERY

SELECT s_id,score, RANK() OVER (ORDER BY score DESC) my_rank , PERCENT_RANK()


OVER (ORDER BY score DESC) percentile_rank , ROW_NUMBER() OVER (ORDER BY
score) row_num, CUME_DIST() OVER (ORDER BY score) cume_dist_val FROM
marksheet;
Assisted Practice: Ranking and Miscellaneous
Window Functions

Output:
Knowledge Check
Knowledge
Check
What is the result of Window Functions ?
1

A. Aggregate value of each row

B. Group of values

C. Sorted values

D. Divides into partition


Knowledge
Check
What is the result of Window Functions ?
1

A. Aggregate value of each row

B. Group of values

C. Sorted values

D. Divides into partition

The correct answer is A

Window functions perform various operations on a group of rows and provide an aggregated value for
each row.
Knowledge
Check
Which of the following are the clauses of window functions in MySQL ?
2

A. ROW NUMBER and RANK

B. LAST VALUE and FIRST VALUE

C. MINIMUM and MAXIMUM

D. PARTITION, FRAME, and ORDER BY


Knowledge
Check
Which of the following are the clauses of window functions in MySQL ?
2

A. ROW NUMBER and RANK

B. LAST VALUE and FIRST VALUE

C. MINIMUM and MAXIMUM

D. PARTITION, FRAME, and ORDER BY

The correct answer is D

The types of clauses are PARTITION Clause, FRAME Clause, and ORDER BY Clause.
Knowledge
Check Which of the following window functions is used to calculate the cumulative
3 distribution of a column's values?

A. ROW_NUMBER()

B. DENSE_RANK()

C. NTILE()

D. CUME_DIST()
Knowledge
Check Which of the following window functions is used to calculate the cumulative
3 distribution of a column's values?

A. ROW_NUMBER()

B. DENSE_RANK()

C. NTILE()

D. CUME_DIST()

The correct answer is D

The CUME_DIST() function is used to calculate the cumulative distribution of a column's values within
a partition.
Knowledge
Check
Which ranking window function returns a value from zero to one ?
4

A. NTH value

B. Percent rank

C. N title

D. Row number
Knowledge
Check
Which ranking window function returns a value from zero to one ?
4

A. NTH value

B. Percent rank

C. N title

D. Row number

The correct answer is B

Percent Rank returns value from zero to one.


Knowledge
Check
What is the purpose of LEAD Function ?
5

A. Retrieve the values from next N rows

B. Retrieve the values from previous N rows

C. Retrieve unique sequential number

D. Assign rank to all the rows within every partition


Knowledge
Check
What is the purpose of LEAD Function ?
5

A. Retrieve the values from next N rows

B. Retrieve the values from previous N rows

C. Retrieve unique sequential number

D. Assign rank to all the rows within every partition

The correct answer is A

LEAD function retrieves the values from next N rows.


Lesson-End Project: Multi-Brand Gadget-Selling Company

Problem statement:
You are working for a gadget-selling company. Your manager has asked you
to perform an end-to-end analysis of all types of products sold by your
organization, i.e., from creating a table and inserting data to extracting the
useful data points using SQL.

Objective:
To analyze the different products across the available categories based on the
prices
Lesson-End Project: Multi-Brand Gadget-Selling Company

Tasks to be performed:

1. Create the database "SELL_INFO"


2. Create the “product" table with four columns (category, brand, name,
and amount) with the correct data type per the Intuition column in the
"SELL_INFO" database
3. Insert the following data in the “product” table:

category brand name amount


Phone Apple iPhone 12 Pro Max 1300
Phone Apple iPhone 12 Pro 1100
Phone Apple iPhone 12 1000
Phone Samsung Galaxy Z Fold 3 1800
Phone Samsung Galaxy Z Flip 3 1000
Phone Samsung Galaxy Note 20 1200
Phone Samsung Galaxy S21 1000
Lesson-End Project: Multi-Brand Gadget-Selling Company

Phone OnePlus OnePlus Nord 300


Phone OnePlus OnePlus 9 800
Phone Google Pixel 5 600
Laptop Apple MacBook Pro 13 2000
Laptop Apple MacBook Air 1200
Laptop Microsoft Surface Laptop 4 2100
Laptop Dell XPS 13 2000
Laptop Dell XPS 15 2300
Laptop Dell XPS 17 2500
Earphone Apple AirPods Pro 280
Earphone Samsung Galaxy Buds Pro 220
Earphone Samsung Galaxy Buds Live 170
Earphone Sony WF-1000XM4 250
Headphone Sony WH-1000XM4 400
Headphone Apple AirPods Max 550
Headphone Microsoft Surface Headphones 2 250
Smartwatch Apple Apple Watch Series 6 1000
Smartwatch Apple Apple Watch SE 400
Smartwatch Samsung Galaxy Watch 4 600
Smartwatch OnePlus OnePlus Watch 220
Lesson-End Project: Multi-Brand Gadget-Selling Company

Tasks to be performed:

4. Fetch the price of the most and least expensive product under the
Headphone category
5. Rank all the products based on the price with the RANK() as well
DENSE_RANK() functions and return the records where RANK() or
DENSE_RANK() has a value equal to 5 or 6
Note: You can also give alias to the newly generated rank and dense rank
columns.
Lesson-End Project: Multi-Brand Gadget-Selling Company

Tasks to be performed:

6. Identify how much more expensive MacBook Pro 13 is (as a percentage)


when compared to all products
7. Create a column capturing the third-highest priced laptop in the entire data
for the item named "Laptop“ from the “category” column
8. Fetch all products that constitute the first 25% of the data in the Product
table based on the amount
Key Takeaways

Window functions provide an aggregated value for each row with a


unique identity.

Aggregate window functions perform on a particular set of rows


and provide the result in a single row.

Ranking window functions specify the rank for individual fields as


per the categorization.

You might also like