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

Window Functions

Window functions allow calculations across rows related to the current row within a result set. They operate on a "window" of rows defined by an OVER clause, which can be partitioned and ordered. Common window functions include RANK(), DENSE_RANK(), PERCENT_RANK(), CUME_DIST(), and ROW_NUMBER(), which assign ranks, percentages, distributions, or unique numbers to rows within a window.

Uploaded by

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

Window Functions

Window functions allow calculations across rows related to the current row within a result set. They operate on a "window" of rows defined by an OVER clause, which can be partitioned and ordered. Common window functions include RANK(), DENSE_RANK(), PERCENT_RANK(), CUME_DIST(), and ROW_NUMBER(), which assign ranks, percentages, distributions, or unique numbers to rows within a window.

Uploaded by

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

Window Functions

Window functions are a category of SQL functions that perform a calculation across a
specified range of rows related to the current row within a result set. These functions
operate on a "window" of rows defined by an OVER clause, which allows you to
segment the result set and apply the function to specific subsets of data.

PARTITION BY Clause:

Divides the result set into partitions to which the window function is applied separately.
Each partition is treated as an independent group, and the window function is calculated
for each partition.

ORDER BY Clause:

Specifies the order in which the rows are processed within each partition. The window
function is applied to the rows in the defined order.

Window Frame:

Determines the range of rows used for each calculation. It is specified using clauses like
ROWS BETWEEN, RANGE BETWEEN, or the default option, which is typically
equivalent to "UNBOUNDED PRECEDING AND CURRENT ROW."

1. RANK()

The RANK() window function assigns a rank to each row based on the specified
ordering within the window. Rows with equal values receive the same rank, and the next
rank is then skipped.

Examples:

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
SALARY,
RANK() OVER (ORDER BY SALARY DESC) AS salary_rank
FROM
oehr_employees;
This query assigns a rank to each employee based on their salary in descending
order.

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
SALARY,
RANK() OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY DESC) AS
dept_salary_rank
FROM
oehr_employees;

This query assigns a rank to each employee based on their salary within each
department, in descending order.

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
SALARY,
RANK() OVER (ORDER BY SALARY DESC) AS overall_salary_rank,
RANK() OVER (PARTITION BY JOB_ID ORDER BY SALARY DESC) AS
job_salary_rank
FROM
oehr_employees
WHERE
SALARY IS NOT NULL;

This query includes an overall salary rank and a rank within each job category, filtering
out rows with NULL salaries.

2. DENSE_RANK()
The DENSE_RANK() window function is similar to RANK(), but it does not leave gaps
between rank values for tied rows. It assigns consecutive rank values without skipping
any.

Examples:

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
SALARY,
DENSE_RANK() OVER (ORDER BY SALARY DESC) AS dense_salary_rank
FROM
oehr_employees;

This query assigns dense ranks to each employee based on their salary in
descending order.

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
SALARY,
DENSE_RANK() OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY
DESC) AS dept_dense_salary_rank
FROM
oehr_employees;

This query assigns dense ranks to each employee based on their salary within each
department, in descending order.

3. PERCENT_RANK()

The PERCENT_RANK() window function calculates the relative rank of each row within
the window as a percentage. It considers the position of a row relative to the total
number of rows in the partition.

Examples:

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
SALARY,
PERCENT_RANK() OVER (ORDER BY SALARY DESC) AS percent_salary_rank
FROM
oehr_employees;

This query calculates the percentage rank of each employee's salary in descending
order.

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
SALARY,
PERCENT_RANK() OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY
DESC) AS dept_percent_salary_rank
FROM
oehr_employees;

This query calculates the percentage rank of each employee's salary within each
department, in descending order.

SELECT
DEPARTMENT_ID,
AVG(SALARY) AS avg_salary,
MAX(SALARY) AS max_salary,
PERCENT_RANK() OVER (ORDER BY AVG(SALARY) DESC) AS
dept_salary_percent_rank
FROM
oehr_employees
GROUP BY
DEPARTMENT_ID;

This query calculates the average and maximum salary for each department and assigns
a percent rank based on the average salary.
4. CUME_DIST() OVER

The CUME_DIST() window function calculates the cumulative distribution of a value


within a partition. It represents the cumulative probability of a value being less than or
equal to the current row's value.

Examples:

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
SALARY,
CUME_DIST() OVER (ORDER BY SALARY DESC) AS cumulative_salary_dist
FROM
oehr_ employees;

This query calculates the cumulative distribution of each employee's salary in


descending order.

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
SALARY,
CUME_DIST() OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY
DESC) AS dept_cumulative_salary_dist
FROM
oehr_employees;

This query calculates the cumulative distribution of each employee's salary within each
department, in descending order.

5. ROW_NUMBER()

The ROW_NUMBER() window function assigns a unique integer to each row within the
window. It does not consider the values of the columns; it just provides a unique identifier
for each row.

Examples:
SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
ROW_NUMBER() OVER (ORDER BY HIRE_DATE) AS hire_date_row_num
FROM
oehr_employees;

This query assigns a unique row number to each employee based on their hire date in
ascending order.

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
ROW_NUMBER() OVER (PARTITION BY JOB_ID ORDER BY HIRE_DATE) AS
job_hire_date_row_num
FROM
oehr_employees;

This query assigns a unique row number to each employee based on their hire date
within each job category, in ascending order.

SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
JOB_ID,
HIRE_DATE,
ROW_NUMBER() OVER (PARTITION BY JOB_ID ORDER BY CASE WHEN JOB_ID
= 'IT_PROG' THEN HIRE_DATE ELSE NULL END) AS job_hire_date_row_num
FROM
oehr_employees;

This query assigns row numbers within each job category but sorts employees in the
'IT_PROG' job first based on their hire date.

You might also like