Displaying Row number without using Row_number Function in SQL Server

Here I am trying to display Row number without using predefined function (ROW_NUMBER()) available in SQLServer

Consider Table Emp  having following columns

Empname varchar(50) 
EmpId   int   PK

Here I am assuming Empid as PK but not an identity column.

SQL QUERY

I have used inner query to display row number.

Select Empname, EmpId,
(Select Count(*) from Emp emp1 where emp1.EmpId< emp2.EmpId )  AS Rownumber from Emp emp2
where Empname is not null 
order by Empname

I also face this question in many interviews as well.

Ebook Download
View all
Learn
View all