HAVING Clause
In this page:
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
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
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
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
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
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;
WHERE COUNT(*) > 1इस्तेमाल करना, जो invalid है, जबकि aggregates पर filterHAVINGमें होना चाहिए।- row filters को
WHEREके बजायHAVINGमें रखना, query को धीमा बनाते हुए। GROUP BYसे पहलेHAVINGलिखना, जो एक syntax error है।
COUNT(),SUM(),AVG(),MIN(), औरMAX()जैसे aggregate functions कई rows को एक value में summarize करते हैं।GROUP BYrows को group करता है ताकि aggregates प्रति group calculate किए जा सकें।HAVINGaggregation के बाद groups filter करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: