-5

I have this query in MySQL:

Select * 
From Customer 
Order By ID DESC Limit 1,1

How to use this query for SQL server ?

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
  • `Select top 1 * From Customer Order By ID DESC` – DLeh Jan 22 '15 at 20:54
  • possible duplicate of [Emulate MySQL LIMIT clause in Microsoft SQL Server 2000](http://stackoverflow.com/questions/216673/emulate-mysql-limit-clause-in-microsoft-sql-server-2000) – dotnetom Jan 22 '15 at 21:02

3 Answers3

1

For SQL Server 2005 and up:

;WITH cte AS
(
    SELECT *, ROW_NUMBER() OVER (ORDER BY ID DESC) AS RowNumber
    FROM Customer
)

SELECT *
FROM cte
WHERE RowNumber = 2
Code Different
  • 90,614
  • 16
  • 144
  • 163
1

MSSQL, use this query to fetch Nth record

SELECT * FROM (
SELECT 
*, ROW_NUMBER() OVER(ORDER BY userID) AS ROW
FROM tblUser 
) AS TMP 
WHERE ROW = n
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
0

In SQL Server 2012 or higher, you can also use Offset and Fetch:

Select  * 
From    Customer 
Order By ID Desc 
Offset 1 Rows
Fetch Next 1 Rows Only
Siyual
  • 16,415
  • 8
  • 44
  • 58