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.
5 Answers
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

- 262,204
- 82
- 430
- 521
SELECT score
FROM student
ORDER BY score asc
LIMIT 3, 1
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

- 4,795
- 3
- 35
- 30
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

- 1
- 1

- 3,580
- 4
- 30
- 46