Posted in

SQL RANK FUNCTIONS

SQL rank functions are used to determine the position of a value in a list/table

What Does a Rank Function Do?

Sql rank functions help us to assign a rank or a number to each row in a result set based on a specified order, which will be helpful to organize and analyze data.

  • Highest number gets rank 1 (in descending order).
  • Lowest number gets rank 1 (in ascending order), depending on how you want to sort it

They help us to figure out:

  • Who came first, second, third, etc.
  • Which items are tied or repeated?
  • How each row compares to the other

Why Do We Need Rank Functions?

Ranking helps in situations like:

  • Finding top performers (e.g., top students, top salespeople).
  • Identifying trends (e.g., products by popularity).
  • Analyzing competition (e.g., leaderboard positions).
  • Prioritizing tasks/data based on scores or metrics

Example :

Let us assume we have a student table with columns student name, department and marks

Student name Department Marks
RamCse89
TomEce90
JohnEce34
RaviMech65
KiranCse45
RajiMech89
MonaCivil100
Sitacse65
RojaMech45

In this table

We can identify

Which student has got the highest mark and lowest mark totally and also, we can get highest and lowest mark in each department by using this rank functions…?

The most commonly used ranking functions in Sql are:

1. Rank():

It assigns a rank to each row within a result set based on a specified ordering (ascending or descending).

If there are ties (i.e., duplicate values in the ordering column), they receive the same rank, and the next rank is skipped accordingly.

Sales Rank()
892
901
654
455
892

In the above table, for sale value 90 it is ranked 1,

next highest sale value 89 but it is repeated twice so rank 2 will be twice hence 3 will be skipped and second rank has come 2 times

next highest sale value is 65, it will be ranked as 4

next highest sale value is 45, it will be ranked as 5

SYNTAX:

RANK() OVER (

    [PARTITION BY partition_expression]

    ORDER BY sort_expression [ASC | DESC]

)

Components

PARTITION BY: (optional) divides the result set into partitions (like groups). The rank is reset for each partition.

ORDER BY: defines the order of rows within each partition.

NOTE:

In order by asc is optional even if you do not mention asc | desc by default it will be asc

SAMPLE DATA:

select * from students;

StudentName Department Marks
RamCse89
TomEce90
JohnEce34
RaviMech65
KiranCse45
RajiMech89
MonaCivil100
SitaCse65
RojaMech45

EXAMPLE WITHOUT USING PARTITION:

a) In Ascending order

select *, rank() over(order by marks) from students;

StudentNameDepartmentMarksrank() over(order by marks)
JohnEce341
KiranCse452
RojaMech452
RaviMech654
SitaCse654
RamCse896
RajiMech896
TomEce908
MonaCivil1009

b) In Descending order

select *, rank() over(order by marks desc) from students;

StudentNameDepartmentMarksrank() over(order by marks desc)
MonaCivil1001
TomEce902
RamCse893
RajiMech893
RaviMech655
SitaCse655
KiranCse457
RojaMech457
JohnEce349

EXAMPLE WITH PARTITION:

a) In Ascending order

select *, rank() over(partition by department order by marks) as partition_rank from students;

StudentNameDepartmentMarkspartition_rank
MonaCivil1001
KiranCse451
SitaCse652
RamCse893
JohnEce341
TomEce902
RojaMech451
RaviMech652
RajiMech893

b) In Descending order

select *, rank() over(partition by department order by marks desc) as partition_rank from students;

StudentName Department Marks partition_rank
MonaCivil1001
RamCse891
SitaCse652
KiranCse453
TomEce901
JohnEce342
RajiMech891
RaviMech652
RojaMech453

2. Dense_Rank():

It assigns a rank to each row within a result set based on a specified ordering (ascending or descending).

If there are ties (i.e., duplicate values in the ordering column), they receive the same rank, and the next rank is continued will not be skipped.

Sales Dense_Rank()
892
901
653
454
892

In the above table, for sale value 90 it is ranked 1,

next highest sale value 89 but it is repeated twice so rank 2 will be twice

next highest sale value is 65, it will be ranked as 3

next highest sale value is 45, it will be ranked as 4

SYNTAX:

Dense_RANK() OVER (

    [PARTITION BY partition_expression]

    ORDER BY sort_expression [ASC | DESC]

)

Components:

PARTITION BY: (optional) divides the result set into partitions (like groups). The rank is reset for each partition.

ORDER BY: defines the order of rows within each partition.

NOTE:

In order by asc is optional even if you do not mention asc | desc by default it will be asc

EXAMPLE WITHOUT USING PARTITION:

a) In Ascending order

select *, dense_rank() over(order by marks) from students;

StudentName Department Marksdense_rank() over(order by marks)
JohnEce341
KiranCse452
RojaMech452
RaviMech653
SitaCse653
RamCse894
RajiMech894
TomEce905
MonaCivil1006

b) In Descending order

select *, dense_rank() over(order by marks desc) from students;

StudentNameDepartmentMarksdense_rank() over(order by marks desc)
MonaCivil1001
TomEce902
RamCse893
RajiMech893
RaviMech654
SitaCse654
KiranCse455
RojaMech455
JohnEce346

EXAMPLE WITH PARTITION:

a) In Ascending order

select *, dense_rank() over(partition by department order by marks) as denserank from students;

StudentName Department Marks denserank
MonaCivil1001
KiranCse451
SitaCse652
RamCse893
JohnEce341
TomEce902
RojaMech451
RaviMech652
RajiMech893

b) In Descending order

select *, dense_rank() over(partition by department order by marks desc ) as denserank from students;

StudentNameDepartmentMarksdenserank
MonaCivil1001
RamCse891
SitaCse652
KiranCse453
TomEce901
JohnEce342
RajiMech891
RaviMech652
RojaMech453

3. Row_number():

It assigns a rank to each row within a result set based on a specified ordering (ascending or descending) as well as based on insertion order of row.

If there are ties (i.e., duplicate values in the ordering column), they receive the different rank, the value in ties which are at top rows will be receiving the highest rank and the rows which are at next rows will be receiving next rank

Sales Row_number()
892
901
654
455
893

In the above table, for sale value 90 it is ranked 1,

next highest sale value 89 but it is repeated twice first sales is in first row and other is in last row hence the sales which is in first row will be getting rank as 2 and sales at last row will be getting rank as 3

next highest sale value is 65, it will be ranked as 4

next highest sale value is 45, it will be ranked as 5

SYNTAX:

row_number() OVER (

    [PARTITION BY partition_expression]

    ORDER BY sort_expression [ASC | DESC]

)

Components:

PARTITION BY: (optional) divides the result set into partitions (like groups). The rank is reset for each partition.

ORDER BY: defines the order of rows within each partition.

NOTE:

In order by asc is optional even if you do not mention asc | desc by default it will be asc

EXAMPLE WITHOUT USING PARTITION:

a) In Ascending order

select *, row_number() over(order by marks) as rowno from students;

StudentNameDepartmentMarksrowno
JohnEce341
KiranCse452
RojaMech453
RaviMech654
SitaCse655
RamCse896
RajiMech897
TomEce908
MonaCivil1009

b) In Descending order

select *, row_number() over(order by marks desc) as rowno from students;

StudentNameDepartmentMarksrowno
MonaCivil1001
TomEce902
RamCse893
RajiMech894
RaviMech655
SitaCse656
KiranCse457
RojaMech458
JohnEce349

EXAMPLE WITH PARTITION:

a) In Ascending order

select *, row_number() over(partition by department order by marks) as rowno from students;

StudentNameDepartmentMarksrowno
MonaCivil1001
KiranCse451
SitaCse652
RamCse893
JohnEce341
TomEce902
RojaMech451
RaviMech652
RajiMech893

b) In Descending order

select *, row_number() over(partition by department order by marks desc) as rowno from students;

StudentNameDepartmentMarksrowno
MonaCivil1001
RamCse891
SitaCse652
KiranCse453
TomEce901
JohnEce342
RajiMech891
RaviMech652
RojaMech453

4. NTILE(n):

It divides the result set into n equal or non-equal parts (n value is given by user), each part will be getting one value

When all the rows in result set are divided into equal parts if that number is not an integer number, then it will be rounding up to nearest even number.

Depending on that integer number the no of rows in one part will be decided

SalesNtile(n)
891
901
652
452
893

In the above table, there are 5 rows

I would like to divide it into 3 parts hence n=3

5/3=1.6

1.6 will be rounding to 2

Therefore, the result set is divided into 3 parts with 2 rows in each part, but the last part will be having one row as the no of rows are odd

SYNTAX:

ntile(n) OVER (

    [PARTITION BY partition_expression]

    ORDER BY sort_expression [ASC | DESC]

)

Components:

PARTITION BY: (optional) divides the result set into partitions (like groups). The rank is reset for each partition.

ORDER BY: defines the order of rows within each partition.

NOTE:

In order by asc is optional even if you do not mention asc | desc by default it will be asc

EXAMPLE WITHOUT USING PARTITION:

a) In Ascending order

select *, ntile(3) over(order by marks) from students;

StudentNameDepartmentMarksntile(3) over(order by marks)
JohnEce341
KiranCse451
RojaMech451
RaviMech652
SitaCse652
RamCse892
RajiMech893
TomEce903
MonaCivil1003

b) In Descending order

select *,ntile(3) over(order by marks desc) from students;

StudentNameDepartmentMarksntile(3) over(order by marks desc)
MonaCivil1001
TomEce901
RamCse891
RajiMech892
RaviMech652
SitaCse652
KiranCse453
RojaMech453
JohnEce343

EXAMPLE WITH PARTITION:

a) In Ascending order

select *,ntile(3) over(partition by department order by marks) as ranking from students;

StudentNameDepartmentMarksranking
MonaCivil1001
KiranCse451
SitaCse652
Ram Cse893
JohnEce341
TomEce902
RojaMech451
RaviMech652
RajiMech893

b) In Descending order

select *, ntile(3) over(partition by department order by marks desc) as ranking from students;

StudentNameDepartmentMarksranking
MonaCivil1001
RamCse891
SitaCse652
KiranCse453
TomEce901
JohnEce342
RajiMech891
RaviMech652
RojaMech453

Leave a Reply

Your email address will not be published. Required fields are marked *