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.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.