How to find the second highest marks in MySQL?
The examples use the students table from our MySQL tutorial: id, name, city, marks, course_id.
-- 1. with a subquery
SELECT MAX(marks) AS second_highest
FROM students
WHERE marks < (SELECT MAX(marks) FROM students);
-- 2. with LIMIT and OFFSET
SELECT DISTINCT marks
FROM students
ORDER BY marks DESC
LIMIT 1 OFFSET 1;
Tutorials Class - Output Window
| second_highest |
|---|
| 88 |
The highest marks are 92 (Maria), so the second highest is 88 (Asha). For the Nth highest, change the OFFSET to N – 1. See Subqueries.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |