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

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

    For SQL server, the following will return the first row from giving table.

    declare @rowNumber int = 1;
        select TOP(@rowNumber) * from [dbo].[someTable];
    EXCEPT
        select TOP(@rowNumber - 1) * from [dbo].[someTable];
    

    You can loop through the values with something like this:

    WHILE @constVar > 0
    BEGIN
        declare @rowNumber int = @consVar;
           select TOP(@rowNumber) * from [dbo].[someTable];
        EXCEPT
           select TOP(@rowNumber - 1) * from [dbo].[someTable];  
    
           SET @constVar = @constVar - 1;    
    END;
    
    0 讨论(0)
  • 2020-11-22 06:41

    T-SQL - Selecting N'th RecordNumber from a Table

    select * from
     (select row_number() over (order by Rand() desc) as Rno,* from TableName) T where T.Rno = RecordNumber
    
    Where  RecordNumber --> Record Number to Select
           TableName --> To be Replaced with your Table Name
    

    For e.g. to select 5 th record from a table Employee, your query should be

    select * from
     (select row_number() over (order by Rand() desc) as Rno,* from Employee) T where T.Rno = 5
    
    0 讨论(0)
  • 2020-11-22 06:42

    There are ways of doing this in optional parts of the standard, but a lot of databases support their own way of doing it.

    A really good site that talks about this and other things is http://troels.arvin.dk/db/rdbms/#select-limit.

    Basically, PostgreSQL and MySQL supports the non-standard:

    SELECT...
    LIMIT y OFFSET x 
    

    Oracle, DB2 and MSSQL supports the standard windowing functions:

    SELECT * FROM (
      SELECT
        ROW_NUMBER() OVER (ORDER BY key ASC) AS rownumber,
        columns
      FROM tablename
    ) AS foo
    WHERE rownumber <= n
    

    (which I just copied from the site linked above since I never use those DBs)

    Update: As of PostgreSQL 8.4 the standard windowing functions are supported, so expect the second example to work for PostgreSQL as well.

    Update: SQLite added window functions support in version 3.25.0 on 2018-09-15 so both forms also work in SQLite.

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

    I suspect this is wildly inefficient but is quite a simple approach, which worked on a small dataset that I tried it on.

    select top 1 field
    from table
    where field in (select top 5 field from table order by field asc)
    order by field desc
    

    This would get the 5th item, change the second top number to get a different nth item

    SQL server only (I think) but should work on older versions that do not support ROW_NUMBER().

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

    SQL SERVER


    Select n' th record from top

    SELECT * FROM (
    SELECT 
    ID, NAME, ROW_NUMBER() OVER(ORDER BY ID) AS ROW
    FROM TABLE 
    ) AS TMP 
    WHERE ROW = n
    

    select n' th record from bottom

    SELECT * FROM (
    SELECT 
    ID, NAME, ROW_NUMBER() OVER(ORDER BY ID DESC) AS ROW
    FROM TABLE 
    ) AS TMP 
    WHERE ROW = n
    
    0 讨论(0)
  • 2020-11-22 06:44

    Contrary to what some of the answers claim, the SQL standard is not silent regarding this subject.

    Since SQL:2003, you have been able to use "window functions" to skip rows and limit result sets.

    And in SQL:2008, a slightly simpler approach had been added, using
    OFFSET skip ROWS FETCH FIRST n ROWS ONLY

    Personally, I don't think that SQL:2008's addition was really needed, so if I were ISO, I would have kept it out of an already rather large standard.

    0 讨论(0)
提交回复
热议问题