T-SQL How to select only Second row from a table?

前端 未结 15 1341
攒了一身酷
攒了一身酷 2020-12-28 12:31

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

15条回答
  •  旧时难觅i
    2020-12-28 13:02

    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.

提交回复
热议问题