0% found this document useful (0 votes)
31 views8 pages

SQL Zero To Hero DAY-15: Important Window Functions in SQL Part-2

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)
31 views8 pages

SQL Zero To Hero DAY-15: Important Window Functions in SQL Part-2

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/ 8

SQL ZERO TO HERO

DAY-15

IMPORTANT WINDOW
FUNCTIONS IN SQL
Part-2

RANK(), DENSE RANK (), ROW_NUMBER()

Created by:
Devikrishna R
Youtube - Vision Board: https://youtube.com/shorts/Udx6ENQjAoE

LinkedIn - https://www.linkedin.com/in/devikrishna-r-
%F0%9F%87%AE%F0%9F%87%B3-92085678/
INTERMEDIATE -ADVANCE CONCEPT

SQL window function types

The window functions are divided into three types value window functions, aggregation
window functions, and ranking window functions:

Value window functions

• FIRST_VALUE()
• LAG()
• LAST_VALUE()
• LEAD()

Ranking window functions

• CUME_DIST()
• DENSE_RANK()
• NTILE()
• PERCENT_RANK()
• RANK()
• ROW_NUMBER()

Aggregate window functions

• AVG()
• COUNT()
• MAX()
• MIN()
• SUM()

WINDOW FUNCTION

window_function_name(expression)
OVER (
[partition_defintion]
[order_definition]
)
window_function: The name of the window function.

Expression: it is the column name for which we calculate an aggregated value.

OVER: It specifies the window clauses for aggregate functions. It mainly contains two
expressions partition by and order by, and it always has an opening and closing
parenthesis even there is no expression.

PARTITION BY: This clause divides the rows into partitions, and then a window
function is operated on each partition. Here we need to provide columns for the
partition after the PARTITION BY clause. If we want to specify more than one column,
we must separate them by a comma operator. SQL Server will group the entire table
when this clause is not specified, and values will be aggregated accordingly.

ORDER BY: It is used to specify the order of rows within each partition. When this
clause is not defined, SQL Server will use the ORDER BY for the entire table.

SQL ROW_NUMBER() Function

The SQL ROW_NUMBER() is a window function that assigns a sequential integer


number to each row in the query’s result set.

Select * from Employee

--Employee Table already created before

Insert into Employee values (8,'jay','k',1080,'DataEng','Male',1);


Insert into Employee values (9,'vinu','y',6060,'Finance','Male',2);
Insert into Employee values (10,'Li','l',10500,'Art','Male',4);
Insert into Employee values (11,'Ann','K',8500,'HR','FeMale',5);
Insert into Employee values (12,'Frankile','J',5500,'DataEng','Male',6);
Insert into Employee values (13,'ji','jo',1080,'DataEng','FeMale',1);
Insert into Employee values (14,'vint','k',6080,'Finance','Male',2);
Insert into Employee values (15,'Lieo','w',11100,'Art','Male',4);
Insert into Employee values (16,'Ansusan','K',8550,'HR','FeMale',5);
Insert into Employee values (17,'keer','J',5700,'DataEng','Male',6);
QUESTIONS:

--1.MAX SALARY FROM THE EMPLOYEE TABLE

--2. SELECT DEPARTMENT WISE MAX SALARY

--3.ROW_NUMBER

--4. HOW TO WRITE A MAX SALARY PER DEPARTMENT WISE.

--5. HOW TO WRITE A PAGINATION (ARRANGE IN PAGE ).

--6 FETCH THE FIRST EMPLOYEE, WHO JOINED IN EACH DEPARTMENT.

select MAX(salary) from Employee

select Department, MAX(salary) as Mxsalary from Employee group by Department

Select *, ROW_NUMBER() over (order by department) from Employee

--Partition

Select *,MAX(salary) over(partition by department) from Employee

SELECT
ROW_NUMBER() OVER (
ORDER BY department
) row_num,
firstname,
lastname,
salary,Department
FROM
employee;
---Pagenation

