Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 58

COUNT, SUM, AVG, MIN और MAX

एक Dataset, Exact Results

Verified dataset: Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) और Sana (X-B, 88.50, Active)। Total marks = 340.00 और overall average = 85.00।
SELECT COUNT(*) AS students,
       SUM(marks) AS total,
       AVG(marks) AS average,
       MIN(marks) AS minimum,
       MAX(marks) AS maximum
FROM students;
4 | 340.00 | 85.000000 | 74.00 | 91.00

AVG का displayed scale client/type से बदल सकता है; mathematical value 85 है।

COUNT Variants

SELECT COUNT(*) AS all_rows,
       COUNT(marks) AS known_marks,
       COUNT(DISTINCT class_name) AS classes
FROM students;
all_rows = 4 known_marks = 4 classes = 2

COUNT(1) भी rows count करता है, पर COUNT(*) intent clear है। COUNT(DISTINCT expression) distinct non-NULL values गिनता है।

SUM और AVG

SELECT SUM(marks) AS active_total,
       ROUND(AVG(marks), 2) AS active_average
FROM students
WHERE status = 'Active';
active_total = 266.00 active_average = 88.67

AVG non-NULL SUM को non-NULL count से divide करता है। Business rule बिना individual marks पहले round न करें।

MIN और MAX

SELECT MIN(marks) AS lowest,
       MAX(marks) AS highest,
       MIN(full_name) AS first_name,
       MAX(full_name) AS last_name
FROM students;
lowest = 74.00 highest = 91.00 first_name = Aarav last_name = Sana

Text MIN/MAX collation follow करते हैं। Maximum वाली complete student row के लिए ORDER BY + LIMIT/tie policy या subquery join लें; MAX केवल value देता है।

Conditional Aggregation

SELECT
  COUNT(*) AS total,
  SUM(CASE WHEN status = 'Active' THEN 1 ELSE 0 END) AS active,
  SUM(CASE WHEN marks >= 85 THEN 1 ELSE 0 END) AS marks_85_plus
FROM students;
total = 4 active = 3 marks_85_plus = 3

Conditional aggregation one scan में कई metrics बनाता और GROUP BY reports में उपयोगी है।

गलतियाँ और अभ्यास

  • NULL rows count होने पर COUNT(column) लेना।
  • MAX से whole winning row expect करना।
  • Inputs पहले round करना।
  • Implicit text conversion।
  • NULL exclude होने पर SUM/COUNT(*) करना।
  1. Inactive count और average निकालें।
  2. Distinct statuses count करें।
  3. Per class min/max दें।
  4. 90+ marks conditionally count करें।

त्वरित सारांश

  • COUNT के row, non-NULL और distinct forms हैं।
  • SUM total, AVG known mean और MIN/MAX extremes देते हैं।
  • Conditional aggregation multiple metrics बनाता है।
  • NULL, precision, collation और ties matter करते हैं।

Official संदर्भ

References 14 August 2026 को review किए गए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

COUNT(*) और COUNT(column) में क्या अंतर है?
COUNT(*) rows गिनता है। COUNT(column) केवल वे rows गिनता है जहाँ expression NULL नहीं है।
क्या SUM और AVG text columns पर उपयोग करें?
ये numeric aggregates हैं। Implicit text-to-number conversion unsafe है; numeric data numeric columns में रखें।
क्या AVG automatically round करता है?
AVG input type और MySQL precision rules अनुसार numeric result देता है। Required display scale के लिए ROUND लें।
Unique classes कैसे count करें?
COUNT(DISTINCT class_name) लें। NULL उस count में शामिल नहीं होता।
Condition match करने वाली rows कैसे count करें?
MySQL में SUM(condition) या portable SUM(CASE WHEN condition THEN 1 ELSE 0 END) लें।
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.