The Report

  • + 0 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;