SELECT * FROM (
SELECT
ROW_NUMBER() OVER (
ORDER BY department
) row_num,
firstname,
lastname,
salary,Department
FROM
employee
)e
WHERE
row_num > 10 AND row_num <=17;

SELECT department,
ROW_NUMBER() OVER (
PARTITION BY department
ORDER BY salary DESC) row_num,
firstname,
lastname,
salary
FROM
employee

-- Fetch first employee joined in each department

Select * from (SELECT department,


ROW_NUMBER() OVER (
PARTITION BY department
ORDER BY department DESC) row_num,
firstname,
lastname,
salary
FROM
employee) data where row_num=1

• First, the PARTITION BY clause distributes the employees by departments.


• Second, the ORDER BY clause sorts the employee in each department by
salary in the descending order.
• Third, the ROW_NUMBER() assigns each row a sequential integer number. It
resets the number when the department changes.
2) RANK
The RANK() function is a window function that assigns a rank to each row in the partition of a
result set.

RANK() OVER (
PARTITION BY expr
ORDER BY expr )

QUESTIONS:

--1. Arrange all the employees in a rank based on their salary

--2. Arrange all the employees in a rank based on their salary and department wise.

--3. find 3 top employees in a rank based on their salary and department.

--2. RANK

SELECT
Firstname,
Lastname,
Department,
salary,
RANK() OVER ( ORDER BY salary desc) salary_rank
FROM
Employee;

SELECT
Firstname,
Lastname,
Department,
salary,
RANK() OVER (partition by department ORDER BY salary desc) salary_rank
FROM
Employee;

select * from (
SELECT
Firstname,
Lastname,
Department,
salary,
RANK() OVER (partition by department ORDER BY salary desc) salary_rank
FROM
Employee) emp where salary_rank<4

3.SQL DENSE_RANK() function

This window function that assigns ranks to rows in partitions with no gaps in the
ranking values.

If two or more rows in each partition have the same values, they receive the same
rank. The next row has the rank increased by one.

Different from the RANK() function, the DENSE_RANK() function always generates
consecutive rank values.

Syntax:

DENSE_RANK() OVER (
PARTITION BY expr
ORDER BY expr )

--Dense Rank
Select * , RANK() OVER (partition by department ORDER BY salary) salary_rank
FROM Employee

Select * ,
RANK() OVER (partition by department ORDER BY salary) rank_rank,
DENSE_RANK() OVER (partition by department ORDER BY salary) dense_rank

from Employee

IMPORTANT INTERVIEW QUESTIONS:

What is the difference between RANK(), DENSE RANK (), ROW_NUMBER()?

3 Krish R 10000 Art Male NULL 1 1 1


10 Li l 10500 Art Male 4 2 2 2
15 Lieo w 11100 Art Male 4 3 3 3
1 Devi R 1000 DataEng FeMale 5 1 1 1
8 jay k 1080 DataEng Male 1 2 2 2
13 ji jo 1080 DataEng FeMale 1 2 2 3
5 Harshit P 5000 DataEng Male 1 4 3 4
12 Frankile J 5500 DataEng Male 6 5 4 5
17 keer J 5700 DataEng Male 6 6 5 6
2 Vimal s 6000 Finance Male 4 1 1 1
9 vinu y 6060 Finance Male 2 2 2 2
14 vint k 6080 Finance Male 2 3 3 3
4 Jan K 8000 HR FeMale 2 1 1 1
11 Ann K 8500 HR FeMale 5 2 2 2
16 Ansusan K 8550 HR FeMale 5 3 3 3
6 gle c 4000 software Male NULL 1 1 1
tech
7 reenu k 9000 dept FeMale NULL 1 1 1

Select * ,
RANK() OVER (partition by department ORDER BY salary) rank_rank,
DENSE_RANK() OVER (partition by department ORDER BY salary) denserank,
ROW_NUMBER() OVER (partition by department ORDER BY salary) rownum

FROM Employee

You might also like