SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX() Notes in Hindi & English : SQL एग्रीगेट फंक्शन्स: COUNT(), SUM(), AVG(), MIN(), MAX() और NULL और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX() (SQL एग्रीगेट फंक्शन्स: COUNT(), SUM(), AVG(), MIN(), MAX() और NULL) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: SQL aggregate functions (COUNT, SUM, AVG, MIN, MAX) collapse multiple rows into a single summary value, ignoring NULLs in column arguments while counting all rows under COUNT(*).

अध्याय सार: SQL एग्रीगेट फंक्शन्स (COUNT, SUM, AVG, MIN, MAX) अनेक पंक्तियों को एक संक्षिप्त मान में बदलते हैं; कॉलम आर्ग्युमेंट में ये NULL को अनदेखा करते हैं जबकि COUNT(*) सभी पंक्तियों को गिनता है।

SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX(): Detailed Notes & Architecture (SQL एग्रीगेट फंक्शन्स: COUNT(), SUM(), AVG(), MIN(), MAX() और NULL)

1. The Five Standard Aggregate Functions and Data Type Rules (पाँच मानक एग्रीगेट फंक्शन्स और डेटा टाइप नियम)

[EN] ANSI SQL defines five primary aggregate functions: COUNT (number of rows/values), SUM (arithmetic total), AVG (arithmetic mean), MIN (lowest value), and MAX (highest value). Critically, SUM() and AVG() operate exclusively on numeric columns, whereas COUNT(), MIN(), and MAX() operate smoothly across numbers, strings (using collation order—'A' < 'Z'), and dates (earliest/latest chronological dates).

[HI] ANSI SQL में पाँच प्रमुख एग्रीगेट फंक्शन्स हैं: COUNT (गिनती), SUM (योग), AVG (औसत), MIN (न्यूनतम) और MAX (अधिकतम)। ध्यान रखने योग्य बात यह है कि SUM() और AVG() केवल संख्यात्मक (Numeric) कॉलम पर चलते हैं, जबकि COUNT(), MIN() और MAX() संख्या, टेक्स्ट स्ट्रिंग और दिनांक (Date) तीनों पर कार्य करते हैं।

2. Critical NULL Rules: COUNT(*) vs COUNT(col) and AVG(col) (NULL के महत्वपूर्ण नियम: COUNT(*) बनाम COUNT(col) और AVG(col))

[EN] Aggregate functions obey a crucial NULL rule: `COUNT(*)` counts every row in the table or group—even rows containing all NULLs. In contrast, `COUNT(col)`, `SUM(col)`, `AVG(col)`, `MIN(col)`, and `MAX(col)` completely ignore NULL values! Because `AVG(col)` divides `SUM(col)` by `COUNT(col)` (only non-NULL rows) rather than `COUNT(*)`, `AVG(Bonus)` over `{100, 200, NULL}` returns `(300 / 2) = 150`, NOT `100`! Wrap nullable columns in `COALESCE(Bonus, 0)` when NULLs should count as zero.

[HI] `COUNT(*)` पूरी टेबल की सभी पंक्तियाँ गिनता है—चाहे उनमें NULL मान ही क्यों न हों। इसके विपरीत `COUNT(col)`, `SUM(col)`, `AVG(col)`, `MIN(col)` और `MAX(col)` सभी NULL मानों को पूरी तरह अनदेखा (Ignore) करते हैं! चूँकि `AVG(col)` का मान `SUM(col) / COUNT(col)` होता है, अतः `{100, 200, NULL}` का औसत `300 / 2 = 150` आएगा (100 नहीं)। यदि NULL को 0 मानकर औसत निकालना हो, तो `AVG(COALESCE(col, 0))` लिखना चाहिए।

3. DISTINCT Modifier and Empty Result Set Behavior (DISTINCT का प्रयोग और खाली टेबल (Empty Set) परिणाम)

[EN] Adding `DISTINCT` inside an aggregate eliminates duplicate non-NULL values before computation: e.g., `COUNT(DISTINCT Dept)` counts unique departments and `SUM(DISTINCT Marks)` sums each distinct score once. On an empty table or when `WHERE` matches 0 rows, `COUNT(*)` and `COUNT(col)` return `0`, whereas `SUM`, `AVG`, `MIN`, and `MAX` all return `NULL` (an essential competitive exam distinction!).

