Learn SQL

Count students in each grade

Let’s revisit the query we wrote to count the number of students.

Let’s say if instead of counting all students, we want to count the number of students in each grade.

This involves grouping students as per their grades, and then apply count on each group.

SQL provides clause GROUP BY to achieve that.

Observe that in the result of above query, there is a row where the grade value is NULL. That’s expected, because even in GROUP BY clause, a NULL grade is still a unique grade. There are students with a NULL grade, so they appear in the results of the query.

If we want to skip the results with NULL grade, we can filter them out by adding a simple WHERE clause.