How The Count Function is Used in Your SQL
Assuming we have a list of student names, their birthdays and courses they attend, and we want to find the number of students that attend the Physics course, the statement below would make this possible.
SELECT COUNT(*) FROM students WHERE course = 'Physics';
If there are 50 students taking the physics course then the result would be an integer 50.
You can see the COUNT() function hides the complexity of how this is done. Here is another example with a different expression.
SELECT COUNT(birthday), COUNT(*) FROM students WHERE course = 'Physics';
Assuming we had two students that did not have their birthday records in the database, the result would be:
COUNT_birthday = 48
COUNT_* = 50
What just happened here? This simply tells us that the COUNT() function only counts entries that are not NULL except when you use the “*" as the expression.
When using Compound expressions in the SQL count function, this would be the way to do it.
SELECT COUNT(DISTINCT student_name, birthday) FROM student WHERE course = 'Physics';
This would be results based on whether the users share names, and whether any of the fields listed contains NULL entries.