64

I have a table and I need to retrieve the ID of the Second row. How to achieve that ?

By Top 2 I select the two first rows, but I need only the second row

Adam Miller
  • 767
  • 1
  • 9
  • 22
Tony
  • 12,405
  • 36
  • 126
  • 226

18 Answers18

117

In SQL Server 2012+, you can use OFFSET...FETCH:

SELECT
   <column(s)>
FROM
   <table(s)>
ORDER BY
   <sort column(s)>
OFFSET 1 ROWS   -- Skip this number of rows
FETCH NEXT 1 ROWS ONLY;  -- Return this number of rows
SQLDiver
  • 1,948
  • 2
  • 11
  • 14
30

Assuming SQL Server 2005+ an example of how to get just the second row (which I think you may be asking - and is the reason why top won't work for you?)

set statistics io on

;with cte as
(
  select *
    , ROW_NUMBER() over (order by number) as rn
  from master.dbo.spt_values
) 
select *
from cte
where rn = 2

/* Just to add in what I was running RE: Comments */
;with cte as
(
  select top 2 *
    , ROW_NUMBER() over (order by number) as rn
  from master.dbo.spt_values
) 
select *
from cte
where rn = 2
Dale K
  • 25,246
  • 15
  • 42
  • 71
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
  • by using `TOP 2` in the CTE, like I do in my answer (inaddition to the `ROW_NUMBER()`), you can improve the efficiency of the query by a factor of about 6. Using `set showplan_all on` my version has a TotalSubtreeCost of 0.0125678 while this version (without using a `TOP 2`) has a value of 0.06937763, while returning the same result set. – KM. Sep 01 '10 at 11:52
  • @KM - I'm not sure whether there is a real saving there as opposed to helping make the estimated cost more accurate. When I use `set statistics io on` it doesn't look as though it makes any difference. I'm quite wary of reading too much into the figures given in the execution plan ever since this http://stackoverflow.com/questions/3424650/sql-query-pervious-row-optimisation/3426364#3426364 – Martin Smith Sep 01 '10 at 12:12
  • when I run it with `set statistics io on`, your version runs with `Table 'spt_values'. Scan count 1, logical reads 18, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.` after adding the `TOP 2`, I get `Table 'spt_values'. Scan count 1, logical reads 6, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.` Also, the `set showplan_all on` shows that the execution plans are different. – KM. Sep 01 '10 at 12:36
  • @KM - Are you running the same as the code in my edited answer? I've tested on SQL Server 2008 and 2005 and on both got `Table 'spt_values'. Scan count 1, logical reads 6, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.` Maybe a service pack dependant behaviour then? – Martin Smith Sep 01 '10 at 12:45
  • Yes, I'm running it just like in your edit, 1st version has: `Table 'spt_values'. Scan count 1, logical reads 18, physical reads 1, read-ahead reads 16, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.` and the 2nd version (with TOP 2) has: `Table 'spt_values'. Scan count 1, logical reads 6, physical reads 2, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.`. when I run `select count(*) from master.dbo.spt_values` I get `2346`, what do you get? if you have the same number of rows, it might come down to hardware and memory usage. – KM. Sep 01 '10 at 13:37
  • @KM - I get `2346` on SQL Server 2005 and `2506` on SQL Server 2008. I already +1 ed your answer I guess that would indeed be the safer option then. – Martin Smith Sep 01 '10 at 13:44
  • when I run this on tables other than `master.dbo.spt_values`, I see similar results (fewer logical and/or physical reads) when using the `TOP 2` – KM. Sep 01 '10 at 13:49
  • I recommend OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY. Far simpler and no cte required. – PRMan Aug 21 '20 at 21:04
  • yeah, didn't exist in SQL Server in 2010 – Martin Smith Aug 21 '20 at 21:20
23

No need of row number functions if field ID is unique.

SELECT TOP 1 *
FROM (
  SELECT TOP 2 * 
  FROM yourTable
  ORDER BY ID
) z
ORDER BY ID DESC
Dale K
  • 25,246
  • 15
  • 42
  • 71
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
11

Use ROW_NUMBER() to number the rows, but use TOP to only process the first two.

try this:

DECLARE @YourTable table (YourColumn int)
INSERT @YourTable VALUES (5)
INSERT @YourTable VALUES (7)
INSERT @YourTable VALUES (9)
INSERT @YourTable VALUES (17)
INSERT @YourTable VALUES (25)

;WITH YourCTE AS
(
  SELECT TOP 2
    *, ROW_NUMBER() OVER(ORDER BY YourColumn) AS RowNumber
  FROM @YourTable
) 
SELECT *
FROM YourCTE
WHERE RowNumber=2

OUTPUT:

YourColumn  RowNumber
----------- --------------------
7           2

(1 row(s) affected)
Dale K
  • 25,246
  • 15
  • 42
  • 71
KM.
  • 101,727
  • 34
  • 178
  • 212
9

you can use OFFSET and FETCH NEXT

SELECT id
FROM tablename
ORDER BY column
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;

NOTE:

OFFSET can only be used with ORDER BY clause. It cannot be used on its own.

OFFSET value must be greater than or equal to zero. It cannot be negative, else return error.

The OFFSET argument is used to identify the starting point to return rows from a result set. Basically, it exclude the first set of records.

The FETCH argument is used to return a set of number of rows. FETCH can’t be used itself, it is used in conjuction with OFFSET.

Vivekanand Panda
  • 832
  • 12
  • 23
  • This needs to be way higher. I used this in a subquery with no cte and it worked great. – PRMan Aug 21 '20 at 21:03
6

I'm guessing you're using SQL 2005 or greater. The 2nd line selects the top 2 rows and by using ORDER BY ROW_COUNT DESC, the 2nd row is arranged as being first, then it is selected using TOP 1

SELECT TOP 1 COLUMN1, COLUMN2
from (
  SELECT TOP 2 COLUMN1, COLUMN2
  FROM Table
) ORDER BY ROW_NUMBER DESC 
Dale K
  • 25,246
  • 15
  • 42
  • 71
Patriotec
  • 399
  • 1
  • 6
  • 21
5
with T1 as
(
  select row_number() over(order by ID) rownum, T2.ID
  from Table2 T2
)
select ID
from T1
where rownum=2
Dale K
  • 25,246
  • 15
  • 42
  • 71
Anil Soman
  • 2,443
  • 7
  • 40
  • 64
3

Use TOP 2 in the SELECT to get the desired number of rows in output. This would return in the sequence the data was created. If you have a date option you could order by the date along with TOP n Clause.

To get the top 2 rows;

SELECT TOP 2 [Id] FROM table 

To get the top 2 rows order by some field

SELECT TOP 2 [ID] FROM table ORDER BY <YourColumn> ASC/DESC

To Get only 2nd Row;

WITH Resulttable AS 
( 
  SELECT TOP 2 
    *, ROW_NUMBER() OVER(ORDER BY YourColumn) AS RowNumber 
  FROM @Table 
)  
SELECT *
FROM Resultstable
WHERE RowNumber = 2
Dale K
  • 25,246
  • 15
  • 42
  • 71
Dheer
  • 3,926
  • 6
  • 34
  • 45
2

Select top 2 [id] from table Order by [id] desc should give you want you the latest two rows added.

However, you will have to pay particular attention to the order by clause as that will determine the 1st and 2nd row returned.

If the query was to be changed like this:

Select top 2 [id] from table Order by ModifiedDate desc

You could get two different rows. You will have to decide which column to use in your order by statement.

codingbadger
  • 42,678
  • 13
  • 95
  • 110
2

I have a much easier way than the above ones.

DECLARE @FirstId int, @SecondId int

    SELECT TOP 1 @FirstId = TableId from MyDataTable ORDER BY TableId 
    SELECT TOP 1 @SecondId = TableId from MyDataTable WHERE TableId <> @FirstId  ORDER BY TableId 

SELECT @SecondId 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Mahesh4b7
  • 56
  • 5
  • Above and below are not good descriptions, as the order depends on the number of votes. Also, it would be cool, if you could explain your code snippet and the advantages to the other answer you refer to – NOhs Sep 27 '19 at 15:19
1
SELECT *
FROM (
  SELECT top 3 *
    , ROW_NUMBER() OVER (ORDER BY [newsid] desc) AS Rownumber
  FROM news
  where (news_type in(2,12))
) results
WHERE results.Rownumber = 1

// news table name and newsid column name

Dale K
  • 25,246
  • 15
  • 42
  • 71
1

Certainly TOP will surfice if you simply want the TOP 2, but if you need them individually so that you can do something with those values then use the ROW_NUMBER which will give you more control over the rows you want to select

ps. I did this as i'm not sure if the OP is after a simple TOP 2 in a select. (I may be wrong!)

-- Get first row, same as TOP 1
SELECT [Id] FROM 
(
    SELECT [Id], ROW_NUMBER() OVER (ORDER BY [Id]) AS Rownumber
    FROM table
) results
WHERE results.Rownumber = 1

-- Get second row only
SELECT [Id] FROM 
(
    SELECT [Id], ROW_NUMBER() OVER (ORDER BY [Id]) AS Rownumber
    FROM table
) results
WHERE results.Rownumber = 2
kevchadders
  • 8,335
  • 4
  • 42
  • 61
0

This is also useful:

SELECT t.*
FROM (
  SELECT e1.*
    , row_number() OVER (ORDER BY e1.Rate DESC) AS _Rank
  FROM
  HumanResources.EmployeePayHistory AS e1
) AS t
WHERE t._Rank = 2
Dale K
  • 25,246
  • 15
  • 42
  • 71
Himanshu
  • 31,810
  • 31
  • 111
  • 133
0

There is a relative simple solution based on comments from SQLDiver and Taha Ali.

Imagine there is a string containing 'domain\username' retrieved by function ORIGINAL_LOGIN() and i don't need the domain component.

First approach

DECLARE @Login NVARCHAR(100)
SELECT @Login = 'domain\username'
-- SELECT @Login = ORIGINAL_LOGIN()

SELECT
    VALUE
FROM
    STRING_SPLIT(@Login, '\')

returns a table having 2 rows. First row holds the domain and second row holds the requested username.

Using

DECLARE @Login NVARCHAR(100)
SELECT @Login = 'domain\username'
-- SELECT @Login = ORIGINAL_LOGIN()

SELECT
    VALUE
FROM
    STRING_SPLIT(@Login, '\')
ORDER BY
    (SELECT NULL)
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;

returns exactly the second row from the temporary table.

sahl04
  • 353
  • 4
  • 6
0

Another idea is this:

 SELECT MIN(id) FROM
     (
      SELECT TOP 2(TAB.[id])
          FROM TAB 
          where TAB.field1 =3
           ORDER BY TAB.[creationDate] DESC
          ) AS TEST
     )
daniele3004
  • 13,072
  • 12
  • 67
  • 75
0

Just to add another option (one that doesn't use ORDER BY at all):

SELECT TOP 2
    t2.*
FROM YourTable t2
EXCEPT
SELECT TOP 1
    t1.*
FROM YourTable t1;
Alexandre
  • 1,132
  • 1
  • 10
  • 21
-1
select *
from (
  select ROW_NUMBER() OVER (ORDER BY Column_Name) as ROWNO, *
  from Table_Name
) Table_Name
where ROWNO = 2
Dale K
  • 25,246
  • 15
  • 42
  • 71
Jay Ponkia
  • 900
  • 1
  • 8
  • 15
-2
SELECT TOP 2 [Id] FROM table
Szymon Kuzniak
  • 848
  • 1
  • 6
  • 16
  • 1
    You can order your results, so top will return not first two added but whether first and second you need – Szymon Kuzniak Sep 01 '10 at 10:50
  • 2
    YOu must order the results, you have no guarantees you wil get the first two added without an order by. Never use a top without an order by and expect to get the earliest records added. – HLGEM Sep 01 '10 at 13:34