SQL SERVER: Generate Row Number/Serial Number without ordering by any columns


Introduction: In this article i am going to explain how to generate row number/serial number with the result set returned by the select query  without using any column in the order by clause. 


Description: ROW_NUMBER() function is used to generate a serial/row number for a given record set returned by the select query. We have to use ORDER BY clause along with ROW_NUMBER() function to generate row numbers so that the numbers are assigned to the specific order.

We have learned that way in previous article How to Generate Row Number/Serial Number with each row. In that article row number was generated based on the column we put in order by clause. But what if we don’t want to sort the records and still want to generate row number ? There is a way , we can use dummy sub query column inside the ORDER BY clause. 

So In this article, we will learn how to generate row number without using any column in ORDER BY clause.

Implementation: Let's understand by an example:

IF OBJECT_ID('tempdb..#tbEmployee') IS NOT NULL
DROP TABLE #tbEmployee 


CREATE TABLE #tbEmployee
(
EmployeeName VARCHAR(100),
Age INT
) 

--Insert some dummy data
INSERT INTO #tbEmployee (EmployeeName,Age)
VALUES
('Neeraj',24),
('Abhay',23),
('Arjun',24),
('Bharat',25),
('Simran',23),
('Kashish',22),
('Rohan',25)

--View table data
SELECT * FROM #tbEmployee

EmployeeName
Age
Neeraj
24
Abhay
23
Arjun
24
Bharat
25
Simran
23
Kashish
22
Rohan
25


--Generate row/serial number without ordering the result by any column
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS SrNo, * FROM #tbEmployee

--Note: we can use any constant value e.g. 0, 1, 25, 100,null or
--any character or string value to generate row/serial number without ordering the result by any column
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS SrNo, * FROM #tbEmployee

SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 100)) AS SrNo, * FROM #tbEmployee

SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS SrNo, * FROM #tbEmployee

SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 'a')) AS SrNo, * FROM #tbEmployee

SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 'hello')) AS SrNo, * FROM #tbEmployee

SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NEWID())) AS SrNo, * FROM #tbEmployee

Result will be same for all of the above queries as:

SrNo
EmployeeName
Age
1
Neeraj
24
2
Abhay
23
3
Arjun
24
4
Bharat
25
5
Simran
23
6
Kashish
22
7
Rohan
25

Row number is generated for each row but records are in the same order that were added in the table.

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Newest
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon