Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 February SQL Server: Maths of ROW_NUMBER, RANK, DENSE_RANK

SQL Server: Maths of ROW_NUMBER, RANK, DENSE_RANK

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am comparing and sharing maths on ROW_NUMBER(), RANK(), and DENSE_RANK() of SQL Server. We are using these functions in day to day work, but still few people are not aware of it.

Something like, Can we use DENSE_RANK() as ROW_NUMBER()?
The answer is yes, we can use for DISTINCT records because using ROW_NUMBER(), you cannot generate distinct records as it applicable for all records.

Check the below samples, and try it yourself:

Create a table with sample records:

1
2
3
4
5
6
7
CREATE TABLE tbl_numbers (name varchar(10))
GO
 
INSERT INTO tbl_numbers
VALUES ('ABC'),('ABC'),('ABC')
,('XYZ'),('XYZ'),('LMN'),('OPQ'),('OPQ')
GO

Compare the output of Row_Number, Rank, DENSE_RANK:

1
2
3
4
5
6
SELECT
name
,ROW_NUMBER() OVER(ORDER BY name) AS RowNumber
,RANK() OVER(ORDER BY name) AS RankNumber
,DENSE_RANK() OVER(ORDER BY name) AS DENSE_RankNumber
FROM tbl_numbers

Result:

1
2
3
4
5
6
7
8
9
10
name RowNumber RankNumber DENSE_RankNumber
---------- -------------------- -------------------- --------------------
ABC 1 1 1
ABC 2 1 1
ABC 3 1 1
LMN 4 4 2
OPQ 5 5 3
OPQ 6 5 3
XYZ 7 7 4
XYZ 8 7 4

Using ROW_NUMBER, you cannot get the distinct records:

1
2
3
4
SELECT DISTINCT
name
,ROW_NUMBER() OVER(ORDER BY name) AS RowNumber
FROM tbl_numbers

Result:

1
2
3
4
5
6
7
8
9
10
name RowNumber
---------- ------------
ABC 1
ABC 2
ABC 3
LMN 4
OPQ 5
OPQ 6
XYZ 7
XYZ 8

Using CTE, get the distinct records:

1
2
3
4
5
6
7
8
9
10
WITH CTE AS
(
SELECT DISTINCT
name
FROM tbl_numbers
)
SELECT
name
,ROW_NUMBER() OVER(ORDER BY name) AS RowNumber
FROM CTE

Result:

1
2
3
4
5
6
name RowNumber
---------- -----------
ABC 1
LMN 2
OPQ 3
XYZ 4

Using DENSE_RANK, get the distinct records:
This query is faster than, CTE and you can use DENSE_RANK as a ROW_NUMBER for distinct Records.

1
2
3
4
SELECT DISTINCT
name
,DENSE_RANK() OVER(ORDER BY name) AS DENSE_RankNumber
FROM tbl_numbers

Result:

1
2
3
4
5
6
name DENSE_RankNumber
---------- --------------------
ABC 1
LMN 2
OPQ 3
XYZ 4

Feb 19, 2018Anvesh Patel
SQL Puzzle: SQL Advance Query – Using Recursive CTE, Generates the ColumnsPostgreSQL: Generate Number series and Date time series or sequence

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

February 19, 2018 SQL ServerAnvesh Patel, database, database research and development, dbrnd, dense_rank(), RANK, ROW_NUMBER, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....