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.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 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.