2

I have student table which has column of score. I want to get the fourth highest score,Where the table data is sorted in ascending order.

Salman A
  • 262,204
  • 82
  • 430
  • 521

5 Answers5

4

This should work:

SELECT score FROM table ORDER BY score ASC LIMIT 1 OFFSET 4 
halfer
  • 19,824
  • 17
  • 99
  • 186
alagu
  • 586
  • 1
  • 4
  • 12
2

Just add the LIMIT clause:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1)

So you would write:

SELECT column_name
FROM table_name
ORDER BY column_name
LIMIT 3, 1
Salman A
  • 262,204
  • 82
  • 430
  • 521
0
SELECT score
FROM student
ORDER BY score asc
LIMIT 3, 1
0

For SQL Server queries one can use folowing statmenet:

with data as 
(
    select
        ROW_NUMBER() OVER (ORDER BY id) as rownum,
        id
    from thetable 
)
select * from data 
where rownum = 4 
order by id asc 
Stefan Michev
  • 4,795
  • 3
  • 35
  • 30
0

MSSQL doesnt support LIMIT .You have to do it using TOP Keyword or by ROW_NUMBER() for your reference here is the link LIMIT IN MSSQL

You can do it by using ROW_NUMBER()

SELECT * FROM (
      SELECT ROW_NUMBER() OVER (ORDER BY score ASC) AS RowNumber,
      *
      FROM student
 ) AS foo
WHERE RowNumber = 4

You will get 4th highest score

or you can do it like this also

SELECT * FROM (
      SELECT ROW_NUMBER() OVER (ORDER BY score ASC) AS RowNumber,
      *
      FROM student
 ) AS foo
WHERE RowNumber >3 and <=4
Community
  • 1
  • 1
Ameya Deshpande
  • 3,580
  • 4
  • 30
  • 46