[HI] फंक्शन के अंदर `DISTINCT` लिखने से गणना से पहले डुप्लीकेट मान हट जाते हैं, जैसे `COUNT(DISTINCT Dept)` अद्वितीय विभागों की गिनती करता है। यदि टेबल खाली हो या `WHERE` से 0 पंक्तियाँ मिलें, तो `COUNT(*)` व `COUNT(col)` का उत्तर `0` आता है, जबकि `SUM()`, `AVG()`, `MIN()` और `MAX()` का उत्तर `NULL` आता है!

4. Restrictions in WHERE, Mixing Columns, and Nesting (WHERE में प्रतिबंध, कॉलम मिश्रण और नेस्टिंग नियम)

[EN] Aggregates cannot be placed inside a `WHERE` clause (such as `WHERE Salary > AVG(Salary)`) because WHERE runs row-by-row prior to grouping; use a scalar subquery `(SELECT AVG(Salary) FROM Emp)` or `HAVING` instead. Similarly, standard SQL forbids mixing a raw non-aggregated column with an aggregate without `GROUP BY` (e.g., `SELECT Name, MAX(Marks)` fails) and forbids direct aggregate nesting like `MAX(AVG(Salary))` without a subquery or CTE.

[HI] एग्रीगेट फंक्शन्स को कभी भी `WHERE` क्लॉज के अंदर नहीं लिखा जा सकता (जैसे `WHERE Salary > AVG(Salary)` अमान्य है)—इसके लिए सबक्वेरी या `HAVING` उपयोग होता है। इसी तरह बिना `GROUP BY` के सामान्य कॉलम के साथ एग्रीगेट मिलाना (`SELECT Name, MAX(Marks)`) और `MAX(AVG(Salary))` जैसी सीधी नेस्टिंग मानक SQL में वर्जित है।

Syntax, Formula & Technical Example (SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX())

-- Comparing COUNT(*), COUNT(col), AVG(col), and COALESCE on nullable Bonus
SELECT COUNT(*)                    AS TotalEmployees,
       COUNT(Bonus)                AS BonusRecipients,
       COUNT(DISTINCT Dept)        AS UniqueDepartments,
       SUM(Bonus)                  AS TotalBonus,
       AVG(Bonus)                  AS AvgAmongRecipients,
       AVG(COALESCE(Bonus, 0))     AS AvgAcrossAllStaff
FROM Employees;

SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX() Quick Reference & Comparison Table

Aggregate FunctionNULL & Empty Table BehaviorSupported Column Types
COUNT(*)Counts ALL rows including NULLs; returns 0 on empty setRow-level count (independent of column type)
COUNT(col)Ignores NULL values in col; returns 0 on empty/all-NULL setNumeric, String, Date, Boolean
SUM(col) / AVG(col)Ignores NULLs completely; returns NULL on empty/all-NULL setStrictly Numeric types only
MIN(col) / MAX(col)Ignores NULLs; returns NULL on empty/all-NULL setNumeric, String (collation), Date

SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX() Top 20 Bilingual MCQs with Answers

Q1. What is the fundamental difference between `COUNT(*)` and `COUNT(Bonus)`?

(`COUNT(*)` और `COUNT(Bonus)` के बीच मुख्य तकनीकी अंतर क्या है?)

(A) COUNT(Bonus) throws an error if Bonus has NULLs

(B) COUNT(*) counts every row including NULLs; COUNT(Bonus) counts only rows where Bonus is NOT NULL

(C) COUNT(*) counts columns while COUNT(Bonus) counts rows

(D) Both return identical counts in every table

Correct Answer: (B)

[EN] COUNT(*) measures total row cardinality, whereas COUNT(col) skips NULL entries in that column.

[HI] COUNT(*) पूरी टेबल की सभी पंक्तियाँ (NULL सहित) गिनता है, जबकि COUNT(Bonus) केवल गैर-NULL Bonus वाली पंक्तियाँ गिनता है।

Q2. A table has 4 rows with Scores `(10, 20, NULL, 30)`. What does `SELECT AVG(Score) FROM Test` return?

(एक टेबल की 4 पंक्तियों में Score मान `(10, 20, NULL, 30)` हैं। `SELECT AVG(Score) FROM Test` क्या मान देगा?)

(A) 15 (calculated as 60 / 4)

(B) NULL

(C) 0

(D) 20 (calculated as 60 / 3, ignoring NULL)

Correct Answer: (D)

[EN] AVG() ignores NULL completely, dividing SUM(60) by COUNT(Score)=3 rather than COUNT(*)=4.

[HI] AVG() फंक्शन NULL को अनदेखा करता है, इसलिए 60 / 3 = 20 उत्तर आएगा (60 / 4 = 15 नहीं)।

Q3. On an empty table with 0 rows, what do `SELECT COUNT(*), SUM(Salary)` return?

(0 पंक्तियों वाली खाली टेबल पर `SELECT COUNT(*), SUM(Salary)` चलाने से क्या परिणाम मिलता है?)

(A) COUNT(*) = 0, SUM(Salary) = NULL

(B) COUNT(*) = 0, SUM(Salary) = 0

(C) 0 rows returned (empty table)

(D) COUNT(*) = NULL, SUM(Salary) = NULL

Correct Answer: (A)

[EN] COUNT returns 0 when 0 rows match, whereas SUM, AVG, MIN, and MAX return NULL.

[HI] खाली टेबल पर COUNT हमेशा 0 देता है, जबकि SUM, AVG, MIN और MAX का मान NULL आता है।

Q4. Which SQL aggregate functions can validly operate on VARCHAR (string) and DATE columns?

(इनमें से कौन से एग्रीगेट फंक्शन्स VARCHAR (स्ट्रिंग) और DATE कॉलम पर भी सफलतापूर्वक चलते हैं?)

(A) COUNT(), MIN(), and MAX()

(B) All five: COUNT, SUM, AVG, MIN, MAX

(C) COUNT() only

(D) SUM() and AVG() only

Correct Answer: (A)

[EN] MIN/MAX use collation or chronological order on strings/dates, while SUM/AVG require numeric types.

[HI] MIN(), MAX() और COUNT() टेक्स्ट व तारीख पर भी चलते हैं; SUM() और AVG() केवल संख्याओं के लिए हैं।

Q5. Given Dept values `('IT', 'HR', NULL, 'IT', 'Sales', NULL)`, what does `COUNT(DISTINCT Dept)` return?

(Dept मान `('IT', 'HR', NULL, 'IT', 'Sales', NULL)` होने पर `COUNT(DISTINCT Dept)` क्या लौटाएगा?)

(A) 3 ('IT', 'HR', 'Sales')

(B) 6

(C) 4 (including NULL)

(D) 2

Correct Answer: (A)

[EN] COUNT(DISTINCT col) discards both duplicate values ('IT') and all NULL values.

[HI] COUNT(DISTINCT Dept) डुप्लीकेट ('IT') और NULL दोनों को हटाकर केवल 3 अद्वितीय विभाग गिनता है।

Q6. Why does `SELECT Name, MAX(Salary) FROM Emp` fail in standard SQL?

(मानक SQL में `SELECT Name, MAX(Salary) FROM Emp` त्रुटि क्यों देता है?)

(A) MAX() cannot be used on Salary

(B) You cannot combine an ungrouped column (Name) with an aggregate function (MAX) without GROUP BY

(C) Name must be placed after MAX(Salary)

(D) SELECT allows only one column with MAX()

Correct Answer: (B)

[EN] Without GROUP BY, MAX(Salary) reduces the table to 1 row while Name has N rows, making output ambiguous.

[HI] बिना GROUP BY के एक सामान्य कॉलम (Name) और एग्रीगेट (MAX) को एक साथ प्रिंट नहीं किया जा सकता।

Q7. Why is `SELECT Name FROM Emp WHERE Salary > AVG(Salary)` invalid SQL?

(`SELECT Name FROM Emp WHERE Salary > AVG(Salary)` क्वेरी अमान्य क्यों है?)

(A) Name cannot appear with AVG()

(B) AVG(Salary) always returns a string

