How to select the nth row in a SQL database table?

后端 未结 30 2865
执笔经年
执笔经年 2020-11-22 06:06

I\'m interested in learning some (ideally) database agnostic ways of selecting the nth row from a database table. It would also be interesting to see how this can b

30条回答
  •  独厮守ぢ
    2020-11-22 06:47

    SELECT
        top 1 *
    FROM
        table_name
    WHERE
        column_name IN (
            SELECT
                top N column_name
            FROM
                TABLE
            ORDER BY
                column_name
        )
    ORDER BY
        column_name DESC
    

    I've written this query for finding Nth row. Example with this query would be

    SELECT
        top 1 *
    FROM
        Employee
    WHERE
        emp_id IN (
            SELECT
                top 7 emp_id
            FROM
                Employee
            ORDER BY
                emp_id
        )
    ORDER BY
        emp_id DESC
    

提交回复
热议问题