Rank and Dense Rank in SQL Server — Transcript
Full transcript
- 0:00this is part 110 of SQL Server tutorial
- 0:03in this video we'll discuss Rank and
- 0:05dense rank functions in SQL Server Rank
- 0:08and dense rank functions are introduced
- 0:10in SQL Server 2005 both of these
- 0:12functions return a rank starting at one
- 0:15based on the ordering of rows imposed by
- 0:17the order by Clause order by Clause is
- 0:19required Partition by Clause is optional
- 0:22when the data is partition rank is reset
- 0:25to one when the partition changes now we
- 0:28have two different functions to provide
- 0:30a rank so what is the difference between
- 0:32Rank and dense rank functions rank
- 0:35function skips rankings if there is a
- 0:37tie whereas dense rank will not let's
- 0:40understand this with an example let's
- 0:42say we have two rows at rank one and we
- 0:44have five rows in total in the table in
- 0:46this case rank function is going to give
- 0:49a rank of one for the first two rows 1
- 0:52one because there is a tie for the third
- 0:54row look at that it gives a rank of
- 0:56three it has skipped rank two meaning
- 1:00rank function is going to leave holes in
- 1:02the rankings it provide if there is a
- 1:05tie dense rank on the other hand will
- 1:08not leave those holes so just like rank
- 1:11function dense rank also gives a rank of
- 1:14one to the first two rows and then for
- 1:16the third row it's actually giving a
- 1:18rank of two instead of three so that's
- 1:21the fundamental difference between Rank
- 1:23and dense rank functions and we have the
- 1:25syntax here we first specify the name of
- 1:27the function rank or dense rank followed
- 1:30by that we use the over keyword and then
- 1:32within parenthesis we use the auto by
- 1:34clause and then the column list by which
- 1:36we want to sort the data let's look at
- 1:38an example and that should make all
- 1:40these points
- 1:41clear we'll use this employees table for
- 1:44the examples in this sto what we want to
- 1:46do is sort the employees data by salary
- 1:49column in descending order and then
- 1:51provide ranks for every row so we
- 1:54basically want to provide a rank of one
- 1:55to the first highest salary rank of two
- 1:57to the second highest salary so on and
- 2:00so forth within our result set we want
- 2:06name
- 2:08gender salary and rank so let's use the
- 2:12rank function so we first specify the
- 2:15name of the function followed by that we
- 2:17use the over keyword and then within
- 2:18parenthesis we use utob by Clause utob
- 2:21by Clause is required because Rank and
- 2:24dense rank are issued based on the
- 2:27ordering imposed by this utob by clause
- 2:30so we want to sort the data by salary
- 2:32column in descending order and let's
- 2:36give this column an alas let's call this
- 2:38rank since rank is a function name let's
- 2:41wrap that with a pair of square brackets
- 2:45and let's do the same thing for dense
- 2:47rank so I'm going to use dense rank
- 2:50function as well and let's change the
- 2:53column name to dense rank let's execute
- 2:58the query and see what we get now the
- 3:00first thing to keep in mind here is that
- 3:01we are not using Partition by Clause so
- 3:05the data is not partition we have a
- 3:06single partition here now look at what
- 3:10rank and dense rank functions does for
- 3:12the first two rows both the functions
- 3:15give a rank of one and then for the
- 3:17third row rank function gives a rank of
- 3:20three whereas Den rank gives a rank of
- 3:23two so rank function is going to leave
- 3:26holes in the rankings it provides if
- 3:29there is a
- 3:30dense rank on the other hand will not do
- 3:32that it doesn't leave any holes so here
- 3:36we don't have a partition
- 3:38involved and we have that exact same
- 3:40example right here now let's see what's
- 3:43going to happen if there is a partition
- 3:45involved what we want to do now is
- 3:47partition this employee table data based
- 3:50on the gender column when we do that we
- 3:52are going to get two partitions male
- 3:54employee partition and female employee
- 3:55partition so let's use the Partition by
- 3:58class so Partition
- 4:02by gender let's do the same thing for
- 4:04dense rank as
- 4:06well now let's execute this query and
- 4:09see what we get the first thing to
- 4:11notice here is that we've got two
- 4:12partitions the first partition is female
- 4:14employee partition and look at the
- 4:16ranking it starts from one and goes till
- 4:18four same for both Rank and T rank when
- 4:21the partition changes look at what
- 4:23happens to the rank it is reset to one
- 4:26just like the r number function that we
- 4:28discussed in our previous video video
- 4:31session and we have that example right
- 4:34here so the next obvious question that
- 4:36comes to our mind is what is the use
- 4:38case for Rank and dense rank functions
- 4:41well both of these functions can be used
- 4:43to find nth highest salary however which
- 4:47function to use depends on what you want
- 4:49to do when there is a time for example
- 4:53if we have two employees with the first
- 4:56highest salary then there could be two
- 4:58possible business cas cases if your
- 5:00gness case is to not return any rows for
- 5:04the second highest salary then use rank
- 5:07function on the other hand if your
- 5:08business case is to return the row that
- 5:11is present after the tide rows as the
- 5:14second highest salary then use dense
- 5:17rank function let's understand this with
- 5:19an example so what I'm going to do is
- 5:22modify this query So within our result
- 5:24set I don't want name and gender columns
- 5:26so I'm going to get rid of those two
- 5:27columns we just want salary and then for
- 5:30now I'm going to use rank function I'm
- 5:32going to get rid of this Partition by
- 5:34Clause just use Auto by and let's call
- 5:38this
- 5:39column salary
- 5:42Rank and let's remove this T rank
- 5:45function
- 5:47now now whatever result we get when we
- 5:50execute this query I'm going to make it
- 5:52a part of a CDA Common Table expression
- 5:56so with let's call this result
- 6:05as so that's our CTE there now what I
- 6:09want to
- 6:10do is
- 6:14select top
- 6:18one
- 6:20salary
- 6:22from our result
- 6:25City
- 6:27where rank equals 1 now what I'm going
- 6:31to do is just about this I'm going to
- 6:33include another query select star from
- 6:36employees order
- 6:38buy salary descending
- 6:42order so let's execute this and see what
- 6:45is the highest salary so highest salary
- 6:47is 8,000 now if we want to find the
- 6:50highest salary we can use this you know
- 6:52the first highest
- 6:54salary so actually the column name is
- 6:56salary rank not
- 6:58rank
- 7:00where salary rank is one so when we
- 7:02execute this we get the first highest
- 7:04salary without any problem but when I
- 7:07say I want the second highest
- 7:10salary look at what I'm going to get I
- 7:13get no row if your business case is this
- 7:16if there is a tie then you know here the
- 7:19tie is for the first highest salary that
- 7:21means we are not going to have the
- 7:23second highest salary if we use the rank
- 7:26function and that is what your business
- 7:28requirement is then use the rank
- 7:30function return no rows if there is a
- 7:32tie for the first rank for the second
- 7:34highest salary okay and and basically
- 7:37that's because rank function leaves
- 7:41holes so let's execute this query look
- 7:44at that we don't have a salary rank of
- 7:46two so that's why it returns no Row for
- 7:49the second highest salary on the other
- 7:51hand if your requirement is you know
- 7:54well even if there is a tie you know in
- 7:56this case the tie is for the first two
- 7:58rows I mean for the first highest salary
- 8:01so but still for the third row I want to
- 8:03treat that as the uh row with the second
- 8:06rank in that case use dense rank
- 8:11function so when I use dense Rank and
- 8:15when we execute this we're going to get
- 8:197,000 as the second highest
- 8:22salary okay so depending on your
- 8:25business requirement you can use rank or
- 8:27dense rank
- 8:30now we can also use these Rank and dense
- 8:33rank functions to find the nth highest
- 8:35salary among male or female employee
- 8:37groups and by now you might have already
- 8:39guessed the way we do that is by using
- 8:42Partition by Clause so if you want to
- 8:44find for example let's say the third
- 8:47highest salary amount paid among the
- 8:49female employee group we can very easily
- 8:52achieve that by using Partition by
- 8:56class so Partition by let's partition
- 9:01the data by gender
- 9:07column first let's execute this query on
- 9:10its own so this is going to partition
- 9:12the data look at that you know this is
- 9:16the female employee group we can very
- 9:18easily that if we see that if we include
- 9:20the gender column and this is the male
- 9:22employee group so among the female
- 9:24employee group if you look at the
- 9:26salaries the third highest salary is
- 9:294,500 so I can say select top one salary
- 9:33from results so we need gender column as
- 9:35well within our result set so I'm going
- 9:37to include
- 9:39that so select top one salary from
- 9:41result where salary rank equals three
- 9:47and gender
- 9:50equals
- 9:52female okay so when we do this we should
- 9:56get
- 9:584,500 there we
- 10:02go thank you for listening and have a
- 10:04great
- 10:06day
About this transcript
This page contains the full transcript of Rank and Dense Rank in SQL Server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,590 words across 231 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.