Select nth Row from Table in SQL Server


DECLARE @RowNumber INT = 9

WITH TableWithRow AS
(
      SELECT
       (ROW_NUMBER() OVER(ORDER BY employees.EmployeeID)) as ROW,
       FirstName,LastName         
      FROM employees
)

SELECT * FROM TableWithRow WHERE ROW = @RowNumber


Output:

ROW                  FirstName  LastName
-------------------- ---------- --------------------
9                    Ashvin     Padhiyar

(1 row(s) affected) 


Note: ROW_NUMBER() is an analytic function. It assigns a unique number to each row to which it is applied, in the ordered sequence of rows specified in the order_by_clause, beginning with 1.
 

Comments

Popular posts from this blog

Auto Refresh .aspx page - ASP.NET

Auto Sequence Number in Grid Control with Paging

MVC Request Execution Stages - Life Cycle

LINQ - C# Programming

ASP.NET MVC Version History

How to Edit More than 200 Rows in SQL Server 2008

Page get postback twice in Mozilla!!!!

SOLID Principles with C#

How to Update Dependent Excel Cells Automatically