(C) Aggregate functions are forbidden inside WHERE because WHERE evaluates before aggregation

(D) '>' cannot compare against AVG()

Correct Answer: (C)

[EN] Rewrite using a scalar subquery: `WHERE Salary > (SELECT AVG(Salary) FROM Emp)`.

[HI] WHERE पंक्ति-दर-पंक्ति पहले चलता है इसलिए उसमें सीधे एग्रीगेट नहीं आ सकता; इसके लिए सबक्वेरी लिखनी होती है।

Q8. Given column X with values `(5, 5, 10, NULL, 10)`, what does `SELECT SUM(DISTINCT X)` return?

(कॉलम X में मान `(5, 5, 10, NULL, 10)` होने पर `SELECT SUM(DISTINCT X)` का उत्तर क्या होगा?)

(A) 15 (5 + 10)

(B) 20

(C) 30 (5 + 5 + 10 + 10)

(D) NULL

Correct Answer: (A)

[EN] DISTINCT retains only unique non-NULL values {5, 10}, whose sum is 15.

[HI] DISTINCT केवल अद्वितीय गैर-NULL मान {5, 10} लेता है, जिनका योग 15 होगा।

Q9. When is `AVG(col)` strictly guaranteed to equal `SUM(col) / COUNT(*)`?

(`AVG(col)` का मान `SUM(col) / COUNT(*)` के बिल्कुल बराबर कब होगा?)

(A) Always, in every table

(B) Only when values are negative

(C) Only when all values are distinct

(D) Only when `col` contains zero NULL values (and table is non-empty)

Correct Answer: (D)

[EN] AVG(col) = SUM(col) / COUNT(col); this matches SUM(col) / COUNT(*) only if COUNT(col) == COUNT(*).

[HI] जब कॉलम में एक भी NULL मान न हो (तभी COUNT(col) और COUNT(*) बराबर होंगे)।

Q10. How do you ensure `NULL` bonuses count as `0` when calculating the company-wide average bonus?

(कंपनी का कुल औसत बोनस निकालते समय NULL को 0 मानकर औसत कैसे निकाला जाए?)

(A) SUM(Bonus) / COUNT(Bonus)

(B) AVG(Bonus IS NULL)

(C) AVG(Bonus)

(D) AVG(COALESCE(Bonus, 0))

Correct Answer: (D)

[EN] COALESCE(Bonus, 0) substitutes 0 for NULL before AVG() averages across all employees.

[HI] COALESCE(Bonus, 0) प्रत्येक NULL मान को 0 में बदल देता है, जिससे सभी कर्मचारियों की संख्या भाग में आती है।

Q11. What does `SELECT MIN(City) FROM Branch` return for Cities `('Udaipur', 'Ajmer', 'Kota')`?

(`('Udaipur', 'Ajmer', 'Kota')` में से `SELECT MIN(City) FROM Branch` क्या लौटाएगा?)

(A) 'Udaipur'

(B) 'Kota'

(C) Error: MIN requires integer

(D) 'Ajmer'

Correct Answer: (D)

[EN] On text columns, MIN() returns the lexicographically smallest string ('Ajmer').

[HI] टेक्स्ट कॉलम पर MIN() वर्णमाला के अनुसार सबसे पहला मान ('Ajmer') लौटाता है।

Q12. What does `SELECT COUNT(NULL) FROM Employees` return if Employees has 50 rows?

(यदि Employees में 50 पंक्तियाँ हैं, तो `SELECT COUNT(NULL) FROM Employees` क्या देगा?)

(A) 50

(B) 1

(C) NULL

(D) 0

Correct Answer: (D)

[EN] Because the constant expression NULL is null on every single row, COUNT() ignores all 50 rows and returns 0.

[HI] COUNT() को दिया गया आर्ग्युमेंट हर पंक्ति में NULL होने के कारण कोई मान नहीं गिना जाएगा और उत्तर 0 आएगा।

Q13. In SQL, what does `COUNT(1)` return compared to `COUNT(*)` on a 25-row table full of NULL columns?

(सभी NULL कॉलम वाली 25 पंक्तियों की टेबल पर `COUNT(1)` और `COUNT(*)` क्या लौटाते हैं?)

