Sql query to search for the fifth record

How can I find the fifth record in a table using an SQL query?

+3
source share
6 answers
SELECT TOP 1 * FROM (SELECT TOP 5 * FROM Table T ORDER BY Column ASC) ORDER BY Column Desc
+2
source

If you feel controversial, consider using "SELECT * FROM table LIMIT 1" and claiming that since SQL does not promise to return results in any particular order, the returned string is spiritually equivalent to the fifth, and then shows your tattoo: "N- the first element of the disordered set doesn't make sense! "

+10
source

SqlServer, TOP .

select top 1 * from(
select top 5 * from myTable order by orderingColumn) as A
order by orderingColumn desc

Oracle, ( )

select *
  from (
    select *, row_number() over (order by orderingColumn) r
      from items
  )
where r = 5; 
+2

SELECT * FROM table LIMIT 1 OFFSET 4;

+1

MySQL

SELECT * FROM anytable LIMIT ORDER BY id LIMIT 4,1

+1

For SQL Server (recently still incarnations) something like this should work:

SELECT
  *
FROM
  (
    SELECT 
      *, 
      ROW_NUMBER() OVER (ORDER BY the_table.the_column) 'row_num'
    FROM 
      the_table
  ) numbered_rows
WHERE
  row_num = 5

However, I would actually vote for Thomas Loladei :)

0
source

Source: https://habr.com/ru/post/1770899/


All Articles