Write a query to select the student with the maximum marks
Description: Show the name and marks of the student who has the highest marks.
Tables: the exercises use the students (id, name, city, marks, course_id) and courses (id, course_name) tables, with the data from our MySQL INSERT chapter. The students are: Robin (Delhi, 85), Maria (Mumbai, 92), John (Pune, 78), Asha (Jaipur, 88), Vikram (Delhi, 65), Neha (Mumbai, 45).
Hint:
MAX(marks)gives the highest marks.- Or sort in descending order and take one row.
MySQL query for the student with maximum marks
-- with a subquery
SELECT name, marks
FROM students
WHERE marks = (SELECT MAX(marks) FROM students);
-- with ORDER BY and LIMIT
SELECT name, marks
FROM students
ORDER BY marks DESC
LIMIT 1;
Output:
Tutorials Class - Output Window
| name | marks |
|---|---|
| Maria | 92 |
How it works
- The subquery returns 92, and the outer query finds the row with 92 marks.
- The subquery way returns more than one row if two students have the same top marks. The
LIMIT 1way returns only one.
Read Subqueries.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |