{"id":3936,"date":"2025-06-06T20:49:27","date_gmt":"2025-06-06T20:49:27","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3936"},"modified":"2025-06-07T09:58:00","modified_gmt":"2025-06-07T09:58:00","slug":"sql-having","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-having\/","title":{"rendered":"SQL HAVING"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> lets you <strong>filter grouped results<\/strong> after using <code>GROUP BY<\/code>. Think of it as a <code>WHERE<\/code> for aggregated data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Basic Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT column1, aggregate_function(column2)\nFROM table_name\nGROUP BY column1\nHAVING condition_on_aggregate;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Example: Departments with More Than 5 Employees<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT department, COUNT(*) AS total_employees\nFROM employees\nGROUP BY department\nHAVING COUNT(*) &gt; 5;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Only departments with more than 5 employees are shown.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Using Multiple Conditions<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT department, AVG(salary) AS avg_salary\nFROM employees\nGROUP BY department\nHAVING AVG(salary) &gt; 60000 AND COUNT(*) &gt; 3;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Difference Between WHERE and HAVING<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Clause<\/th><th>When It Works<\/th><th>Filters On<\/th><\/tr><\/thead><tbody><tr><td>WHERE<\/td><td>Before grouping<\/td><td>Individual rows<\/td><\/tr><tr><td>HAVING<\/td><td>After grouping (aggregation)<\/td><td>Groups (aggregated results)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83e\udde0 <strong>Quick Recap<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Key Point<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td>Use <code>HAVING<\/code> to filter grouped data after <code>GROUP BY<\/code><\/td><td>Filters groups based on aggregate conditions<\/td><\/tr><tr><td>Conditions usually involve aggregate functions like <code>COUNT()<\/code>, <code>AVG()<\/code>, <code>SUM()<\/code>, etc.<\/td><td>Aggregate functions are required in HAVING conditions<\/td><\/tr><tr><td><code>WHERE<\/code> filters rows <strong>before<\/strong> grouping<\/td><td>Applies to individual rows, not grouped results<\/td><\/tr><tr><td>Use <code>WHERE<\/code> for row-level filtering and <code>HAVING<\/code> for group-level filtering<\/td><td>Both can be combined for precise filtering<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Use <code>HAVING<\/code> when filtering on aggregated data<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>HAVING lets you filter grouped results after using GROUP BY. Think of it as a WHERE for aggregated data. \ud83d\udd39 Basic Syntax \ud83d\udd39 Example: Departments with More Than 5 Employees Only departments with more than 5 employees are shown. \ud83d\udd39 Using Multiple Conditions \ud83d\udd39 Difference Between WHERE and HAVING Clause When It Works Filters On [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3936","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}