Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views4 pages

Window

The document compares three SQL ranking functions: ROW_NUMBER(), RANK(), and DENSE_RANK(). ROW_NUMBER() assigns unique sequential numbers without gaps, RANK() assigns the same rank to ties but leaves gaps in the sequence, and DENSE_RANK() also assigns the same rank to ties but does not leave gaps. Example SQL queries are provided for each function using a 'Students' table.
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)
10 views4 pages

Window

The document compares three SQL ranking functions: ROW_NUMBER(), RANK(), and DENSE_RANK(). ROW_NUMBER() assigns unique sequential numbers without gaps, RANK() assigns the same rank to ties but leaves gaps in the sequence, and DENSE_RANK() also assigns the same rank to ties but does not leave gaps. Example SQL queries are provided for each function using a 'Students' table.
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/ 4

ROW_NUMBER()

VS
RANK()
VS
DENSE_RANK()
ROW_NUMBER()
Purpose: Assigns a unique sequential number
to each row, starting from 1, without any
gaps, even if there are ties

During ties: When there are multiple rows


with the same value, ROW_NUMBER() will still
assign them unique sequential numbers.
SELECT
Name
, Score
, ROW_NUMBER() OVER (ORDER BY Score DESC) AS RN
FROM
Students;
RANK()
Purpose: Assigns a rank to each row, starting
from 1. It handles ties by giving the same rank to
rows with identical values but leaves gaps in the
ranking sequence.

During ties: If there are ties, rows with the same


value will receive the same rank, and the next
rank will skip the number(s) of the tied ranks.

SELECT
Name
, Score
, RANK() OVER (ORDER BY Score DESC) AS RN
FROM
Students;
DENSE_RANK()
Purpose: Similar to RANK(), but does not leave
gaps in the ranking sequence when there are
ties.
During ties: If there are ties, all rows with the
same value will receive the same rank, and the
next rank will be consecutive (i.e., no gaps).

SELECT
Name
, Score
, DENSE_RANK() OVER (ORDER BY Score DESC) AS RN
FROM
Students;

You might also like