Aggregate Functions: avg(), max(), min(), sum(), count(), first(), last()

Aggregate functions कई rows को एक जवाब में निचोड़ती हैं, SUM जोड़ता है, AVG औसत निकालता है, COUNT गिनता है, MAX/MIN चरम पाते हैं, और GROUP BY के साथ जोड़े जाने पर वे पूरी table के लिए एक के बजाय per category एक जवाब देती हैं।

11 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

हज़ार rows से एक number

Meera हर sale देखना नहीं चाहती, वह कुल जोड़ चाहती है। या औसत price। या वह कितने items रखती है। एक number जो पूरी table को summarise करे।

वह एक aggregate function है: यह कई rows लेता है और एक जवाब लौटाता है। और आप इनसे Excel में मिल चुके हैं, SUM, AVERAGE, COUNT, MAX, MIN, अब SQL में, और वे एक महाशक्ति पाती हैं: GROUP BY के साथ जोड़ी जाकर, वे 'सब कुछ के लिए' के बजाय 'per category' जवाब देती हैं।

यहीं एक database एक calculator को पछाड़ता है: एक line दस लाख rows को summarise करती है।

Theory

Data के लिए एक juicer

एक juicer संतरों का ढेर लेता है और एक गिलास रस बनाता है, कई चीज़ें अंदर, एक चीज़ बाहर। Aggregate functions rows के लिए एक juicer हैं: 500 sale rows उंडेलिए, एक कुल जोड़, या एक औसत, या एक count बाहर पाइए। और GROUP BY अलग जगों वाला juicer है: एक गिलास grocery-रस, एक dairy-रस, पूरे ढेर के लिए एक के बजाय per group एक summary।

Practical

Summarise the sales table

-- Whole-table aggregates (one answer each)
SELECT SUM(qty)   AS total_qty,   -- 20+15+30+25+40 = 130
       AVG(price) AS avg_price,   -- (45+120+60+35+30)/5 = 58
       COUNT(*)   AS num_items,   -- 5
       MAX(price) AS costliest,   -- 120
       MIN(price) AS cheapest     -- 30
FROM sales;

-- Per-category with GROUP BY
SELECT category, SUM(qty) AS qty_sold
FROM sales
GROUP BY category;
-- Grocery 50, Beverage 15, Personal 25, Dairy 40

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

GROUP BY और HAVING

अकेला, एक aggregate पूरी table को summarise करता है। GROUP BY जोड़िए और यह हर group को summarise करता है:

SELECT category, SUM(qty) FROM sales GROUP BY category कुल मात्रा per category देता है।

और groups को छानने को? WHERE नहीं कर सकता, यह grouping से पहले rows छानता है। आपको HAVING चाहिए, जो aggregation के बाद छानता है:

... GROUP BY category HAVING SUM(qty) > 30

सिर्फ वे categories रखता है जिनका कुल 30 से ज़्यादा हो। WHERE rows छानता है; HAVING groups छानता है। वह जोड़ी एक क्लासिक exam बिंदु है।

Quiz

एक column में 5 rows हैं: 3 में values हैं, 2 NULL हैं। COUNT(*) और COUNT(column) क्या लौटाते हैं?

  1. COUNT(*) = 5, COUNT(column) = 3 (यह NULLs छोड़ता है)
  2. दोनों 5 लौटाते हैं
  3. दोनों 3 लौटाते हैं
  4. COUNT(*) = 3, COUNT(column) = 5
Show the answer

COUNT(*) = 5, COUNT(column) = 3 (यह NULLs छोड़ता है)

*COUNT() सारी rows गिनता है (5), NULLs समेत। COUNT(column) उस column में सिर्फ non-NULL values गिनता है (3)। यह ठीक Unit 2 वाला Excel COUNT बनाम COUNTA* फ़र्क़ है, SQL में पुनर्जन्म लिया। COUNT()-बनाम-COUNT(col) जाल पूरे SQL unit के सबसे भरोसेमंद exam सवालों में से एक है।

Think first

WHERE या HAVING?

Meera 'वे categories जिनकी बेची गई कुल मात्रा 30 से ज़्यादा हो' चाहती है। वह WHERE SUM(qty) > 30 आज़माती है और एक error पाती है। WHERE यह क्यों नहीं कर सकता, और उसे क्या इस्तेमाल करना चाहिए?

Show the answer

WHERE grouping से पहले अलग rows छानता है, तो यह एक group का SUM नहीं देख सकता (जो उस चरण पर अभी मौजूद ही नहीं), इसलिए error। उसे HAVING चाहिए, जो GROUP BY aggregation के बाद छानता है: GROUP BY category HAVING SUM(qty) > 30। नियम: rows के लिए WHERE, groups/aggregates के लिए HAVING। इन्हें गड्डमड्ड करना नंबर-एक GROUP BY गलती है।

Watch out

Marks कहाँ कटते हैं

*COUNT() सारी rows गिनता है; COUNT(col) NULLs छोड़ता है (COUNTA समांतर)। aggregates छानने को WHERE इस्तेमाल करना, वह HAVING का काम है (WHERE grouping से पहले चलता है)। यह भूलना कि एक aggregate rows को समेटता है एक value में (तो आप एक aggregate के साथ एक सादा column SELECT नहीं कर सकते जब तक वह GROUP BY में न हो)। और ध्यान दीजिए FIRST/LAST** का सीमित/ग़ैर-मानक support है, उन पर मत टिकिए। AVG, SUM, COUNT, MAX, MIN भरोसेमंद पाँच हैं।

Theory

आपने यह पहले किया है, दो बार

SUM, AVG, COUNT, MAX, MIN, ये Unit 2 के Excel lesson के वही functions हैं, और GROUP BY ठीक Consolidate है। आपने उन्हें एक spreadsheet में click किया; आप उन्हें SQL में type करते हैं; idea एक-से हैं। वह हस्तांतरण ही इस subject को spreadsheet-पहले बनाने का पूरा मक़सद है। दो lessons बचे: auto-numbering के लिए sequences, और views। आगे: scalar functions जो हर value को एक-एक करके बदलती हैं।

Summary

Key takeaways

  • Aggregate functions कई rows को एक value में समेटती हैं: SUM, AVG, COUNT, MAX, MIN।
  • COUNT(*) सारी rows गिनता है; COUNT(column) सिर्फ non-NULL values गिनता है (Excel COUNTA बनाम COUNT की तरह)।
  • GROUP BY per group एक aggregate देता है (SUM(qty) per category)।
  • HAVING aggregation के बाद groups छानता है; WHERE उससे पहले rows छानता है।
  • FIRST/LAST का सीमित support है; मुख्य पाँच पर भरोसा कीजिए।
  • याद रखने का hook: एक juicer (कई rows अंदर, एक जवाब बाहर); GROUP BY अलग जग देता है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of SQL and Queries (Single Table only)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Aggregate Functions: avg(), max(), min(), sum(), count(), first(), last() · Data Processing and Analysis (DPA) · Gri-Learn