Here we will explain the difference between ROW_NUMBER(), OVER() and PARTITION BY clauses. This is one of the basic and necessary terms that you may have used in your daily life when working on SQL Server and of course a question that is generally asked in interviews.
Here I've written a few lines of code to show it.
Before jumping into the code, let's understand what the ROW_NUMBER() and PARTITION BY clauses are.
ORDER BY is a required clause for using ROW_NUMBER() since row number is generated on the basis of the column used along with ROW_NUMBER().
Example
Assume there are 7 employees in your table, 3 are from the city Roorkee and the other 4 are from Rohtak and Noida respectively.
Now, if you would use "Order By empid" only, it would assign ROW_NUMBER() on the basis of empid in ascending order from 1, 2, 3, 4,5,6,7.
But, if you want to generate row numbers differently for cities then use PARTITION BY.
Please have a look at the following example:
Before jumping into the code, let's understand what the ROW_NUMBER() and PARTITION BY clauses are.
ORDER BY is a required clause for using ROW_NUMBER() since row number is generated on the basis of the column used along with ROW_NUMBER().
Example
Assume there are 7 employees in your table, 3 are from the city Roorkee and the other 4 are from Rohtak and Noida respectively.
Now, if you would use "Order By empid" only, it would assign ROW_NUMBER() on the basis of empid in ascending order from 1, 2, 3, 4,5,6,7.
But, if you want to generate row numbers differently for cities then use PARTITION BY.
Please have a look at the following example:
- declare @table table(
- empid varchar(10),Name varchar(20), city char(10)
- )
- insert into @table
- select 'EMP001','Sachin Kalia', 'RoorKee'
- union all select 'EMP002', 'Rohit Kalia', 'RoorKee'
- union all select 'EMP003', 'Yogendra', 'RoorKee'
- union all select 'EMP004', 'Ravish Sindhwani', 'Rohtak'
- union all select 'EMP005', 'Parvinder', 'Rohtak'
- union all select 'EMP006', 'Abhinav Singh', 'Noida'
- union all select 'EMP006', 'Anshu Agarwal', 'Noida'
- Select * from @table

Now execute the following lines one by one and see the actual facts.
- declare @table table(
- empid varchar(10),Name varchar(20), city char(10)
- )
- insert into @table
- select 'EMP001','Sachin Kalia', 'RoorKee'
- union all select 'EMP002', 'Rohit Kalia', 'RoorKee'
- union all select 'EMP003', 'Yogendra', 'RoorKee'
- union all select 'EMP004', 'Ravish Sindhwani', 'Rohtak'
- union all select 'EMP005', 'Parvinder', 'Rohtak'
- union all select 'EMP006', 'Abhinav Singh', 'Noida'
- union all select 'EMP006', 'Anshu Agarwal', 'Noida'
- --Select * from @table
- SELECT *, ROW_NUMBER() OVER (ORDER BY empid ) As Counter
- FROM @table

One more clause that can be used with OVER is PARTITION BY for use when you want to set one more level of filtration when generating Row_Number. Here's an example:
- declare @table table(
- empid varchar(10),Name varchar(20), city char(10)
- )
- insert into @table
- select 'EMP001','Sachin Kalia', 'RoorKee'
- union all select 'EMP002', 'Rohit Kalia', 'RoorKee'
- union all select 'EMP003', 'Yogendra', 'RoorKee'
- union all select 'EMP004', 'Ravish Sindhwani', 'Rohtak'
- union all select 'EMP005', 'Parvinder', 'Rohtak'
- union all select 'EMP006', 'Abhinav Singh', 'Noida'
- union all select 'EMP006', 'Anshu Agarwal', 'Noida'
- SELECT *, ROW_NUMBER() OVER (PARTITION BY city ORDER BY empid) As CounterByCityName FROM @table

This is the beauty of these keywords and may be utilized to make the rows sequential in manner.
Kindly find an attached SQL file.
Thanks!

Santhakumar MunuswamyPosted Jul 30, 2015, 7:16 AM
Thanks for nice article
Sibeesh VenuPosted Jul 30, 2015, 5:04 AM
Nice Share
Pankaj Kumar ChoudharyPosted Jul 29, 2015, 7:40 PM
Nice Explain Sir............
Sachin KaliaPosted Jul 29, 2015, 12:54 PM
@Thanks to All for going through with this...
Shridhar SharmaPosted Jul 29, 2015, 12:04 PM
good one sir
Atul RawatPosted Jul 29, 2015, 10:25 AM
nice article sir
RakeshPosted Jul 29, 2015, 9:36 AM
Nice one sir
Gopi ChandPosted Jul 29, 2015, 8:56 AM
well done
Nilesh JadavPosted Jul 29, 2015, 8:53 AM
Nice one sir
Neeraj KumarPosted Jul 29, 2015, 8:04 AM
Nice Article
Rajeesh MenothPosted Jul 29, 2015, 7:30 AM
Good One