100% found this document useful (1 vote)
143 views3 pages

SQL SERVER Rank Functions

The document discusses four ranking functions in SQL Server: RANK, DENSE_RANK, NTILE, and ROW_NUMBER. It uses a sample exam results table to demonstrate examples of each function. RANK assigns sequential ranks to rows within a partition but may have gaps. DENSE_RANK is similar but without gaps. NTILE distributes rows into a specified number of groups. ROW_NUMBER assigns a unique sequential number to each row.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
143 views3 pages

SQL SERVER Rank Functions

The document discusses four ranking functions in SQL Server: RANK, DENSE_RANK, NTILE, and ROW_NUMBER. It uses a sample exam results table to demonstrate examples of each function. RANK assigns sequential ranks to rows within a partition but may have gaps. DENSE_RANK is similar but without gaps. NTILE distributes rows into a specified number of groups. ROW_NUMBER assigns a unique sequential number to each row.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL SERVER Ranking Functions - RANK, DENSE_RANK, NTILE, ROW_NUMBER

Let's take following sample table and data to know about RANK,RANK_DENSE,NTILE,ROW_NUMBER with examples
Create table ExamResult(name varchar(50),Subject varchar(20),Marks int) insert into ExamResult values('Adam','Maths',70) insert into ExamResult values ('Adam','Science',80) insert into ExamResult values ('Adam','Social',60) insert into ExamResult values('Rak','Maths',60) insert into ExamResult values ('Rak','Science',50) insert into ExamResult values ('Rak','Social',70) insert into ExamResult values('Sam','Maths',90) insert into ExamResult values ('Sam','Science',90) insert into ExamResult values ('Sam','Social',80)

RANK(): Returns the rank of each row in the result set of partitioned column
select Name,Subject,Marks, RANK() over(partition by name order by Marks desc)Rank From ExamResult order by name,subject

DENSE_RANK() This is same as RANK() function. Only differencec is returns rank with out gaps.
select Name,Subject,Marks, DENSE_RANK() over(partition by name order by Marks desc)Rank From ExamResult order by name

in RANK() result set screeshot, you can notice that there is gap in Rank(2) for the name Sam and same gap is removed in DENSE_RANK(). NTILE(): Distributes the rows in an ordered partition into a specified number of groups. It devides the partitioned result set into specified number of groups in an order. Example for NTILE(2):
select Name,Subject,Marks, NTILE(2) over(partition by name order by Marks desc)Quartile From ExamResult order by name,subject

Example for NTILE(3):


select Name,Subject,Marks, NTILE(3) over(partition by name order by Marks desc)Quartile From ExamResult order by name,subject

ROW_NUMBER(): Returns the serial number of the row order by specified column.
select Name,Subject,Marks, ROW_NUMBER() over(order by Name) RowNumber From ExamResult order by name,subject

You might also like