{"id":950,"date":"2024-12-14T19:37:25","date_gmt":"2024-12-14T12:37:25","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=950"},"modified":"2024-12-15T21:15:42","modified_gmt":"2024-12-15T14:15:42","slug":"postgresql-aggregate-functions","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/","title":{"rendered":"PostgreSQL Aggregate Functions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about PostgreSQL aggregate functions including <code>AVG<\/code>, <code>COUNT<\/code>, <code>MAX<\/code>, <code>MIN<\/code>, and <code>SUM<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-postgresql-aggregate-functions'>Introduction to PostgreSQL Aggregate Functions <a href=\"#introduction-to-postgresql-aggregate-functions\" class=\"anchor\" id=\"introduction-to-postgresql-aggregate-functions\" title=\"Anchor for Introduction to PostgreSQL Aggregate Functions\">#<\/a><\/h2>\n\n\n\n<p>Aggregate functions in PostgreSQL are remarkably simple. They take a set of values and return a single value, making your data analysis tasks a breeze.<\/p>\n\n\n\n<p>PostgreSQL supports standard aggregate functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-min\/\" target=\"_blank\" rel=\"noreferrer noopener\">MIN<\/a><\/code> function returns the minimum value in a set of values.<\/li>\n\n\n\n<li>The <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-max\/\" target=\"_blank\" rel=\"noreferrer noopener\">MAX<\/a><\/code> function returns the maximum value in a set of values.<\/li>\n\n\n\n<li>The <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/\" target=\"_blank\" rel=\"noreferrer noopener\">SUM<\/a><\/code> function returns the total of values.<\/li>\n\n\n\n<li>The <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-avg\/\" target=\"_blank\" rel=\"noreferrer noopener\">AVG<\/a><\/code> function returns the average of values.<\/li>\n\n\n\n<li>The <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/\" target=\"_blank\" rel=\"noreferrer noopener\">COUNT<\/a><\/code> function returns the number of values in a set.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='basic-syntax'>Basic Syntax <a href=\"#basic-syntax\" class=\"anchor\" id=\"basic-syntax\" title=\"Anchor for Basic Syntax\">#<\/a><\/h3>\n\n\n\n<p>The following shows how to use an aggregate function with a column in a table:<\/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>\n  aggregate_function (column1)\n<span class=\"hljs-keyword\">FROM<\/span>\n  <span class=\"hljs-built_in\">table_name<\/span>;<\/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, the <code>aggregate_function<\/code> will apply to all values in <code>column1<\/code> from the <code>table_name<\/code> and return a single value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='using-with-the-group-by-clause'>Using with the GROUP BY Clause <a href=\"#using-with-the-group-by-clause\" class=\"anchor\" id=\"using-with-the-group-by-clause\" title=\"Anchor for Using with the GROUP BY Clause\">#<\/a><\/h3>\n\n\n\n<p>When using the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/\">GROUP BY<\/a><\/code> clause with aggregate functions, you can calculate summarized values of groups to get a deeper insight into your data:<\/p>\n\n\n<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\">SELECT<\/span>\n  aggregate_function (column1),\n  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  column2;<\/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>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The <code>GROUP BY<\/code> clause groups values in <code>column2<\/code> into groups.<\/li>\n\n\n\n<li>The <code>aggregate_function<\/code> calculates a single value from each group&#8217;s values in <code>column1<\/code>.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id='using-in-having-clause'>Using in HAVING Clause <a href=\"#using-in-having-clause\" class=\"anchor\" id=\"using-in-having-clause\" title=\"Anchor for Using in HAVING Clause\">#<\/a><\/h3>\n\n\n\n<p>The <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/\">HAVING<\/a><\/code> clause, when used with an aggregate function, gives you the flexibility to filter groups by the aggregated value, putting you in complete control of your data analysis:<\/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  aggregate_function (column1),\n  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  column2\n<span class=\"hljs-keyword\">HAVING<\/span>\n  aggregate_function (column1) <span class=\"hljs-keyword\">operator<\/span> expression;<\/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<h2 class=\"wp-block-heading\" id='postgresql-aggregate-function-examples'>PostgreSQL Aggregate Function Examples <a href=\"#postgresql-aggregate-function-examples\" class=\"anchor\" id=\"postgresql-aggregate-function-examples\" title=\"Anchor for PostgreSQL Aggregate Function Examples\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll take the <code>products<\/code> table from the <code>inventory<\/code> database to demonstrate the aggregate functions.<\/p>\n\n\n\n<p>The <code>products<\/code> table contains information about various products such as their names, prices, brands, categories, and safety stocks.<\/p>\n\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 example uses the <code>COUNT<\/code> function to return the number of products in the <code>products<\/code> table:<\/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  COUNT(product_id)\n<span class=\"hljs-keyword\">FROM<\/span>\n  products;<\/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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIENPVU5UKHByb2R1Y3RfaWQpIEZST00gcHJvZHVjdHM7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<br><\/a><\/p>\n\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\"> count\n<span class=\"hljs-comment\">-------<\/span>\n    <span class=\"hljs-number\">25<\/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>To find the number of products for each brand, you group the products by <code>brand_id<\/code> and count the number of products in each:<\/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  brand_id,\n  COUNT(product_id) <span class=\"hljs-keyword\">AS<\/span> product_count\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  brand_id\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  product_count <span class=\"hljs-keyword\">DESC<\/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><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGJyYW5kX2lkLCBDT1VOVChwcm9kdWN0X2lkKSBBUyBwcm9kdWN0X2NvdW50IEZST00gcHJvZHVjdHMgR1JPVVAgQlkgYnJhbmRfaWQgT1JERVIgQlkgcHJvZHVjdF9jb3VudCBERVNDOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<br><\/a><\/p>\n\n\n\n<p>Output:<\/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\"> brand_id | product_count\n<span class=\"hljs-comment\">----------+---------------<\/span>\n        <span class=\"hljs-number\">2<\/span> |             <span class=\"hljs-number\">6<\/span>\n        <span class=\"hljs-number\">1<\/span> |             <span class=\"hljs-number\">6<\/span>\n        <span class=\"hljs-number\">7<\/span> |             <span class=\"hljs-number\">4<\/span>\n        <span class=\"hljs-number\">5<\/span> |             <span class=\"hljs-number\">3<\/span>\n        <span class=\"hljs-number\">6<\/span> |             <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-number\">9<\/span> |             <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-number\">8<\/span> |             <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-number\">3<\/span> |             <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-number\">4<\/span> |             <span class=\"hljs-number\">1<\/span>\n       <span class=\"hljs-number\">10<\/span> |             <span class=\"hljs-number\">1<\/span><\/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>If you want to return the brand name instead of the id, you can <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-inner-join\/\">join<\/a> the <code>products<\/code> with the <code>brands<\/code> table:<\/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\"><span class=\"hljs-keyword\">SELECT<\/span>\n  brand_name,\n  COUNT(product_id) <span class=\"hljs-keyword\">AS<\/span> product_count\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n  <span class=\"hljs-keyword\">JOIN<\/span> brands <span class=\"hljs-keyword\">USING<\/span> (brand_id)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  brand_name\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  product_count <span class=\"hljs-keyword\">DESC<\/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<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGJyYW5kX25hbWUsIENPVU5UKHByb2R1Y3RfaWQpIEFTIHByb2R1Y3RfY291bnQgRlJPTSBwcm9kdWN0cyBKT0lOIGJyYW5kcyBVU0lORyAoYnJhbmRfaWQpIEdST1VQIEJZIGJyYW5kX25hbWUgT1JERVIgQlkgcHJvZHVjdF9jb3VudCBERVNDOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<br><\/a><\/p>\n\n\n\n<p>Output:<\/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\"> brand_name | product_count\n<span class=\"hljs-comment\">------------+---------------<\/span>\n Apple      |             <span class=\"hljs-number\">6<\/span>\n Samsung    |             <span class=\"hljs-number\">6<\/span>\n Dell       |             <span class=\"hljs-number\">4<\/span>\n Sony       |             <span class=\"hljs-number\">3<\/span>\n Lenovo     |             <span class=\"hljs-number\">1<\/span>\n Microsoft  |             <span class=\"hljs-number\">1<\/span>\n LG         |             <span class=\"hljs-number\">1<\/span>\n Xiaomi     |             <span class=\"hljs-number\">1<\/span>\n HP         |             <span class=\"hljs-number\">1<\/span>\n Huawei     |             <span class=\"hljs-number\">1<\/span><\/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<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 find the average price of smartphones with <code>category_id 3<\/code>:<\/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\"><span class=\"hljs-keyword\">SELECT<\/span>\n  AVG(price) <span class=\"hljs-keyword\">AS<\/span> average_price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  category_id = <span class=\"hljs-number\">3<\/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<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIEFWRyhwcmljZSkgQVMgYXZlcmFnZV9wcmljZSBGUk9NIHByb2R1Y3RzIFdIRVJFIGNhdGVnb3J5X2lkID0gMzs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<br><\/a><\/p>\n\n\n\n<p>Output:<\/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\">     average_price\n<span class=\"hljs-comment\">-----------------------<\/span>\n <span class=\"hljs-number\">1121.4185714285714286<\/span><\/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>To round the average price to a number with two digits after the decimal point, you can use the <code>ROUND()<\/code> function:<\/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\"><span class=\"hljs-keyword\">SELECT<\/span>\n  ROUND(AVG(price), <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">AS<\/span> average_price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  category_id = <span class=\"hljs-number\">3<\/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<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIFJPVU5EKEFWRyhwcmljZSksIDIpIEFTIGF2ZXJhZ2VfcHJpY2UgRlJPTSBwcm9kdWN0cyBXSEVSRSBjYXRlZ29yeV9pZCA9IDM7\" 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-13\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> average_price\n<span class=\"hljs-comment\">---------------<\/span>\n       <span class=\"hljs-number\">1121.42<\/span><\/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>You can group the products by category and compute the average price of products in each:<\/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\"><span class=\"hljs-keyword\">SELECT<\/span>\n  category_name,\n  ROUND(AVG(price), <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">AS<\/span> average_price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n  <span class=\"hljs-keyword\">JOIN<\/span> categories <span class=\"hljs-keyword\">USING<\/span> (category_id)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  category_name\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  average_price <span class=\"hljs-keyword\">DESC<\/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<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGNhdGVnb3J5X25hbWUsIFJPVU5EKEFWRyhwcmljZSksIDIpIEFTIGF2ZXJhZ2VfcHJpY2UgRlJPTSBwcm9kdWN0cyBKT0lOIGNhdGVnb3JpZXMgVVNJTkcgKGNhdGVnb3J5X2lkKSBHUk9VUCBCWSBjYXRlZ29yeV9uYW1lIE9SREVSIEJZIGF2ZXJhZ2VfcHJpY2UgREVTQzs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<br><\/a><\/p>\n\n\n\n<p>Output:<\/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\">category_name | average_price\n<span class=\"hljs-comment\">---------------+---------------<\/span>\n Televisions   |       <span class=\"hljs-number\">2499.99<\/span>\n Laptops       |       <span class=\"hljs-number\">1449.99<\/span>\n Desktops      |       <span class=\"hljs-number\">1149.99<\/span>\n Smartphones   |       <span class=\"hljs-number\">1121.42<\/span>\n Tablets       |        <span class=\"hljs-number\">899.99<\/span>\n Audio Systems |        <span class=\"hljs-number\">849.99<\/span>\n Wearables     |        <span class=\"hljs-number\">374.99<\/span>\n Accessories   |        <span class=\"hljs-number\">224.99<\/span><\/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<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> function to find the lowest price of products with <code>category_id 3<\/code>:<\/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  MIN(price)\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span> category_id = <span class=\"hljs-number\">3<\/span>;<\/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\/?q=U0VMRUNUIE1JTihwcmljZSkgRlJPTSBwcm9kdWN0cyBXSEVSRSBjYXRlZ29yeV9pZCA9IDM7\" 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\">  min\n<span class=\"hljs-comment\">--------<\/span>\n <span class=\"hljs-number\">799.99<\/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>To find the lowest price of products in each category, you can use the <code>GROUP BY<\/code> clause to group products by category and apply the <code>MIN<\/code> function to each group:<\/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  category_name,\n  MIN(price)\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n  <span class=\"hljs-keyword\">JOIN<\/span> categories <span class=\"hljs-keyword\">USING<\/span> (category_id)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  category_name\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  category_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\/?q=U0VMRUNUIGNhdGVnb3J5X25hbWUsIE1JTihwcmljZSkgRlJPTSBwcm9kdWN0cyBKT0lOIGNhdGVnb3JpZXMgVVNJTkcgKGNhdGVnb3J5X2lkKSBHUk9VUCBCWSBjYXRlZ29yeV9uYW1lIE9SREVSIEJZIGNhdGVnb3J5X25hbWU7\" 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\">category_name |   min\n<span class=\"hljs-comment\">---------------+---------<\/span>\n Accessories   |  <span class=\"hljs-number\">199.99<\/span>\n Audio Systems |  <span class=\"hljs-number\">399.99<\/span>\n Desktops      |  <span class=\"hljs-number\">999.99<\/span>\n Laptops       | <span class=\"hljs-number\">1299.99<\/span>\n Smartphones   |  <span class=\"hljs-number\">799.99<\/span>\n Tablets       |  <span class=\"hljs-number\">699.99<\/span>\n Televisions   | <span class=\"hljs-number\">1999.99<\/span>\n Wearables     |  <span class=\"hljs-number\">349.99<\/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<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 <code>SELECT<\/code> statement uses the <code>MAX()<\/code> function to find the highest price of products with <code>category_id 3<\/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  MAX(price)\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  category_id = <span class=\"hljs-number\">3<\/span>;<\/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\/?q=U0VMRUNUIE1BWChwcmljZSkgRlJPTSBwcm9kdWN0cyBXSEVSRSBjYXRlZ29yeV9pZCA9IDM7\" 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\">   max\n<span class=\"hljs-comment\">---------<\/span>\n <span class=\"hljs-number\">1799.99<\/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>To find the highest price of products in each category, you can use the <code>GROUP BY<\/code> clause to group products by category and apply the <code>MAX<\/code> function to each group:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" 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  category_name,\n  MAX(price)\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n  <span class=\"hljs-keyword\">JOIN<\/span> categories <span class=\"hljs-keyword\">USING<\/span> (category_id)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  category_name\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  category_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><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\/?q=U0VMRUNUIGNhdGVnb3J5X25hbWUsIE1BWChwcmljZSkgRlJPTSBwcm9kdWN0cyBKT0lOIGNhdGVnb3JpZXMgVVNJTkcgKGNhdGVnb3J5X2lkKSBHUk9VUCBCWSBjYXRlZ29yeV9uYW1lIE9SREVSIEJZIGNhdGVnb3J5X25hbWU7\" 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-23\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> category_name |   max\n<span class=\"hljs-comment\">---------------+---------<\/span>\n Accessories   |  <span class=\"hljs-number\">249.99<\/span>\n Audio Systems | <span class=\"hljs-number\">1299.99<\/span>\n Desktops      | <span class=\"hljs-number\">1299.99<\/span>\n Laptops       | <span class=\"hljs-number\">1599.99<\/span>\n Smartphones   | <span class=\"hljs-number\">1799.99<\/span>\n Tablets       | <span class=\"hljs-number\">1099.99<\/span>\n Televisions   | <span class=\"hljs-number\">2999.99<\/span>\n Wearables     |  <span class=\"hljs-number\">399.99<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><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 safety stocks for all products:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" 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(safety_stock)\n<span class=\"hljs-keyword\">FROM<\/span>\n  products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><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\/?q=U0VMRUNUIFNVTShzYWZldHlfc3RvY2spIEZST00gcHJvZHVjdHM7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<br><\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" 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\">425<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><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>To calculate the total safety stock for each brand, you can group the products by brand and apply the <code>SUM<\/code> function to each group:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" 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  brand_name,\n  SUM(safety_stock) <span class=\"hljs-keyword\">AS<\/span> total_safety_stock\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n  <span class=\"hljs-keyword\">JOIN<\/span> brands <span class=\"hljs-keyword\">USING<\/span> (brand_id)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  brand_name\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  total_safety_stock <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><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\/?q=U0VMRUNUIGJyYW5kX25hbWUsIFNVTShzYWZldHlfc3RvY2spIEFTIHRvdGFsX3NhZmV0eV9zdG9jayBGUk9NIHByb2R1Y3RzIEpPSU4gYnJhbmRzIFVTSU5HIChicmFuZF9pZCkgR1JPVVAgQlkgYnJhbmRfbmFtZSBPUkRFUiBCWSB0b3RhbF9zYWZldHlfc3RvY2sgREVTQzs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<br><\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> brand_name | total_safety_stock\n<span class=\"hljs-comment\">------------+--------------------<\/span>\n Samsung    |                <span class=\"hljs-number\">175<\/span>\n Apple      |                <span class=\"hljs-number\">150<\/span>\n Sony       |                 <span class=\"hljs-number\">40<\/span>\n Huawei     |                 <span class=\"hljs-number\">30<\/span>\n Dell       |                 <span class=\"hljs-number\">20<\/span>\n LG         |                 <span class=\"hljs-number\">10<\/span>\n HP         |                  <span class=\"hljs-number\">0<\/span>\n Lenovo     |                  <span class=\"hljs-number\">0<\/span>\n Xiaomi     |                  <span class=\"hljs-number\">0<\/span>\n Microsoft  |                  <span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><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='using-multiple-postgresql-aggregate-functions-in-the-same-statement'>Using Multiple PostgreSQL Aggregate Functions in the Same Statement <a href=\"#using-multiple-postgresql-aggregate-functions-in-the-same-statement\" class=\"anchor\" id=\"using-multiple-postgresql-aggregate-functions-in-the-same-statement\" title=\"Anchor for Using Multiple PostgreSQL Aggregate Functions in the Same Statement\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses all the <code>MIN<\/code>, <code>MAX<\/code>, <code>AVG<\/code>, <code>COUNT<\/code>, and <code>SUM<\/code> functions to find the min price, max price, average price, product count, and total safety stock of all products:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" 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(price) <span class=\"hljs-keyword\">AS<\/span> min_price,\n  MAX(price) <span class=\"hljs-keyword\">AS<\/span> max_price,\n  ROUND(AVG(price),<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">AS<\/span> average_price,\n  COUNT(product_id) <span class=\"hljs-keyword\">AS<\/span> product_count,\n  SUM(safety_stock) <span class=\"hljs-keyword\">AS<\/span> total_safety_stock\n<span class=\"hljs-keyword\">FROM<\/span>\n  products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><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\/?q=U0VMRUNUIE1JTihwcmljZSkgQVMgbWluX3ByaWNlLCBNQVgocHJpY2UpIEFTIG1heF9wcmljZSwgQVZHKHByaWNlKSBBUyBhdmVyYWdlX3ByaWNlLCBDT1VOVChwcm9kdWN0X2lkKSBBUyBwcm9kdWN0X2NvdW50LCBTVU0oc2FmZXR5X3N0b2NrKSBBUyB0b3RhbF9zYWZldHlfc3RvY2sgRlJPTSBwcm9kdWN0czs%3D\" 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-29\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> min_price | max_price | average_price | product_count | total_safety_stock\n<span class=\"hljs-comment\">-----------+-----------+---------------+---------------+--------------------<\/span>\n    <span class=\"hljs-number\">199.99<\/span> |   <span class=\"hljs-number\">2999.99<\/span> |       <span class=\"hljs-number\">1225.99<\/span> |            <span class=\"hljs-number\">25<\/span> |                <span class=\"hljs-number\">425<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><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='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>An aggregate function accepts a set of values and returns a single value.<\/li>\n\n\n\n<li>PostgreSQL offers standard aggregate functions, including <code>AVG<\/code>, <code>COUNT<\/code>, <code>MAX<\/code>, <code>MIN<\/code>, and <code>SUM<\/code>.<\/li>\n\n\n\n<li>Use an aggregate function with the <code>GROUP BY<\/code> clause to calculate an aggregated value for each group.<\/li>\n<\/ul>\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=\"950\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Aggregate Functions\"\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=\"950\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Aggregate Functions\"\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 about PostgreSQL aggregate functions including AVG, COUNT, MAX, MIN, and SUM. Introduction to PostgreSQL Aggregate Functions # Aggregate functions in PostgreSQL are remarkably simple. They take a set of values and return a single value, making your data analysis tasks a breeze. PostgreSQL supports standard aggregate functions: Basic Syntax [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-950","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 Aggregate Functions<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn about PostgreSQL aggregate functions including AVG, COUNT, MAX, MIN, and SUM.\" \/>\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-aggregate-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL Aggregate Functions\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn about PostgreSQL aggregate functions including AVG, COUNT, MAX, MIN, and SUM.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-15T14:15:42+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-aggregate-functions\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/\",\"name\":\"PostgreSQL Aggregate Functions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-12-14T12:37:25+00:00\",\"dateModified\":\"2024-12-15T14:15:42+00:00\",\"description\":\"In this tutorial, you'll learn about PostgreSQL aggregate functions including AVG, COUNT, MAX, MIN, and SUM.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Aggregate Functions\"}]},{\"@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 Aggregate Functions","description":"In this tutorial, you'll learn about PostgreSQL aggregate functions including AVG, COUNT, MAX, MIN, and SUM.","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-aggregate-functions\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL Aggregate Functions","og_description":"In this tutorial, you'll learn about PostgreSQL aggregate functions including AVG, COUNT, MAX, MIN, and SUM.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2024-12-15T14:15:42+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-aggregate-functions\/","url":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/","name":"PostgreSQL Aggregate Functions","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-12-14T12:37:25+00:00","dateModified":"2024-12-15T14:15:42+00:00","description":"In this tutorial, you'll learn about PostgreSQL aggregate functions including AVG, COUNT, MAX, MIN, and SUM.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Aggregate Functions"}]},{"@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\/950","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=950"}],"version-history":[{"count":13,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/950\/revisions"}],"predecessor-version":[{"id":1001,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/950\/revisions\/1001"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}