{"id":73,"date":"2026-03-06T06:21:05","date_gmt":"2026-03-06T06:21:05","guid":{"rendered":"https:\/\/gigz.pk\/sql\/?post_type=lesson&#038;p=73"},"modified":"2026-03-16T18:49:44","modified_gmt":"2026-03-16T18:49:44","slug":"having-clause","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/sql\/lesson\/having-clause\/","title":{"rendered":"\u00a0HAVING Clause"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773566167802\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>HAVING clause<\/strong> is used in SQL to filter groups of data after they have been aggregated. Unlike the <strong>WHERE<\/strong> clause, which filters individual rows, the <strong>HAVING<\/strong> clause works with aggregated data using functions like <code>COUNT()<\/code>, <code>SUM()<\/code>, <code>AVG()<\/code>, <code>MAX()<\/code>, or <code>MIN()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT column1, aggregate_function(column2)<br>FROM table_name<br>GROUP BY column1<br>HAVING condition;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Key Points:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>GROUP BY<\/code> is required to group rows before filtering.<\/li>\n\n\n\n<li>The <code>HAVING<\/code> clause comes <strong>after<\/strong> the <code>GROUP BY<\/code> clause.<\/li>\n\n\n\n<li>Aggregate functions are used inside the <code>HAVING<\/code> condition.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1: Basic HAVING Clause<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose you have a table named <code>Orders<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>CustomerID<\/th><th>OrderAmount<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>500<\/td><\/tr><tr><td>2<\/td><td>200<\/td><\/tr><tr><td>1<\/td><td>300<\/td><\/tr><tr><td>3<\/td><td>1000<\/td><\/tr><tr><td>2<\/td><td>800<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You want to find customers whose total orders exceed 700.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT CustomerID, SUM(OrderAmount) AS TotalOrders<br>FROM Orders<br>GROUP BY CustomerID<br>HAVING SUM(OrderAmount) &gt; 700;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>CustomerID<\/th><th>TotalOrders<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>800<\/td><\/tr><tr><td>2<\/td><td>1000<\/td><\/tr><tr><td>3<\/td><td>1000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2: Using COUNT() with HAVING<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Find products that have been ordered more than 5 times from the <code>OrderDetails<\/code> table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT ProductID, COUNT(*) AS NumberOfOrders<br>FROM OrderDetails<br>GROUP BY ProductID<br>HAVING COUNT(*) &gt; 5;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Important Notes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <strong>WHERE<\/strong> for filtering rows <strong>before aggregation<\/strong>.<\/li>\n\n\n\n<li>Use <strong>HAVING<\/strong> for filtering <strong>after aggregation<\/strong>.<\/li>\n\n\n\n<li>You can combine <code>HAVING<\/code> with <code>WHERE<\/code> for more precise results.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>HAVING clause<\/strong> is a powerful tool for filtering grouped data based on aggregate values. Always remember: <code>WHERE<\/code> filters rows, <code>HAVING<\/code> filters groups.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/sql\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">SQL Foundations Program (SQL-101) > Working with Functions > HAVING Clause<\/span><\/span><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":21,"template":"","class_list":["post-73","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u00a0HAVING Clause - SQL Learning Hub<\/title>\n<meta name=\"description\" content=\"Learn the SQL HAVING clause with examples. Understand how to filter grouped data using GROUP BY and aggregate functions in SQL queries.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/sql\/lesson\/having-clause\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0HAVING Clause - SQL Learning Hub\" \/>\n<meta property=\"og:description\" content=\"Learn the SQL HAVING clause with examples. Understand how to filter grouped data using GROUP BY and aggregate functions in SQL queries.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/sql\/lesson\/having-clause\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Learning Hub\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T18:49:44+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/having-clause\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/having-clause\\\/\",\"name\":\"\u00a0HAVING Clause - SQL Learning Hub\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\"},\"datePublished\":\"2026-03-06T06:21:05+00:00\",\"dateModified\":\"2026-03-16T18:49:44+00:00\",\"description\":\"Learn the SQL HAVING clause with examples. Understand how to filter grouped data using GROUP BY and aggregate functions in SQL queries.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/having-clause\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/having-clause\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/having-clause\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Foundations Program (SQL-101) > Working with Functions > HAVING Clause\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\",\"name\":\"SQL Learning Hub\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u00a0HAVING Clause - SQL Learning Hub","description":"Learn the SQL HAVING clause with examples. Understand how to filter grouped data using GROUP BY and aggregate functions in SQL queries.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/sql\/lesson\/having-clause\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0HAVING Clause - SQL Learning Hub","og_description":"Learn the SQL HAVING clause with examples. Understand how to filter grouped data using GROUP BY and aggregate functions in SQL queries.","og_url":"https:\/\/gigz.pk\/sql\/lesson\/having-clause\/","og_site_name":"SQL Learning Hub","article_modified_time":"2026-03-16T18:49:44+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/sql\/lesson\/having-clause\/","url":"https:\/\/gigz.pk\/sql\/lesson\/having-clause\/","name":"\u00a0HAVING Clause - SQL Learning Hub","isPartOf":{"@id":"https:\/\/gigz.pk\/sql\/#website"},"datePublished":"2026-03-06T06:21:05+00:00","dateModified":"2026-03-16T18:49:44+00:00","description":"Learn the SQL HAVING clause with examples. Understand how to filter grouped data using GROUP BY and aggregate functions in SQL queries.","breadcrumb":{"@id":"https:\/\/gigz.pk\/sql\/lesson\/having-clause\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/sql\/lesson\/having-clause\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/sql\/lesson\/having-clause\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/sql\/"},{"@type":"ListItem","position":2,"name":"SQL Foundations Program (SQL-101) > Working with Functions > HAVING Clause"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/sql\/#website","url":"https:\/\/gigz.pk\/sql\/","name":"SQL Learning Hub","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/sql\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/media?parent=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}