Select, Filter and Sort · Problem 10 of 118

The Five Lowest Grades

MediumSelect, Filter and SortSchool data

A teacher wants to see who needs extra help.

Among the enrollments that have a grade (grade is not NULL), return the student_id, section_id and grade of the five lowest grades. Sort by grade, then student_id, then section_id.

Hint 1

Some enrollments have no grade yet. In SQLite, NULL sorts before every number in an ascending sort.

Reference solution

SELECT student_id, section_id, grade
FROM enrollments
WHERE grade IS NOT NULL
ORDER BY grade, student_id, section_id
LIMIT 5;

A missing grade is not a low grade, it is an unknown one. In SQLite, NULL counts as smaller than any number, so an ascending sort puts the ungraded rows first, and LIMIT 5 would return five rows with no grade at all. Other databases place NULLs differently, which is one more reason to filter them out explicitly with IS NOT NULL.