{"id":572,"date":"2021-11-16T16:26:48","date_gmt":"2021-11-16T16:26:48","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/?page_id=572"},"modified":"2021-11-17T13:08:35","modified_gmt":"2021-11-17T13:08:35","slug":"having-clause","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/advanced-higher\/having-clause\/","title":{"rendered":"HAVING Clause"},"content":{"rendered":"<p><strong>Examples are based on the <a href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/reference\/sample-databases\/\">GoGo Gadgets<\/a> database<\/strong><\/p>\n<p>Aggregate functions (sum, count, max, min, avg) are used to create summarised GROUP BY results.<\/p>\n<p>However these summarised results cannot be restricted in a WHERE clause &#8211; which is used to specify which individual <strong>records<\/strong> are to be included in the results.<\/p>\n<p>The HAVING clause is used to filter the results from aggregate functions summarised GROUP BY.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">-- display total sales for each item\r\nSELECT Item.itemID, SUM(quantity*price) AS &quot;Total Sales&quot;\r\nFROM OrderItem, Item\r\nWHERE OrderItem.ItemID = Item.ItemID\r\nGROUP BY Item.itemID\r\nORDER BY SUM(quantity*price) DESC\r\n<\/pre>\n<table class=\"datatable\">\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>ItemID<\/strong><\/td>\n<td><strong>Total Sales<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>7072<\/td>\n<td>378.0<\/td>\n<\/tr>\n<tr>\n<td><strong>2<\/strong><\/td>\n<td>9605<\/td>\n<td>208.0<\/td>\n<\/tr>\n<tr>\n<td><strong>&#8230;<\/strong><\/td>\n<td>&#8230;<\/td>\n<td>&#8230;<\/td>\n<\/tr>\n<tr>\n<td><strong>42<\/strong><\/td>\n<td>5986<\/td>\n<td>4.0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"brush: sql; highlight: [6]; title: ; notranslate\" title=\"\">-- display items where total sales greater than 200\r\nSELECT Item.itemID, SUM(quantity*price) AS &quot;Total Sales&quot;\r\nFROM OrderItem, Item\r\nWHERE OrderItem.ItemID = Item.ItemID\r\nGROUP BY Item.itemID\r\nHAVING SUM(quantity*price) &gt; 200\r\nORDER BY SUM(quantity*price) DESC\r\n<\/pre>\n<table class=\"datatable\">\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>ItemID<\/strong><\/td>\n<td><strong>Total Sales<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>7072<\/td>\n<td>378.0<\/td>\n<\/tr>\n<tr>\n<td><strong>2<\/strong><\/td>\n<td>9605<\/td>\n<td>208.0<\/td>\n<\/tr>\n<tr>\n<td><strong>3<\/strong><\/td>\n<td>7108<\/td>\n<td>204.0<\/td>\n<\/tr>\n<tr>\n<td><strong>4<\/strong><\/td>\n<td>6584<\/td>\n<td>204.0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Examples are based on the GoGo Gadgets database Aggregate functions (sum, count, max, min, avg) are used to create summarised GROUP BY results. However these summarised results cannot be restricted in a WHERE clause &#8211; which is used to specify which individual records are to be included in the results. The HAVING clause is used to filter the results from aggregate functions summarised GROUP BY. &#8212; display total sales for<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/advanced-higher\/having-clause\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":533,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-572","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/comments?post=572"}],"version-history":[{"count":9,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/572\/revisions"}],"predecessor-version":[{"id":603,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/572\/revisions\/603"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/533"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/media?parent=572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}