Row Number function in SQL Server — Transcript
Full transcript
- 0:00this is part 109 of SQL Server tutorial
- 0:03in this video we'll discuss R number
- 0:05function in SQL Server this is
- 0:07continuation to part 108 so please watch
- 0:09part 108 before
- 0:11proceeding row number function is
- 0:13introduced in SQL Server 2005 it Returns
- 0:16the sequential number of a row starting
- 0:19at One Auto by Clause is required
- 0:22Partition by Clause is optional when the
- 0:24data is partitioned row number is reset
- 0:27to one when the partition changes we
- 0:29have the syntax here we first specify
- 0:31the row number function followed by that
- 0:34we use over keyword and then within
- 0:36parenthesis we use the order by clause
- 0:38and then the column list by which we
- 0:40want to sort the data let's look at an
- 0:42example and that should make all these
- 0:44points clear let's flip to SQL
- 0:46management Studio we'll use this
- 0:48employees table notice within our result
- 0:50set at the moment we have got three
- 0:51columns name gender salary in addition
- 0:54to these three columns I also want a row
- 0:57number against every Row in the result
- 1:00set so I'm going to use SQL Server
- 1:02rowcor number function this function
- 1:05does not take any parameters so followed
- 1:08by the function name we use over keyword
- 1:11and followed by that within parenthesis
- 1:14we use order by Clause now let's sort
- 1:18the data by gender column so order by
- 1:22gender and let's give this column
- 1:24andalas let's call this row number let's
- 1:29execute this and look at that we get a
- 1:31row number for every Row in the result
- 1:34set starting from one now this order by
- 1:38Clause is required if we don't specify
- 1:41the order by Clause we get an error
- 1:43saying the function rule number must
- 1:45have an Over clause with order
- 1:48by and the reason it requires order by
- 1:51Clause is because this row number is
- 1:53generated based on the order that is
- 1:55imposed by this order by
- 1:58Clause now at the moment we not using
- 2:00Partition by let's see what's going to
- 2:02happen when we use Partition by Clause
- 2:05so let's partition the
- 2:09data
- 2:11by gender column so we are partitioning
- 2:14the data by gender column ordering it by
- 2:16gender column so let's execute this
- 2:19query and see what result we get look at
- 2:21that when the data is partitioned look
- 2:23at the first four rows first so that's
- 2:26the female employees partition so the
- 2:29row number has started at 1 and it went
- 2:31all the way till four and here we are
- 2:33changing the partition from female to
- 2:36male so at row number five look at what
- 2:39happened to row number it is reset to
- 2:42one and then it goes all the way until
- 2:44six so whenever the partition changes
- 2:46row number is reset to
- 2:50one so here is the first example that we
- 2:52discussed without partition row number
- 2:54goes from 1 all the way till 10 and here
- 2:57we have the example of partition ing
- 3:00data and look at the row number 1 to 4
- 3:02for female employees and 1:6 for male
- 3:06employees now what is the use of this
- 3:08row number function what's the use case
- 3:10one of the excellent use cases is
- 3:12deleting all duplicate rows except one
- 3:15so if you have a situation where you
- 3:17have a table with a lot of duplicate
- 3:18rows and you want to delete all those
- 3:20duplicate rows except one then we can
- 3:23use row number function to very easily
- 3:25achieve that we discussed this in detail
- 3:28in part four of SQ server interview
- 3:30questions and answers video series so if
- 3:32you go to SQL server interview questions
- 3:35and answers video series and then in
- 3:38that part four is delete duplicate rows
- 3:40in SQL and here is the associated text
- 3:43article for that video so let's actually
- 3:46quickly go over this example so at the
- 3:49moment we already have employees table
- 3:50so I'm going to delete this employees
- 3:53table and use this script to create that
- 3:57employees table once again so let's copy
- 3:59the script and let's delete this
- 4:02employees table
- 4:04first and then we'll recreate that
- 4:06employees
- 4:13table so let's execute the script and
- 4:17let's select the data from this table
- 4:20Select Staff from
- 4:22employees so notice we've got you know
- 4:25several duplicate rows so what we want
- 4:27to do is delete all the duplicates
- 4:29except one and to do
- 4:31[Applause]
- 4:33that we are going to use this script
- 4:36let's actually copy
- 4:42this okay so look at the query here
- 4:45which is part of this
- 4:47CTE so when we execute this look at what
- 4:50we get so we get all the data in the
- 4:52employees table ID first name last name
- 4:54gender and salary columns and then we
- 4:57have a row number and look at how we are
- 5:00using row number function here so select
- 5:02star this is going to retrieve all the
- 5:04columns from the employees table in
- 5:06addition to all the columns we want row
- 5:08number so we are using row number
- 5:10function uh followed by that over
- 5:12keyword Partition by ID column so if you
- 5:15look at this ID column look at that you
- 5:18know we have ID 1 and for ID one rows
- 5:22look at the row number one to three and
- 5:25for rows with ID 2 row number is reset
- 5:27to one and it goes from 1 to two
- 5:30and for this partition where ID is three
- 5:33row number again starts at one and goes
- 5:35all the way till three now so this is a
- 5:40CTE a Common Table expression on that
- 5:42CTE we are issuing a delete query so the
- 5:45name of the CT is employee CT so delete
- 5:48from employee CT where row number is
- 5:51greater than one that means it's going
- 5:54to delete wherever it finds row number
- 5:57greater than one so it's going going to
- 5:59delete these two rows so what happens
- 6:02when it deletes those two rows we be
- 6:04left with one row where ID is one
- 6:07similarly it's going to delete the
- 6:09second row for the second partition and
- 6:11for the third partition it is going to
- 6:13delete these two rows you know
- 6:16effectively at the end of it it's going
- 6:18to leave just one row in each partition
- 6:21now let's say for example you want to
- 6:23delete all rows except two duplicates
- 6:26then you can simply change this
- 6:28condition to
- 6:30where row number greater than two so
- 6:32what is it going to do in that case it's
- 6:34going to delete all the rows where row
- 6:36number is greater than two so from
- 6:38partition one it's going to delete this
- 6:40row and from partition two it's not
- 6:42going to delete any row and from
- 6:44partition three it's going to delete the
- 6:46last row so let's actually execute this
- 6:48query and see what we get so it has
- 6:51deleted two rows from the table so when
- 6:54we select from employees now look at
- 6:55that we have two duplicates okay for
- 6:59every partition now when we change the
- 7:03condition to where row number greater
- 7:04than one it should delete all the
- 7:07duplicates except one now let's select
- 7:10the
- 7:12data and look at that so we don't have
- 7:15duplicates
- 7:18anymore thank you for listening and have
- 7:20a great
- 7:22day
About this transcript
This page contains the full transcript of Row Number function in SQL Server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,200 words across 173 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.