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

后端 未结 30 2584
执笔经年
执笔经年 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:44

    I'm a bit late to the party here but I have done this without the need for windowing or using

    WHERE x IN (...)
    
    SELECT TOP 1
    --select the value needed from in t1
    [col2]
    FROM
    (
       SELECT TOP 2 --the Nth row, alter this to taste
       UE2.[col1],
       UE2.[col2],
       UE2.[date],
       UE2.[time],
       UE2.[UID]
       FROM
       [table1] AS UE2
       WHERE
       UE2.[col1] = ID --this is a subquery 
       AND
       UE2.[col2] IS NOT NULL
       ORDER BY
       UE2.[date] DESC, UE2.[time] DESC --sorting by date and time newest first
    ) AS t1
    ORDER BY t1.[date] ASC, t1.[time] ASC --this reverses the order of the sort in t1
    

    It seems to work fairly fast although to be fair I only have around 500 rows of data

    This works in MSSQL

    0 讨论(0)
  • 2020-11-22 06:45

    Here is a fast solution of your confusion.

    SELECT * FROM table ORDER BY `id` DESC LIMIT N, 1
    

    Here You may get Last row by Filling N=0, Second last by N=1, Fourth Last By Filling N=3 and so on.

    This is very common question over the interview and this is Very simple ans of it.

    Further If you want Amount, ID or some Numeric Sorting Order than u may go for CAST function in MySQL.

    SELECT DISTINCT (`amount`) FROM cart ORDER BY CAST( `amount` AS SIGNED ) DESC LIMIT 4 , 1
    

    Here By filling N = 4 You will be able to get Fifth Last Record of Highest Amount from CART table. You can fit your field and table name and come up with solution.

    0 讨论(0)
  • unbelievable that you can find a SQL engine executing this one ...

    WITH sentence AS
    (SELECT 
        stuff,
        row = ROW_NUMBER() OVER (ORDER BY Id)
    FROM 
        SentenceType
        )
    SELECT
        sen.stuff
    FROM sentence sen
    WHERE sen.row = (ABS(CHECKSUM(NEWID())) % 100) + 1
    
    0 讨论(0)
  • 2020-11-22 06:46

    LIMIT n,1 doesn't work in MS SQL Server. I think it's just about the only major database that doesn't support that syntax. To be fair, it isn't part of the SQL standard, although it is so widely supported that it should be. In everything except SQL server LIMIT works great. For SQL server, I haven't been able to find an elegant solution.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 06:49

    In Oracle 12c, You may use OFFSET..FETCH..ROWS option with ORDER BY

    For example, to get the 3rd record from top:

    SELECT * 
    FROM   sometable
    ORDER BY column_name
    OFFSET 2 ROWS FETCH NEXT 1 ROWS ONLY;
    
    0 讨论(0)
提交回复
热议问题