← Back to MySQL Course | Chapter 10: Aggregate Functions & Grouping | Lesson 7 of 7

HAVING Clause

HAVING एक teacher की तरह है जो आपके sort करने के बाद check करता है कि कौन से piles काफी बड़े हैं। यह groups filter करता है, single rows नहीं।
Syntax
sql
SELECT column_name, aggregate_function(column)
FROM table_name
GROUP BY column_name
HAVING aggregate_condition;

Groups Filter करना

HAVING clause grouped data को aggregation हो जाने के बाद filter करता है। क्योंकि WHERE clause COUNT() या SUM() जैसे aggregate functions evaluate नहीं कर सकता, आपको उन aggregate results के आधार पर groups filter करने के लिए HAVING इस्तेमाल करना ज़रूरी है।

उदाहरण: Filtering Groups

sql
CREATE TABLE orders (id INT, customer_id INT);
INSERT INTO orders VALUES (1, 1), (2, 1), (3, 2);
SELECT customer_id, COUNT(*) AS order_count
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 1;

HAVING बनाम WHERE

key difference याद रखें: WHERE individual rows को group होने से पहले filter करता है, जबकि HAVING summarized groups को process हो जाने के बाद filter करता है। इन्हें mix up करना beginners की सबसे common SQL mistakes में से एक है।

उदाहरण: HAVING vs WHERE

sql
CREATE TABLE orders (id INT, region TEXT, total INT);
INSERT INTO orders VALUES (1, 'North', 500), (2, 'North', 50), (3, 'South', 800);
SELECT region, SUM(total) AS region_total
FROM orders
WHERE total > 0
GROUP BY region
HAVING SUM(total) > 300;

कई Conditions के साथ HAVING

आप ठीक वैसे ही जैसे WHERE में करते HAVING clause में कई group filters combine कर सकते हैं। अपने rules connect करने के लिए AND और OR जैसे logical operators इस्तेमाल करें, जैसे एक minimum order count और एक minimum total spend दोनों की माँग करना।

उदाहरण: HAVING with Multiple Conditions

sql
CREATE TABLE orders (id INT, customer_id INT, total INT);
INSERT INTO orders VALUES (1, 1, 100), (2, 1, 200), (3, 2, 50);
SELECT customer_id, COUNT(*) AS order_count, SUM(total) AS spend
FROM orders
GROUP BY customer_id
HAVING COUNT(*) >= 2 AND SUM(total) > 200;

Alias Values के साथ HAVING

MySQL आपको ज़्यादातर databases के standard WHERE clauses के उलट HAVING filter के अंदर अपनी SELECT clause में defined alias column names reference करने देता है। यह पूरी aggregate expression दोहराने के बजाय आपको same name reuse करने देकर आपकी queries readable रखता है।

उदाहरण: HAVING with Alias Values

sql
CREATE TABLE orders (id INT, category TEXT, total INT);
INSERT INTO orders VALUES (1, 'Books', 50), (2, 'Books', 60), (3, 'Toys', 10);
SELECT category, SUM(total) AS category_total
FROM orders
GROUP BY category
HAVING category_total > 100;

Complex Group Limits

HAVING aggregated values पर complex mathematical comparisons handle कर सकता है। यह आपके data में deviations और trends identify करने के लिए excellent है, जैसे उन categories को flag करना जिनकी average price किसी set threshold से ज़्यादा बढ़ी हो।

उदाहरण: Complex Group Limits

sql
CREATE TABLE prices (id INT, category TEXT, price INT);
INSERT INTO prices VALUES (1, 'Books', 10), (2, 'Books', 100), (3, 'Toys', 20);
SELECT category, AVG(price) AS avg_price
FROM prices
GROUP BY category
HAVING AVG(price) > 50;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. WHERE COUNT(*) > 1 इस्तेमाल करना, जो invalid है, जबकि aggregates पर filter HAVING में होना चाहिए।
  2. row filters को WHERE के बजाय HAVING में रखना, query को धीमा बनाते हुए।
  3. GROUP BY से पहले HAVING लिखना, जो एक syntax error है।
चैप्टर सारांश
  • COUNT(), SUM(), AVG(), MIN(), और MAX() जैसे aggregate functions कई rows को एक value में summarize करते हैं।
  • GROUP BY rows को group करता है ताकि aggregates प्रति group calculate किए जा सकें।
  • HAVING aggregation के बाद groups filter करता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.