Difference between rank dense rank and row number in SQL — Transcript
Full transcript
- 0:00this is Part 111 of SQL Server tutorial
- 0:03in this video we'll discuss the
- 0:04difference between rank dense Rank and
- 0:07row number functions in SQL Server first
- 0:10let's look at the similarities between
- 0:11these three functions all these three
- 0:13functions return an increasing integer
- 0:16value starting at one based on the
- 0:18ordering of rows imposed by the order by
- 0:20Clause if there are no ties if there are
- 0:23ties then the rules are a bit different
- 0:25which we will discuss in just a bit for
- 0:28all these three functions Auto by Clause
- 0:30is required Partition by Clause is
- 0:32optional when the data is partition the
- 0:35integer value is reset to one when the
- 0:37partition changes so all these are the
- 0:39similarities and another important thing
- 0:41to keep in mind is that all these three
- 0:44functions are going to behave exactly
- 0:46the same way if we don't have duplicate
- 0:49values in the column that we are using
- 0:51in the order by Clause so if you look at
- 0:54these three functions here row number
- 0:55rank and dense rank we know that for all
- 0:58these three functions autoby Clause is
- 1:00required so we are using the autoby
- 1:02clause and then we are sorting the data
- 1:05by salary column in descending order and
- 1:08if you look at that salary column in
- 1:10this table notice that we don't have any
- 1:12duplicate values in that column and if
- 1:15you look at what data this query
- 1:17produces look at the row number rank and
- 1:20dense rank functions all three of them
- 1:23produce the same numbering
- 1:25sequence that's how the behavior is
- 1:28going to be if you don't have have
- 1:30duplicate values in the column that you
- 1:33have used in the aut by Clause of these
- 1:36three
- 1:37functions now I have already typed this
- 1:39exact same query here look at this when
- 1:41we execute that we get the same
- 1:44numbering sequence for all the three
- 1:46functions that we have seen on the slide
- 1:48if you want to see the difference
- 1:50between these three functions then we
- 1:52need to have some duplicate data in the
- 1:55column that we have used in the aut by
- 1:57Clause so to get some duplicate data
- 2:00what I'm going to do is delete the data
- 2:02that we already have in this employees
- 2:05table and then I'm going to execute this
- 2:07insert script right here and if you look
- 2:10at this insert script notice that for
- 2:11the first three rows we are inserting
- 2:13same salary
- 2:158,000 now let's go ahead and execute the
- 2:19same query and look at the result we get
- 2:22we get a different result now let's see
- 2:25that in this PowerPoint slide we can
- 2:27actually see it better here now look at
- 2:29this for the first plus three rows we
- 2:31have the same salary 8,000 so there is a
- 2:34tie between those three rows in this
- 2:37case row number function is still going
- 2:39to do it the same way as it has been
- 2:42doing it's going to assign an increasing
- 2:45unique number for each row starting at
- 2:48one so it starts from one goes all the
- 2:50way till five even if there are ties
- 2:53whereas if you look at the rank function
- 2:55for the tide rows that is for the
- 2:57duplicate rows it's giving the same rank
- 2:59one
- 3:00okay and then if you look at the row
- 3:02that is present after the tide rows look
- 3:04at that it didn't give it rank two it
- 3:07has skipped rank two three and for this
- 3:11third row it has given I mean fourth row
- 3:13it has given a rank of
- 3:14four okay that means rank function is
- 3:18going to skip rankings if there are
- 3:21duplicate rows if you look at dense rank
- 3:24function this function just like rank
- 3:26function it's going to assign the same
- 3:28rank where we have ties so in this case
- 3:31it has assigned one for the first three
- 3:34rows and then for the third fourth row
- 3:37that is present after the tide rows it
- 3:40has given a rank of two it didn't skip
- 3:43any rank so that's the fundamental
- 3:45difference between these three functions
- 3:47so here is the difference row number
- 3:50returns an increasing unique number for
- 3:52each row starting at one even if there
- 3:54are duplicates rank function returns an
- 3:57increasing unique number for each row
- 3:59starting at one
- 4:00when there are duplicates same rank is
- 4:02assigned to all the duplicate rows but
- 4:04the next row after the duplicate rows
- 4:07will have the rank it would have been
- 4:09assigned if there had been no duplicates
- 4:12so rank function skips rankings if there
- 4:14are duplicates dense rank returns an
- 4:17increasing unique number for each row
- 4:20starting at one just like the rank
- 4:21function when there are duplicates same
- 4:24rank is assigned to all the duplicate
- 4:25rows but the dense rank function will
- 4:28not skip any rank ranks this means the
- 4:31next row after the duplicate rows will
- 4:34have the next rank in the
- 4:37sequence thank you for listening and
- 4:39have a great day
About this transcript
This page contains the full transcript of Difference between rank dense rank and row number in SQL by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 832 words across 117 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.