0% found this document useful (0 votes)
12 views17 pages

Data and AI SQL Window Functions

The document provides an overview of various SQL window functions, including NTH_VALUE, CUME_DIST, PERCENT_RANK, and others, explaining their purpose and providing examples related to employee salaries. Each function is designed to perform specific calculations or retrieve data from a result set, such as ranking salaries or calculating percentiles. The document serves as a guide for understanding and utilizing these SQL functions effectively.

Uploaded by

hanumeshwr
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)
12 views17 pages

Data and AI SQL Window Functions

The document provides an overview of various SQL window functions, including NTH_VALUE, CUME_DIST, PERCENT_RANK, and others, explaining their purpose and providing examples related to employee salaries. Each function is designed to perform specific calculations or retrieve data from a result set, such as ranking salaries or calculating percentiles. The document serves as a guide for understanding and utilizing these SQL functions effectively.

Uploaded by

hanumeshwr
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/ 17

DATA AND AI

SQL
Window Functions
Shwetank Singh
GritSetGrow - GSGLearn.com

gsglearn.com
Data and AI

NTH_VALUE
Retrieves the n-th value in an ordered
partition of a result set. Useful for accessing
specific rows within a window frame.

Retrieves the third highest salary for each


employee in the dataset.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

CUME_DIST
Calculates the cumulative distribution of a
value within a group of values. It returns the
relative position of a value in a group.

Shows the proportion of employees earning


less than or equal to each employee's salary.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

PERCENT_RANK
Computes the relative rank of a row within a
partition as a percentage, ranging from 0 to
1.

Assigns a percentile rank to each employee


based on their salary compared to others.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

PERCENTILE_CONT
A continuous percentile function that
calculates a specific percentile value within a
group, interpolating between values if
necessary.

Computes the median salary for each


department by interpolating between
salaries if needed.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

PERCENTILE_DISC
A discrete percentile function that returns the
first value in the ordered set of values that is
greater than or equal to the specified percentile.

Finds the 90th percentile salary within each


department, returning the smallest salary
that is at least the 90th percentile.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

FIRST_VALUE
Returns the first value in an ordered partition
of a result set.

Retrieves the salary of the first hired


employee in each department.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

LAST_VALUE
Returns the last value in an ordered partition
of a result set.

Retrieves the salary of the most recently


hired employee in each department.
Note: Requires specifying the frame to access
the last value correctly.
Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

NTILE
Distributes the rows in an ordered partition into a
specified number of approximately equal groups,
assigning a bucket number to each row.

Divides employees into four quartiles based


on their salaries, assigning each employee to
a quartile from 1 to 4.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

LAG
Accesses data from a previous row in the
same result set without the use of a self-join.

Shows the salary of the employee who was


hired immediately before each employee.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

LEAD
Accesses data from a subsequent row in the
same result set without the use of a self-join.

Shows the salary of the employee who will be


hired immediately after each employee.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

ROW_NUMBER
Assigns a unique sequential integer to rows
within a partition of a result set, starting at 1
for the first row in each partition.

Numbers employees within each department


based on their salary, with the highest-paid
employee being row number 1.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

RANK
Assigns a rank to each row within a partition
of a result set, with gaps in rank values when
there are ties.

Ranks employees within each department by


salary. Employees with the same salary receive
the same rank, and subsequent ranks are
skipped accordingly.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

DENSE_RANK
Similar to RANK(), but without gaps in rank
values when there are ties.

Ranks employees within each department by


salary without skipping ranks for tied
salaries.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

SUM
Calculates the cumulative sum of a numeric
column over a specified window frame.

Computes the running total of salaries


ordered by hire date.

Shwetank Singh
GritSetGrow - GSGLearn.com
Data and AI

AVG
Calculates the average of a numeric column
over a specified window frame.

Determines the average salary within each


department for every employee.

Shwetank Singh
GritSetGrow - GSGLearn.com

You might also like