{"id":430,"date":"2024-11-27T21:20:52","date_gmt":"2024-11-27T14:20:52","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=430"},"modified":"2025-01-02T10:10:04","modified_gmt":"2025-01-02T03:10:04","slug":"postgresql-group-by","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/","title":{"rendered":"PostgreSQL GROUP BY"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use PostgreSQL <code>GROUP BY<\/code> to group rows into groups based on values of one or more columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-postgresql-group-by-clause'>Getting started with PostgreSQL GROUP BY clause <a href=\"#getting-started-with-postgresql-group-by-clause\" class=\"anchor\" id=\"getting-started-with-postgresql-group-by-clause\" title=\"Anchor for Getting started with PostgreSQL GROUP BY clause\">#<\/a><\/h2>\n\n\n\n<p>The PostgreSQL <code>GROUP BY<\/code> clause allows you to group rows into groups based on values of one or more columns. <\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>GROUP BY<\/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;<\/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 from which you want to select data in the <code>FROM<\/code> clause.<\/li>\n\n\n\n<li>Second, provide one or more columns where you want to group values in the <code>GROUP BY<\/code> clause.<\/li>\n\n\n\n<li>Third, list the grouped columns in the <code>SELECT<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<p>It&#8217;s crucial to note that the columns in the <code>SELECT<\/code> clause must appear in the <code>GROUP BY<\/code> clause. <\/p>\n\n\n\n<p>You&#8217;ll encounter an error if you specify a column in the <code>SELECT<\/code> clause that does not appear in the <code>GROUP BY<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='basic-postgresql-group-by-clause-example'>Basic PostgreSQL GROUP BY clause example <a href=\"#basic-postgresql-group-by-clause-example\" class=\"anchor\" id=\"basic-postgresql-group-by-clause-example\" title=\"Anchor for Basic PostgreSQL GROUP BY clause example\">#<\/a><\/h2>\n\n\n\n<p>Suppose we have the following <code>inventories<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>id<\/th><th>product_name<\/th><th>quantity<\/th><th>warehouse_id<\/th><th>brand_id<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>iPhone 15<\/td><td>55<\/td><td>1<\/td><td>1<\/td><\/tr><tr><td>2<\/td><td>iPhone 14 Pro Max<\/td><td>15<\/td><td>2<\/td><td>1<\/td><\/tr><tr><td>3<\/td><td>iPhone 13<\/td><td>35<\/td><td>2<\/td><td>1<\/td><\/tr><tr><td>4<\/td><td>Galaxy S24<\/td><td>50<\/td><td>1<\/td><td>2<\/td><\/tr><tr><td>5<\/td><td>Galaxy Note 23<\/td><td>40<\/td><td>2<\/td><td>2<\/td><\/tr><tr><td>6<\/td><td>Galaxy Z Fold 6<\/td><td>25<\/td><td>2<\/td><td>2<\/td><\/tr><tr><td>7<\/td><td>Galaxy Z Flip 6<\/td><td>15<\/td><td>2<\/td><td>2<\/td><\/tr><tr><td>8<\/td><td>Pixel 9<\/td><td>50<\/td><td>1<\/td><td>3<\/td><\/tr><tr><td>9<\/td><td>Pixel 8 Pro<\/td><td>25<\/td><td>2<\/td><td>3<\/td><\/tr><tr><td>10<\/td><td>Pixel Fold<\/td><td>60<\/td><td>1<\/td><td>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>SQL script to create tables and populate sample data<\/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<p>The following statement uses the <code>GROUP BY<\/code> clause to group rows in the <code>products<\/code> table by the values in the <code>warehouse_id<\/code> column:<\/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<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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=groupby&amp;q=U0VMRUNUIHdhcmVob3VzZV9pZCBGUk9NIGludmVudG9yaWVzIEdST1VQIEJZIHdhcmVob3VzZV9pZDs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/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\"> warehouse_id\n<span class=\"hljs-comment\">--------------<\/span>\n            <span class=\"hljs-number\">2<\/span>\n            <span class=\"hljs-number\">1<\/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>How it works:<\/p>\n\n\n\n<p>First, the <code>FROM<\/code> clause starts examining all rows from the <code>inventories<\/code> table.<\/p>\n\n\n\n<p>Second, the <code>GROUP BY<\/code> clause selects unique values in the <code>warehouse_id<\/code> column. For each <code>warehouse_id<\/code> value, the <code>GROUP BY<\/code> gathers rows into a group. So we have two groups with <code>warehouse_id<\/code> <code>1<\/code> and <code>2<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>warehouse_id<\/th><th>product_name<\/th><th>quantity<\/th><th>brand_id<\/th><\/tr><tr><td rowspan=\"4\">1<\/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>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>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Finally, the <code>SELECT<\/code> clause selects the grouped column <code>warehouse_id<\/code> :<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>warehouse_id<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><\/tr><tr><td>2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The <code>GROUP BY<\/code> clause is more useful when used with aggregate functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='aggregate-functions'>Aggregate functions <a href=\"#aggregate-functions\" class=\"anchor\" id=\"aggregate-functions\" title=\"Anchor for Aggregate functions\">#<\/a><\/h2>\n\n\n\n<p>An aggregate function takes a group of values, performs a calculation, and returns a single value.<\/p>\n\n\n\n<p>Here are the most commonly used aggregate functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>MIN<\/code> function returns the minimum value in a set.<\/li>\n\n\n\n<li><code>MAX<\/code> function returns the maximum value in a set.<\/li>\n\n\n\n<li><code>SUM<\/code> function returns the total of a set.<\/li>\n\n\n\n<li><code>AVG<\/code> function returns the average value of a set.<\/li>\n\n\n\n<li><code>COUNT<\/code> function returns the number of items of a set.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='min-function'>MIN function <a href=\"#min-function\" class=\"anchor\" id=\"min-function\" title=\"Anchor for MIN function\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code>MIN()<\/code> statement to find the lowest quantity in the <code>inventories<\/code> table:<\/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\"><span class=\"hljs-keyword\">SELECT<\/span>\n  MIN(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories;<\/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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=groupby&amp;q=U0VMRUNUIE1JTihxdWFudGl0eSkgRlJPTSBpbnZlbnRvcmllczs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/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\"> min\n<span class=\"hljs-comment\">-----<\/span>\n  <span class=\"hljs-number\">15<\/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>How the query works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the <code>FROM<\/code> clause examines every row in the <code>inventories<\/code> table.<\/li>\n\n\n\n<li>Second, the <code>MIN<\/code> function returns the lowest quantity of all the rows.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='max-function'>MAX Function <a href=\"#max-function\" class=\"anchor\" id=\"max-function\" title=\"Anchor for MAX Function\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>MAX()<\/code> function to find the highest quantity of a product in the inventory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" 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  MAX(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=groupby&amp;q=U0VMRUNUIE1BWChxdWFudGl0eSkgRlJPTSBpbnZlbnRvcmllczs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> max\n<span class=\"hljs-comment\">-----<\/span>\n  <span class=\"hljs-number\">60<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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<h3 class=\"wp-block-heading\" id='sum-function'>SUM Function <a href=\"#sum-function\" class=\"anchor\" id=\"sum-function\" title=\"Anchor for SUM Function\">#<\/a><\/h3>\n\n\n\n<p>The following <code>SELECT<\/code> statement uses the <code>SUM()<\/code> function to calculate the total quantity of all products in the <code>inventories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" 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  SUM(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=groupby&amp;q=U0VMRUNUIFNVTShxdWFudGl0eSkgRlJPTSBpbnZlbnRvcmllczs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> sum\n<span class=\"hljs-comment\">-----<\/span>\n <span class=\"hljs-number\">370<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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<h3 class=\"wp-block-heading\" id='avg-function'>AVG Function <a href=\"#avg-function\" class=\"anchor\" id=\"avg-function\" title=\"Anchor for AVG Function\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code>AVG()<\/code> function to calculate the average quantity of all products in the <code>inventories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" 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  AVG(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=groupby&amp;q=U0VMRUNUIEFWRyhxdWFudGl0eSkgRlJPTSBpbnZlbnRvcmllczs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">         avg\n<span class=\"hljs-comment\">---------------------<\/span>\n <span class=\"hljs-number\">37.0000000000000000<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><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<h3 class=\"wp-block-heading\" id='count-function'>COUNT Function <a href=\"#count-function\" class=\"anchor\" id=\"count-function\" title=\"Anchor for COUNT Function\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code>COUNT()<\/code> function to return the number of rows in the <code>inventories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" 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  COUNT(*)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=groupby&amp;q=U0VMRUNUIENPVU5UKCopIEZST00gaW52ZW50b3JpZXM7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> count\n<span class=\"hljs-comment\">-------<\/span>\n    <span class=\"hljs-number\">10<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><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<h2 class=\"wp-block-heading\" id='using-postgresql-group-by-clause-with-aggregate-functions'>Using PostgreSQL GROUP BY clause with aggregate functions <a href=\"#using-postgresql-group-by-clause-with-aggregate-functions\" class=\"anchor\" id=\"using-postgresql-group-by-clause-with-aggregate-functions\" title=\"Anchor for Using PostgreSQL GROUP BY clause with aggregate functions\">#<\/a><\/h2>\n\n\n\n<p>Here&#8217;s the syntax for using a <code>GROUP BY<\/code> clause with aggregate functions:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" 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  column1,\n  aggregate_function (column2)\n<span class=\"hljs-keyword\">FROM<\/span>\n  <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  column1;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><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<p>First, the <code>GROUP BY<\/code> clause groups the rows from the table into groups based on the <code>column1<\/code>.<\/p>\n\n\n\n<p>Second, the aggregate function performs a calculation and returns a single value for each group:<\/p>\n\n\n\n<p>Third, the <code>SELECT<\/code> clause returns the grouped column (<code>column1<\/code>) and an aggregated value for each group.<\/p>\n\n\n\n<p>For example, the following shows how to calculate the total quantity for each warehouse:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" 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-16\"><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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=groupby&amp;q=U0VMRUNUIHdhcmVob3VzZV9pZCwgU1VNKHF1YW50aXR5KSBGUk9NIGludmVudG9yaWVzIEdST1VQIEJZIHdhcmVob3VzZV9pZDs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> warehouse_id | sum\n<span class=\"hljs-comment\">--------------+-----<\/span>\n            <span class=\"hljs-number\">2<\/span> | <span class=\"hljs-number\">155<\/span>\n            <span class=\"hljs-number\">1<\/span> | <span class=\"hljs-number\">215<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><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 example:<\/p>\n\n\n\n<p>The <code>FROM<\/code> clause starts examining all rows in the <code>inventories<\/code> table.<\/p>\n\n\n\n<p>The <code>GROUP BY<\/code> clause divides all the rows in the inventories 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>The <code>SUM<\/code> function then calculates the total of the quantity for each group.<\/p>\n\n\n\n<p>The <code>SELECT<\/code> clause returns the <code>warehouse_id<\/code> and total quantity for each group:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-postgresql-group-by-with-inner-join'>Using PostgreSQL GROUP BY with INNER JOIN <a href=\"#using-postgresql-group-by-with-inner-join\" class=\"anchor\" id=\"using-postgresql-group-by-with-inner-join\" title=\"Anchor for Using PostgreSQL GROUP BY with INNER JOIN\">#<\/a><\/h2>\n\n\n\n<p>You can use a <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-inner-join\/\">join<\/a> to merge rows of multiple tables and use the <code>GROUP BY<\/code> clause to group rows into groups.<\/p>\n\n\n\n<p>For example, the following query returns the warehouse name and the total quantity of products for each warehouse:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" 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_name,\n  SUM(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> warehouses <span class=\"hljs-keyword\">USING<\/span> (warehouse_id)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  warehouse_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=groupby&amp;q=U0VMRUNUIHdhcmVob3VzZV9uYW1lLCBTVU0ocXVhbnRpdHkpIEZST00gaW52ZW50b3JpZXMgSU5ORVIgSk9JTiB3YXJlaG91c2VzIFVTSU5HICh3YXJlaG91c2VfaWQpIEdST1VQIEJZIHdhcmVob3VzZV9uYW1lOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> warehouse_name | sum\n<span class=\"hljs-comment\">----------------+-----<\/span>\n San Jose       | <span class=\"hljs-number\">215<\/span>\n San Francisco  | <span class=\"hljs-number\">155<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><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>How it works.<\/p>\n\n\n\n<p>First, the <code>FROM<\/code> examines all rows in the <code>inventories<\/code> table.<\/p>\n\n\n\n<p>Next, the <code>INNER JOIN<\/code> merge rows of the <code>inventories<\/code> table with the rows in the <code>warehouses<\/code> table based on the values in the <code>warehouse_id<\/code> column:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>id<\/th><th>product_name<\/th><th>quantity<\/th><th>warehouse_id<\/th><th>brand_id<\/th><th>warehouse_id<\/th><th>warehouse_name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>iPhone 15<\/td><td>55<\/td><td>1<\/td><td>1<\/td><td>1<\/td><td>San Jose<\/td><\/tr><tr><td>2<\/td><td>iPhone 14 Pro Max<\/td><td>15<\/td><td>2<\/td><td>1<\/td><td>2<\/td><td>San Francisco<\/td><\/tr><tr><td>3<\/td><td>iPhone 13<\/td><td>35<\/td><td>2<\/td><td>1<\/td><td>2<\/td><td>San Francisco<\/td><\/tr><tr><td>4<\/td><td>Galaxy S24<\/td><td>50<\/td><td>1<\/td><td>2<\/td><td>1<\/td><td>San Jose<\/td><\/tr><tr><td>5<\/td><td>Galaxy Note 23<\/td><td>40<\/td><td>2<\/td><td>2<\/td><td>2<\/td><td>San Francisco<\/td><\/tr><tr><td>6<\/td><td>Galaxy Z Fold 6<\/td><td>25<\/td><td>2<\/td><td>2<\/td><td>2<\/td><td>San Francisco<\/td><\/tr><tr><td>7<\/td><td>Galaxy Z Flip 6<\/td><td>15<\/td><td>2<\/td><td>2<\/td><td>2<\/td><td>San Francisco<\/td><\/tr><tr><td>8<\/td><td>Pixel 9<\/td><td>50<\/td><td>1<\/td><td>3<\/td><td>1<\/td><td>San Jose<\/td><\/tr><tr><td>9<\/td><td>Pixel 8 Pro<\/td><td>25<\/td><td>2<\/td><td>3<\/td><td>2<\/td><td>San Francisco<\/td><\/tr><tr><td>10<\/td><td>Pixel Fold<\/td><td>60<\/td><td>1<\/td><td>3<\/td><td>1<\/td><td>San Jose<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Then, the <code>GROUP BY<\/code> group merged rows by the values in the <code>warehouse_name<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>warehouse_name<\/th><th>id<\/th><th>product_name<\/th><th>quantity<\/th><th>warehouse_id<\/th><th>brand_id<\/th><\/tr><\/thead><tbody><tr><td rowspan=\"4\">San Jose<\/td><td>1<\/td><td>iPhone 15<\/td><td>55<\/td><td>1<\/td><td>1<\/td><\/tr><tr><td>4<\/td><td>Galaxy S24<\/td><td>50<\/td><td>1<\/td><td>2<\/td><\/tr><tr><td>8<\/td><td>Pixel 9<\/td><td>50<\/td><td>1<\/td><td>3<\/td><\/tr><tr><td>10<\/td><td>Pixel Fold<\/td><td>60<\/td><td>1<\/td><td>3<\/td><\/tr><tr><td rowspan=\"6\">San Francisco<\/td><td>2<\/td><td>iPhone 14 Pro Max<\/td><td>15<\/td><td>2<\/td><td>1<\/td><\/tr><tr><td>3<\/td><td>iPhone 13<\/td><td>35<\/td><td>2<\/td><td>1<\/td><\/tr><tr><td>5<\/td><td>Galaxy Note 23<\/td><td>40<\/td><td>2<\/td><td>2<\/td><\/tr><tr><td>6<\/td><td>Galaxy Z Fold 6<\/td><td>25<\/td><td>2<\/td><td>2<\/td><\/tr><tr><td>7<\/td><td>Galaxy Z Flip 6<\/td><td>15<\/td><td>2<\/td><td>2<\/td><\/tr><tr><td>9<\/td><td>Pixel 8 Pro<\/td><td>25<\/td><td>2<\/td><td>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After that, the <code>SUM<\/code> function calculates the total for each group:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>warehouse_name<\/th><th>sum<\/th><th>id<\/th><th>product_name<\/th><th>quantity<\/th><th>warehouse_id<\/th><th>brand_id<\/th><\/tr><\/thead><tbody><tr><td rowspan=\"4\">San Jose<\/td><td rowspan=\"4\">215<\/td><td>1<\/td><td>iPhone 15<\/td><td>55<\/td><td>1<\/td><td>1<\/td><\/tr><tr><td>4<\/td><td>Galaxy S24<\/td><td>50<\/td><td>1<\/td><td>2<\/td><\/tr><tr><td>8<\/td><td>Pixel 9<\/td><td>50<\/td><td>1<\/td><td>3<\/td><\/tr><tr><td>10<\/td><td>Pixel Fold<\/td><td>60<\/td><td>1<\/td><td>3<\/td><\/tr><tr><td rowspan=\"6\">San Francisco<\/td><td rowspan=\"6\">155<\/td><td>2<\/td><td>iPhone 14 Pro Max<\/td><td>15<\/td><td>2<\/td><td>1<\/td><\/tr><tr><td>3<\/td><td>iPhone 13<\/td><td>35<\/td><td>2<\/td><td>1<\/td><\/tr><tr><td>5<\/td><td>Galaxy Note 23<\/td><td>40<\/td><td>2<\/td><td>2<\/td><\/tr><tr><td>6<\/td><td>Galaxy Z Fold 6<\/td><td>25<\/td><td>2<\/td><td>2<\/td><\/tr><tr><td>7<\/td><td>Galaxy Z Flip 6<\/td><td>15<\/td><td>2<\/td><td>2<\/td><\/tr><tr><td>9<\/td><td>Pixel 8 Pro<\/td><td>25<\/td><td>2<\/td><td>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Finally, the <code>SELECT<\/code> clause returns the warehouse names and total quantity of each:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>warehouse_name<\/th><th>sum<\/th><\/tr><\/thead><tbody><tr><td>San Jose<\/td><td>215<\/td><\/tr><tr><td>San Francisco<\/td><td>200<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='group-by-multiple-columns'>Group by multiple columns <a href=\"#group-by-multiple-columns\" class=\"anchor\" id=\"group-by-multiple-columns\" title=\"Anchor for Group by multiple columns\">#<\/a><\/h2>\n\n\n\n<p>The following statement uses the <code>GROUP BY<\/code> clause to group rows in the <code>inventories<\/code> table by <code>warehouse_id<\/code> and <code>brand_id<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" 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;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=groupby&amp;q=U0VMRUNUIHdhcmVob3VzZV9pZCwgYnJhbmRfaWQsIFNVTShxdWFudGl0eSkgRlJPTSBpbnZlbnRvcmllcyBHUk9VUCBCWSB3YXJlaG91c2VfaWQsIGJyYW5kX2lkOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> warehouse_id | brand_id | sum\n<span class=\"hljs-comment\">--------------+----------+-----<\/span>\n            <span class=\"hljs-number\">1<\/span> |        <span class=\"hljs-number\">1<\/span> |  <span class=\"hljs-number\">55<\/span>\n            <span class=\"hljs-number\">2<\/span> |        <span class=\"hljs-number\">3<\/span> |  <span class=\"hljs-number\">25<\/span>\n            <span class=\"hljs-number\">1<\/span> |        <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-number\">110<\/span>\n            <span class=\"hljs-number\">2<\/span> |        <span class=\"hljs-number\">2<\/span> |  <span class=\"hljs-number\">80<\/span>\n            <span class=\"hljs-number\">1<\/span> |        <span class=\"hljs-number\">2<\/span> |  <span class=\"hljs-number\">50<\/span>\n            <span class=\"hljs-number\">2<\/span> |        <span class=\"hljs-number\">1<\/span> |  <span class=\"hljs-number\">50<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><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 example, the combination of values in the <code>warehouse_id<\/code> and <code>brand_id<\/code> column forms a group. The <code>SUM()<\/code> function returns the total of product quantity for each group.<\/p>\n\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 <code>GROUP BY<\/code> clause to group rows in an SQL query into summary rows based on one or more columns.<\/li>\n\n\n\n<li>Use aggregate functions with the <code>GROUP BY<\/code> clause to calculate a value for each group.<\/li>\n\n\n\n<li>Use <code>JOIN<\/code> with <code>GROUP BY<\/code> to merge rows from multiple table and group rows into groups.<\/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=group-by\"\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=\"430\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL GROUP BY\"\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=\"430\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL GROUP BY\"\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 PostgreSQL GROUP BY to group rows into groups based on values of one or more columns. Getting started with PostgreSQL GROUP BY clause # The PostgreSQL GROUP BY clause allows you to group rows into groups based on values of one or more columns. Here&#8217;s the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":29,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-430","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 GROUP BY<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use PostgreSQL GROUP BY to group rows into groups based on values of one or more columns.\" \/>\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-group-by\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL GROUP BY\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use PostgreSQL GROUP BY to group rows into groups based on values of one or more columns.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-02T03:10:04+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=\"6 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-group-by\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-group-by\\\/\",\"name\":\"PostgreSQL GROUP BY\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-11-27T14:20:52+00:00\",\"dateModified\":\"2025-01-02T03:10:04+00:00\",\"description\":\"In this tutorial, you'll learn how to use PostgreSQL GROUP BY to group rows into groups based on values of one or more columns.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-group-by\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-group-by\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-group-by\\\/#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 GROUP BY\"}]},{\"@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 GROUP BY","description":"In this tutorial, you'll learn how to use PostgreSQL GROUP BY to group rows into groups based on values of one or more columns.","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-group-by\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL GROUP BY","og_description":"In this tutorial, you'll learn how to use PostgreSQL GROUP BY to group rows into groups based on values of one or more columns.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-02T03:10:04+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/","name":"PostgreSQL GROUP BY","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-11-27T14:20:52+00:00","dateModified":"2025-01-02T03:10:04+00:00","description":"In this tutorial, you'll learn how to use PostgreSQL GROUP BY to group rows into groups based on values of one or more columns.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/#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 GROUP BY"}]},{"@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\/430","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=430"}],"version-history":[{"count":13,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/430\/revisions"}],"predecessor-version":[{"id":1347,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/430\/revisions\/1347"}],"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=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}