MySQL + SQL · Lesson 57
COUNT, SUM, AVG, MIN and MAX
COUNT()
SELECT COUNT(*) FROM students; -- all rows
SELECT COUNT(phone) FROM students; -- non-NULL phones
SELECT COUNT(DISTINCT class) FROM students; -- unique classes
SUM, AVG, MAX, MIN
SELECT SUM(amount) AS total_fee FROM fees;
SELECT AVG(marks) AS avg_marks FROM students;
SELECT MAX(marks) AS topper FROM students;
SELECT MIN(marks) AS lowest FROM students;Using AS gives the summary column a clear name.
Important
COUNT(*) counts all rows including NULLs; COUNT(column) ignores NULLs in that column.
Summary
- COUNT(*) = all rows; COUNT(col) skips NULLs; COUNT(DISTINCT col) = unique.
- SUM/AVG for numbers, MAX/MIN for highest/lowest.