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.
Since there's no ID to join the tables directly, we’re using Marks to join the table and match each student to a grade range.
See line: ON Students.Marks BETWEEN Grades.MinMark AND Grades.MaxMark.
I used to think you could only join tables using IDs, but this example shows you can also join using value-based conditions, like a range.
Thought this might help others who are new to SQL like myself!
SELECT IF(Grades.Grade < 8, NULL, Students.Name) AS Name,
Grades.Grade,
Students.Marks
FROM Students JOIN Grades
ON Students.Marks BETWEEN Grades.Min_Mark AND Grades.Max_Mark
ORDER BY Grades.Grade DESC, Students.Name, Students.Marks;
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 →
Since there's no ID to join the tables directly, we’re using Marks to join the table and match each student to a grade range. See line: ON Students.Marks BETWEEN Grades.MinMark AND Grades.MaxMark. I used to think you could only join tables using IDs, but this example shows you can also join using value-based conditions, like a range. Thought this might help others who are new to SQL like myself!
SELECT IF(Grades.Grade < 8, NULL, Students.Name) AS Name, Grades.Grade, Students.Marks FROM Students JOIN Grades ON Students.Marks BETWEEN Grades.Min_Mark AND Grades.Max_Mark ORDER BY Grades.Grade DESC, Students.Name, Students.Marks;