We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
First, match every student’s marks to their grade using a JOIN.
If the grade is 8 or higher, show the student’s name. Otherwise, show NULL.
For sorting:
Put higher grades first.
Within high grades (≥ 8), sort students alphabetically by name.
Within low grades (< 8), sort students by marks in ascending order.
SELECT
CASE WHEN g.Grade < 8 THEN NULL ELSE s.Name END AS Name,
g.Grade,
s.Marks
FROM Students s
JOIN Grades g
ON s.Marks BETWEEN g.Min_Mark AND g.Max_Mark
ORDER BY
g.Grade DESC,
CASE WHEN g.Grade >= 8 THEN s.Name ELSE s.Marks END;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Report
You are viewing a single comment's thread. Return to all comments →
First, match every student’s marks to their grade using a JOIN.
If the grade is 8 or higher, show the student’s name. Otherwise, show NULL.
For sorting:
Put higher grades first.
Within high grades (≥ 8), sort students alphabetically by name.
Within low grades (< 8), sort students by marks in ascending order.
SELECT CASE WHEN g.Grade < 8 THEN NULL ELSE s.Name END AS Name, g.Grade, s.Marks FROM Students s JOIN Grades g ON s.Marks BETWEEN g.Min_Mark AND g.Max_Mark ORDER BY g.Grade DESC, CASE WHEN g.Grade >= 8 THEN s.Name ELSE s.Marks END;