{"id":472,"date":"2024-11-29T08:47:03","date_gmt":"2024-11-29T01:47:03","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=472"},"modified":"2025-01-02T10:18:33","modified_gmt":"2025-01-02T03:18:33","slug":"postgresql-having","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/","title":{"rendered":"PostgreSQL HAVING"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PostgreSQL <code>HAVING<\/code> clause to filter groups returned by the <code>GROUP BY<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-the-postgresql-having-clause'>Getting started with the PostgreSQL HAVING clause <a href=\"#getting-started-with-the-postgresql-having-clause\" class=\"anchor\" id=\"getting-started-with-the-postgresql-having-clause\" title=\"Anchor for Getting started with the PostgreSQL HAVING clause\">#<\/a><\/h2>\n\n\n\n<p>The <code>HAVING<\/code> clause is a powerful tool, allowing you to specify a condition to filter groups that the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/\">GROUP BY<\/a><\/code> clause returns.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>HAVING<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> column1, column2\n<span class=\"hljs-keyword\">FROM<\/span> <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> column1, column2\n<span class=\"hljs-keyword\">HAVING<\/span> condition;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the table name where you want to retrieve the rows in the <code>FROM<\/code> clause.<\/li>\n\n\n\n<li>Second, define one or more columns for grouping in the <code>GROUP BY<\/code> clause.<\/li>\n\n\n\n<li>Third, provide a condition in the <code>HAVING<\/code> clause to filter groups returned by the <code>GROUP BY<\/code> clause. If the condition is true, PostgreSQL includes the group in the result set.<\/li>\n\n\n\n<li>Finally, specify columns (or expressions) you want to include in the final result set in the <code>SELECT<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<p>PostgreSQL evaluates the clauses in the following order:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>FROM<\/code><\/li>\n\n\n\n<li><code>GROUP BY<\/code><\/li>\n\n\n\n<li><code>HAVING<\/code><\/li>\n\n\n\n<li><code>SELECT<\/code><\/li>\n<\/ul>\n\n\n\n<p>Since PostgreSQL evaluates the <code>HAVING<\/code> clause before the <code>SELECT<\/code> clause, column aliases cannot be accessible in the <code>HAVING<\/code> clause.<\/p>\n\n\n\n<p>Unlike the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-where\/\">WHERE<\/a><\/code> clause that filters rows based on individual rows, the <code>HAVING<\/code> clause filters groups based on the results of the <code>GROUP BY<\/code> clause.<\/p>\n\n\n\n<p>This means that PostgreSQL applies the <code>HAVING<\/code> clause after the <code>GROUP BY<\/code> clause has grouped rows, allowing you to filter the groups based on condition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='setting-up-sample-tables'>Setting up sample tables <a href=\"#setting-up-sample-tables\" class=\"anchor\" id=\"setting-up-sample-tables\" title=\"Anchor for Setting up sample tables\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use the following <code>inventories<\/code> table for the demonstration purposes:<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Sample tables<\/summary><pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> warehouses(\n   warehouse_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n   warehouse_name <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span> <span class=\"hljs-keyword\">UNIQUE<\/span> \n);\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> brands(\n   brand_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n   brand_name <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span> <span class=\"hljs-keyword\">UNIQUE<\/span>   \n);\n\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> inventories(\n   id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n   product_name <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n   quantity <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n   warehouse_id <span class=\"hljs-type\">int<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-keyword\">null<\/span>,\n   brand_id <span class=\"hljs-type\">int<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-keyword\">null<\/span>,\n   <span class=\"hljs-keyword\">FOREIGN KEY<\/span> (warehouse_id) <span class=\"hljs-keyword\">REFERENCES<\/span> warehouses(warehouse_id) <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>,\n   <span class=\"hljs-keyword\">FOREIGN KEY<\/span> (brand_id) <span class=\"hljs-keyword\">REFERENCES<\/span> brands(brand_id) <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>\n);\n\n\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> warehouses (warehouse_name) \n<span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'San Jose'<\/span>),\n       (<span class=\"hljs-string\">'San Francisco'<\/span>);\n\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> brands (brand_name) \n<span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'Apple'<\/span>),\n(<span class=\"hljs-string\">'Samsung'<\/span>),\n(<span class=\"hljs-string\">'Google'<\/span>);\n\n\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> inventories (product_name, quantity, warehouse_id, brand_id) \n<span class=\"hljs-keyword\">VALUES<\/span> \n(<span class=\"hljs-string\">'iPhone 15'<\/span>, <span class=\"hljs-number\">55<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>),\n(<span class=\"hljs-string\">'iPhone 14 Pro Max'<\/span>, <span class=\"hljs-number\">15<\/span>,  <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>),\n(<span class=\"hljs-string\">'iPhone 13'<\/span>, <span class=\"hljs-number\">35<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>),\n(<span class=\"hljs-string\">'Galaxy S24'<\/span>, <span class=\"hljs-number\">50<\/span>,  <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>),\n(<span class=\"hljs-string\">'Galaxy Note 23'<\/span>, <span class=\"hljs-number\">40<\/span>,  <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">2<\/span>),\n(<span class=\"hljs-string\">'Galaxy Z Fold 6'<\/span>, <span class=\"hljs-number\">25<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">2<\/span>),\n(<span class=\"hljs-string\">'Galaxy Z Flip 6'<\/span>, <span class=\"hljs-number\">15<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">2<\/span>),\n(<span class=\"hljs-string\">'Pixel 9'<\/span>, <span class=\"hljs-number\">50<\/span>,  <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>),\n(<span class=\"hljs-string\">'Pixel 8 Pro'<\/span>, <span class=\"hljs-number\">25<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>),\n(<span class=\"hljs-string\">'Pixel Fold'<\/span>, <span class=\"hljs-number\">60<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-having-clause-example'>PostgreSQL HAVING clause example <a href=\"#postgresql-having-clause-example\" class=\"anchor\" id=\"postgresql-having-clause-example\" title=\"Anchor for PostgreSQL HAVING clause example\">#<\/a><\/h2>\n\n\n\n<p>The following statement uses the <code>GROUP BY<\/code> clause to provide the product quantity per warehouse:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  warehouse_id,\n  SUM(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  warehouse_id;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It returns two rows that include warehouse id and quantity.<\/p>\n\n\n\n<p>The following statement uses the <code>HAVING<\/code> clause to retrieve only warehouses with a product quantity greater than 200:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  warehouse_id,\n  SUM(quantity) qty\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  warehouse_id\n<span class=\"hljs-keyword\">HAVING<\/span>\n  SUM(quantity) &gt; <span class=\"hljs-number\">200<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> warehouse_id | qty\n<span class=\"hljs-comment\">--------------+-----<\/span>\n            <span class=\"hljs-number\">1<\/span> | <span class=\"hljs-number\">215<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>PostgreSQL evaluates the <code>HAVING<\/code> clause after the <code>SELECT<\/code> clause; you cannot access the <code>qty<\/code> column alias in the <code>HAVING<\/code> clause.<\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, the <code>GROUP BY<\/code> clause summary rows from the <code>inventories<\/code> table into two groups:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>warehouse_id<\/th><th>sum<\/th><th>product_name<\/th><th>quantity<\/th><th>brand_id<\/th><\/tr><\/thead><tbody><tr><td rowspan=\"4\">1<\/td><td rowspan=\"4\">215<\/td><td>iPhone 15<\/td><td>55<\/td><td>1<\/td><\/tr><tr><td>Galaxy S24<\/td><td>50<\/td><td>2<\/td><\/tr><tr><td>Pixel 9<\/td><td>50<\/td><td>3<\/td><\/tr><tr><td>Pixel Fold<\/td><td>60<\/td><td>3<\/td><\/tr><tr><td rowspan=\"6\">2<\/td><td rowspan=\"6\">155<\/td><td>Galaxy Note 23<\/td><td>40<\/td><td>2<\/td><\/tr><tr><td>Galaxy Z Fold 6<\/td><td>25<\/td><td>2<\/td><\/tr><tr><td>Galaxy Z Flip 6<\/td><td>15<\/td><td>2<\/td><\/tr><tr><td>iPhone 14 Pro Max<\/td><td>15<\/td><td>1<\/td><\/tr><tr><td>iPhone 13<\/td><td>35<\/td><td>1<\/td><\/tr><tr><td>Pixel 8 Pro<\/td><td>25<\/td><td>2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Second, the <code>HAVING<\/code> clause filters the groups based on total quantity and includes only the group that makes the condition true:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>warehouse_id<\/strong><\/th><th><strong>sum<\/strong><\/th><th><strong>product_name<\/strong><\/th><th><strong>quantity<\/strong><\/th><th><strong>brand_id<\/strong><\/th><\/tr><\/thead><tbody><tr><td rowspan=\"4\">1<\/td><td rowspan=\"4\">215<\/td><td>iPhone 15<\/td><td>55<\/td><td>1<\/td><\/tr><tr><td>Galaxy S24<\/td><td>50<\/td><td>2<\/td><\/tr><tr><td>Pixel 9<\/td><td>50<\/td><td>3<\/td><\/tr><tr><td>Pixel Fold<\/td><td>60<\/td><td>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Third, the <code>SELECT<\/code> statement retrieves the <code>warehouse_id<\/code> and <code>sum<\/code> columns, granting the column alias:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>warehouse_id<\/strong><\/th><th><strong>qty<\/strong><\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>215<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-having-clause-with-complex-conditions'>PostgreSQL HAVING clause with complex conditions <a href=\"#postgresql-having-clause-with-complex-conditions\" class=\"anchor\" id=\"postgresql-having-clause-with-complex-conditions\" title=\"Anchor for PostgreSQL HAVING clause with complex conditions\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>HAVING<\/code> clause to return the total product quantity for each combination of warehouse and brand and include only brands 1 and 2 and only if the quantity for those brands exceeds 50:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  warehouse_id,\n  brand_id,\n  SUM(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  warehouse_id,\n  brand_id\n<span class=\"hljs-keyword\">HAVING<\/span>\n  brand_id <span class=\"hljs-keyword\">IN<\/span> (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>)\n  <span class=\"hljs-keyword\">AND<\/span> SUM(quantity) &gt; <span class=\"hljs-number\">50<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> warehouse_id | brand_id | sum\n--------------+----------+-----\n            1 |        1 |  55\n            2 |        2 |  80<\/code><\/span><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, the <code>GROUP BY<\/code> returns the total product quantity by warehouse and brand:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> warehouse_id | brand_id | sum\n--------------+----------+-----\n            1 |        1 |  55\n            2 |        3 |  25\n            1 |        3 | 110\n            2 |        2 |  80\n            1 |        2 |  50\n            2 |        1 |  50<\/code><\/span><\/pre>\n\n\n<p>Second, the <code>HAVING<\/code> clause filters the groups by including only <code>brand_id<\/code> 1 and 2 and product quantity more significant than 50:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> warehouse_id | brand_id | sum\n--------------+----------+-----\n            1 |        1 |  55\n            2 |        2 |  80<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the PostgreSQL <code>HAVING<\/code> clause to filter groups that the <code>GROUP BY<\/code> clause returns.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=having\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"472\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL HAVING\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"472\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL HAVING\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the PostgreSQL HAVING clause to filter groups returned by the GROUP BY clause. Getting started with the PostgreSQL HAVING clause # The HAVING clause is a powerful tool, allowing you to specify a condition to filter groups that the GROUP BY clause returns. Here&#8217;s the syntax [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":30,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-472","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL HAVING<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL HAVING clause to filter groups that the GROUP BY returns.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL HAVING\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL HAVING clause to filter groups that the GROUP BY returns.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-02T03:18:33+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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-having\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-having\\\/\",\"name\":\"PostgreSQL HAVING\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-11-29T01:47:03+00:00\",\"dateModified\":\"2025-01-02T03:18:33+00:00\",\"description\":\"In this tutorial, you'll learn how to use the PostgreSQL HAVING clause to filter groups that the GROUP BY returns.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-having\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-having\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-having\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL HAVING\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?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":"PostgreSQL HAVING","description":"In this tutorial, you'll learn how to use the PostgreSQL HAVING clause to filter groups that the GROUP BY returns.","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:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL HAVING","og_description":"In this tutorial, you'll learn how to use the PostgreSQL HAVING clause to filter groups that the GROUP BY returns.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-02T03:18:33+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/","name":"PostgreSQL HAVING","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-11-29T01:47:03+00:00","dateModified":"2025-01-02T03:18:33+00:00","description":"In this tutorial, you'll learn how to use the PostgreSQL HAVING clause to filter groups that the GROUP BY returns.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL HAVING"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=472"}],"version-history":[{"count":3,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/472\/revisions"}],"predecessor-version":[{"id":1348,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/472\/revisions\/1348"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}