← Back to PostgreSQL Course | Chapter 7: Aggregations & Grouping | Lesson 3 of 7

HAVING का उपयोग

HAVING aggregation के बाद groups filter करता है, जबकि WHERE इससे पहले rows filter करता है।

In this page:

  1. HAVING
Syntax
sql
SELECT group_column, aggregate_function(column)
FROM table_name
GROUP BY group_column
HAVING aggregate_function(column) > value;

HAVING

Grouping से पहले rows discard करने के लिए WHERE इस्तेमाल करें और सिर्फ SUM(qty) > 10 जैसी एक aggregate condition satisfy करने वाली groups रखने के लिए HAVING। HAVING aggregates reference कर सकता है, लेकिन WHERE नहीं कर सकता। पहले WHERE से filter करना ज़्यादा efficient है।

Note: Row filters के लिए WHERE और सिर्फ aggregate conditions के लिए HAVING इस्तेमाल करें।

उदाहरण: HAVING

sql
CREATE TABLE sales (id INTEGER PRIMARY KEY, item TEXT, qty INTEGER);
INSERT INTO sales VALUES (1,'pen',5),(2,'book',1),(3,'pen',10),(4,'book',3),(5,'lamp',2);
SELECT item, SUM(qty) AS total FROM sales GROUP BY item HAVING SUM(qty) >= 4 ORDER BY item;
SELECT item, COUNT(*) AS n FROM sales WHERE qty > 1 GROUP BY item HAVING COUNT(*) >= 2;

-- Output:
-- item | total
-- book | 4
-- pen | 15
-- item | n
-- pen | 2
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 में एक aggregate इस्तेमाल करना
  2. row-level filters के लिए HAVING इस्तेमाल करना
  3. यह भूल जाना कि HAVING GROUP BY के बाद चलता है
चैप्टर सारांश
  • WHERE rows filter करता है
  • HAVING groups filter करता है
  • Aggregates HAVING में belong करते हैं
  • Speed के लिए जल्दी 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.