(A) Both return 25

(B) COUNT(1) returns 1; COUNT(*) returns 25

(C) COUNT(1) returns 0; COUNT(*) returns 25

(D) COUNT(1) returns 25; COUNT(*) returns 0

Correct Answer: (A)

[EN] Literal `1` is non-NULL for every row, so modern optimizers treat COUNT(1) and COUNT(*) identically.

[HI] स्थिरांक `1` प्रत्येक पंक्ति के लिए गैर-NULL है, इसलिए दोनों ही सभी 25 पंक्तियों की गिनती करेंगे।

Q14. What does `SELECT SUM(Bonus) FROM Staff` return if Staff has 10 rows and EVERY row has `Bonus = NULL`?

(यदि Staff की सभी 10 पंक्तियों में `Bonus` का मान `NULL` हो, तो `SELECT SUM(Bonus)` क्या लौटाएगा?)

(A) NULL

(B) 10

(C) 0

(D) DivisionError

Correct Answer: (A)

[EN] With zero non-NULL values to sum, SUM(), AVG(), MIN(), and MAX() all evaluate to NULL.

[HI] एक भी गैर-NULL मान न होने पर SUM(), AVG(), MIN() और MAX() चारों NULL लौटाते हैं।

Q15. Can standard ANSI SQL execute `SELECT MAX(SUM(Sales)) FROM Orders GROUP BY Region` directly without a subquery?

(क्या मानक ANSI SQL में बिना सबक्वेरी के सीधे `SELECT MAX(SUM(Sales)) FROM Orders GROUP BY Region` चलाया जा सकता है?)

(A) Yes — when ORDER BY is omitted

(B) No — standard SQL forbids nesting aggregate functions directly; use a subquery or CTE

(C) Yes — supported in every SQL dialect identically

(D) Only if MIN is used instead of MAX

Correct Answer: (B)

[EN] Most SQL dialects (MySQL, PostgreSQL, SQL Server) forbid nested aggregates, requiring a subquery or `ORDER BY SUM(Sales) DESC LIMIT 1`.

[HI] अधिकांश मानक SQL इंजन एग्रीगेट के अंदर एग्रीगेट की सीधी नेस्टिंग की अनुमति नहीं देते; इसके लिए सबक्वेरी या CTE चाहिए।

Q16. Which statement is NOT true about SQL aggregate functions?

(SQL एग्रीगेट फंक्शन्स के संदर्भ में कौन सा कथन असत्य (NOT true) है?)

(A) COUNT(*) counts rows even if all columns in the row are NULL

(B) MIN() and MAX() ignore NULL values automatically

(C) SUM() and AVG() require numeric arguments

(D) AVG(col) treats NULL values as 0 and includes them in the denominator

Correct Answer: (D)

[EN] AVG() completely skips NULLs; it does NOT convert NULL to 0 unless wrapped in COALESCE.

[HI] AVG() कभी भी NULL को 0 मानकर हर (Denominator) में शामिल नहीं करता, बल्कि NULL को अनदेखा कर देता है।

Q17. Within SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX(), which aggregate function directly maps to: "Counts ALL rows including NULLs; returns 0 on empty set"?

(SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX() में कौन सा Aggregate Function सीधे "Counts ALL rows including NULLs; returns 0 on empty set" से संबंधित है?)

(A) MIN(col) / MAX(col)

(B) COUNT(*)

(C) SUM(col) / AVG(col)

(D) COUNT(col)

Correct Answer: (B)

[EN] In SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX(), 'COUNT(*)' handles 'Counts ALL rows including NULLs; returns 0 on empty set' (Row-level count (independent of column type)).

[HI] SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX() में 'COUNT(*)' का कार्य/विवरण 'Counts ALL rows including NULLs; returns 0 on empty set' (Row-level count (independent of column type)) है।

Q18. Which option accurately describes 'COUNT(col)' in SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX()?

(SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX() के संदर्भ में 'COUNT(col)' की सही विशेषता कौन सी है?)

(A) Ignores NULLs; returns NULL on empty/all-NULL set

(B) Ignores NULL values in col; returns 0 on empty/all-NULL set

(C) Ignores NULLs completely; returns NULL on empty/all-NULL set

(D) Counts ALL rows including NULLs; returns 0 on empty set

Correct Answer: (B)

[EN] 'COUNT(col)' provides 'Ignores NULL values in col; returns 0 on empty/all-NULL set' — key point: Numeric, String, Date, Boolean.

[HI] 'COUNT(col)' का मुख्य विवरण 'Ignores NULL values in col; returns 0 on empty/all-NULL set' है (Numeric, String, Date, Boolean)।

Q19. In SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX(), identify the correct parameter for the specification: "Ignores NULLs completely; returns NULL on empty/all-NULL set".

(SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX() में "Ignores NULLs completely; returns NULL on empty/all-NULL set" विवरण किस विकल्प के लिए सही है?)

(A) MIN(col) / MAX(col)

(B) COUNT(col)

(C) COUNT(*)

(D) SUM(col) / AVG(col)

Correct Answer: (D)

[EN] 'SUM(col) / AVG(col)' corresponds to 'Ignores NULLs completely; returns NULL on empty/all-NULL set' (Strictly Numeric types only).

[HI] 'SUM(col) / AVG(col)' का संबंध 'Ignores NULLs completely; returns NULL on empty/all-NULL set' (Strictly Numeric types only) से है।

Q20. Considering SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX(), what key behavior defines 'MIN(col) / MAX(col)'?

(SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX() में 'MIN(col) / MAX(col)' की मुख्य भूमिका या मान क्या है?)

(A) Ignores NULL values in col; returns 0 on empty/all-NULL set

(B) Counts ALL rows including NULLs; returns 0 on empty set

(C) Ignores NULLs; returns NULL on empty/all-NULL set

(D) Ignores NULLs completely; returns NULL on empty/all-NULL set

Correct Answer: (C)

[EN] 'MIN(col) / MAX(col)' represents 'Ignores NULLs; returns NULL on empty/all-NULL set' (Numeric, String (collation), Date).

[HI] 'MIN(col) / MAX(col)' का अर्थ/मान 'Ignores NULLs; returns NULL on empty/all-NULL set' है (Numeric, String (collation), Date)।

SQL Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX() — Exam FAQs

Q1: Is `COUNT(1)` faster than `COUNT(*)` in modern SQL databases?

क्या आधुनिक SQL डेटाबेस में `COUNT(1)` लिखना `COUNT(*)` से तेज़ होता है?

[EN] No—that is a myth. Modern query optimizers in MySQL, PostgreSQL, SQL Server, and Oracle treat `COUNT(*)` and `COUNT(1)` identically, using the smallest available non-null index.

[HI] नहीं—यह एक भ्रम है। आधुनिक डेटाबेस ऑप्टिमाइज़र `COUNT(*)` और `COUNT(1)` दोनों को बिल्कुल एक समान निष्पादित करते हैं।

Q2: Why did `SUM(Amount)` return NULL instead of 0 when no rows matched my WHERE condition?

मेरी WHERE शर्त से कोई पंक्ति न मिलने पर `SUM(Amount)` ने 0 के बजाय NULL क्यों दिया?

[EN] By SQL standard, only COUNT returns 0 on an empty set; SUM, AVG, MIN, and MAX return NULL. Wrap it as `COALESCE(SUM(Amount), 0)` if you need 0.

[HI] SQL मानक के अनुसार खाली सेट पर केवल COUNT ही 0 देता है; SUM, AVG, MIN और MAX हमेशा NULL देते हैं। 0 पाने के लिए `COALESCE(SUM(Amount), 0)` लिखें।

Q3: Why does `AVG(col)` give a different answer from `SUM(col) / COUNT(*)`?

`AVG(col)` और `SUM(col) / COUNT(*)` के उत्तर कभी-कभी अलग क्यों आते हैं?

[EN] Whenever the column contains NULL values, `COUNT(*)` counts NULL rows in the denominator while `AVG(col)` uses `COUNT(col)`, which ignores NULLs.

[HI] जब कॉलम में NULL मान हों, तब `COUNT(*)` उन्हें भी हर में गिन लेता है जबकि `AVG(col)` केवल गैर-NULL मानों की संख्या से भाग देता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)