{"id":993,"date":"2024-12-15T21:04:44","date_gmt":"2024-12-15T14:04:44","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=993"},"modified":"2025-01-05T19:46:59","modified_gmt":"2025-01-05T12:46:59","slug":"postgresql-count","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/","title":{"rendered":"PostgreSQL COUNT Aggregate Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you&#8217;ll learn how to use the PostgreSQL <code>COUNT<\/code> aggregate function to count the number of values in a set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-the-postgresql-count-aggregate-function'>Getting Started with the PostgreSQL COUNT Aggregate Function <a href=\"#getting-started-with-the-postgresql-count-aggregate-function\" class=\"anchor\" id=\"getting-started-with-the-postgresql-count-aggregate-function\" title=\"Anchor for Getting Started with the PostgreSQL COUNT Aggregate Function\">#<\/a><\/h2>\n\n\n\n<p>The PostgreSQL <code>COUNT<\/code> aggregate function counts the number of values in a set.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>COUNT<\/code> function:<\/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  COUNT(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>COUNT<\/code> function returns the number of values in <code>column1<\/code> of <code>table_name<\/code>.<\/p>\n\n\n\n<p>If <code>column1<\/code> contains <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-not-null\/\">NULL<\/a><\/code>, the <code>COUNT(column1)<\/code> function does not count <code>NULL<\/code>. In other words, it ignores <code>NULL<\/code> in the result.<\/p>\n\n\n\n<p>If <code>column1<\/code> has no values, the <code>COUNT(column1)<\/code> function returns zero (0). Unlike the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/\">SUM<\/a><\/code> and <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-avg\/\">AVG<\/a><\/code> functions, the <code>COUNT<\/code> function does not return <code>NULL<\/code> if <code>column1<\/code> has no values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='countdistinct-column1'>COUNT(DISTINCT column1) <a href=\"#countdistinct-column1\" class=\"anchor\" id=\"countdistinct-column1\" title=\"Anchor for COUNT(DISTINCT column1)\">#<\/a><\/h3>\n\n\n\n<p>The <code>COUNT<\/code> function, with its <code>DISTINCT<\/code> option, empowers you to count distinct values in a column, offering flexibility in your data analysis:<\/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  COUNT(<span class=\"hljs-keyword\">DISTINCT<\/span> 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-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<ul class=\"wp-block-list\">\n<li>If <code>column1<\/code> has <code>NULL<\/code>, the <code>COUNT(DISTINCT column1)<\/code> will count the <code>NULL<\/code>.<\/li>\n\n\n\n<li>If <code>column1<\/code> has multiple <code>NULL<\/code>s, the <code>COUNT(DISTINCT column1)<\/code> will consider all duplicates and include one <code>NULL<\/code> in the result.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='count'>COUNT(*) <a href=\"#count\" class=\"anchor\" id=\"count\" title=\"Anchor for COUNT(*)\">#<\/a><\/h3>\n\n\n\n<p>The <code>COUNT(*)<\/code> returns the number of values returned by the <code>SELECT<\/code> statement, including <code>NULL<\/code> and duplicates:<\/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  COUNT(*)\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-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<h3 class=\"wp-block-heading\" id='countcolumn1-vs-countdistinct-column1-vs-count'>COUNT(column1) vs. COUNT(DISTINCT column1) vs. COUNT(*) <a href=\"#countcolumn1-vs-countdistinct-column1-vs-count\" class=\"anchor\" id=\"countcolumn1-vs-countdistinct-column1-vs-count\" title=\"Anchor for COUNT(column1) vs. COUNT(DISTINCT column1) vs. COUNT(*)\">#<\/a><\/h3>\n\n\n\n<p>Let&#8217;s take an example to understand the difference between <code>COUNT(column1)<\/code>, <code>COUNT(DISTINCT column1)<\/code>, and <code>COUNT(*)<\/code>:<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a table<\/a> <code>t<\/code> with one column <code>id<\/code>: <\/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\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> t(id <span class=\"hljs-type\">INT<\/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>Second, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\">insert rows<\/a> into the <code>t<\/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\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> t(id) <span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-number\">1<\/span>),(<span class=\"hljs-number\">2<\/span>),(<span class=\"hljs-number\">2<\/span>),(<span class=\"hljs-number\">3<\/span>), (<span class=\"hljs-keyword\">NULL<\/span>) \n<span class=\"hljs-keyword\">RETURNING<\/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>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\">  id\n<span class=\"hljs-comment\">------<\/span>\n    <span class=\"hljs-number\">1<\/span>\n    <span class=\"hljs-number\">2<\/span>\n    <span class=\"hljs-number\">2<\/span>\n    <span class=\"hljs-number\">3<\/span>\n <span class=\"hljs-keyword\">NULL<\/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>Third, use the <code>COUNT(id)<\/code>, <code>COUNT(DISTINCT id)<\/code>, and <code>COUNT(*)<\/code> functions:<\/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  COUNT(id) <span class=\"hljs-keyword\">AS<\/span> count_id,\n  COUNT(<span class=\"hljs-keyword\">DISTINCT<\/span> id) <span class=\"hljs-keyword\">AS<\/span> count_distinct_id,\n  COUNT(*) <span class=\"hljs-keyword\">AS<\/span> count_star\n<span class=\"hljs-keyword\">FROM<\/span>\n  t;<\/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=count&amp;q=U0VMRUNUIENPVU5UKGlkKSBBUyBjb3VudF9pZCwgQ09VTlQoRElTVElOQ1QgaWQpIEFTIGNvdW50X2Rpc3RpbmN0X2lkLCBDT1VOVCgqKSBBUyBjb3VudF9zdGFyIEZST00gdDs%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-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> count_id | count_distinct_id | count_star\n<span class=\"hljs-comment\">----------+-------------------+------------<\/span>\n        <span class=\"hljs-number\">4<\/span> |                 <span class=\"hljs-number\">3<\/span> |          <span class=\"hljs-number\">5<\/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>In this result:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>COUNT(id)<\/code> returns four because it ignores <code>NULL<\/code> but includes duplicates.<\/li>\n\n\n\n<li>The <code>COUNT(DISTINCT id)<\/code> returns three (1, 2, 3) because it ignores <code>NULL<\/code> and a duplicate <code>id<\/code> (2).<\/li>\n\n\n\n<li>The <code>COUNT(*)<\/code> returns five because it includes <code>NULL<\/code> and duplicates.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-count-aggregate-function-examples'>PostgreSQL COUNT Aggregate Function Examples <a href=\"#postgresql-count-aggregate-function-examples\" class=\"anchor\" id=\"postgresql-count-aggregate-function-examples\" title=\"Anchor for PostgreSQL COUNT Aggregate Function Examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s explore some examples of using the <code>COUNT<\/code> function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"159\" height=\"254\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png\" alt=\"\" class=\"wp-image-1051\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='counting-rows-in-a-table'>Counting Rows in a Table <a href=\"#counting-rows-in-a-table\" class=\"anchor\" id=\"counting-rows-in-a-table\" title=\"Anchor for Counting Rows in a Table\">#<\/a><\/h3>\n\n\n\n<p>The following <code>SELECT<\/code> statement uses the <code>COUNT()<\/code> function to count the number of products in the <code>products<\/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  COUNT(*)\n<span class=\"hljs-keyword\">FROM<\/span>\n  products;<\/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\/?q=U0VMRUNUIENPVU5UKCopIEZST00gcHJvZHVjdHM7\" 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\"> count\n<span class=\"hljs-comment\">-------<\/span>\n    <span class=\"hljs-number\">25<\/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='counting-values-in-groups'>Counting Values in Groups <a href=\"#counting-values-in-groups\" class=\"anchor\" id=\"counting-values-in-groups\" title=\"Anchor for Counting Values in Groups\">#<\/a><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"378\" height=\"254\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-brands-1.png\" alt=\"\" class=\"wp-image-1048\" srcset=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-brands-1.png 378w, https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products-brands-1-300x202.png 300w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/figure>\n\n\n\n<p>The following <code>SELECT<\/code> statement uses the <code>COUNT()<\/code> function to retrieve product count by brand:<\/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  brand_id,\n  COUNT(product_id)\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;<\/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\/?q=U0VMRUNUIGJyYW5kX2lkLCBDT1VOVChwcm9kdWN0X2lkKSBGUk9NIHByb2R1Y3RzIEdST1VQIEJZIGJyYW5kX2lkOw%3D%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-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> brand_id | count\n<span class=\"hljs-comment\">----------+-------<\/span>\n        <span class=\"hljs-number\">9<\/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\">5<\/span> |     <span class=\"hljs-number\">3<\/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>\n        <span class=\"hljs-number\">6<\/span> |     <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-number\">2<\/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\">1<\/span> |     <span class=\"hljs-number\">6<\/span>\n        <span class=\"hljs-number\">8<\/span> |     <span class=\"hljs-number\">1<\/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>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>GROUP BY<\/code> clause groups the products by <code>brand_id<\/code>.<\/li>\n\n\n\n<li>The <code>COUNT<\/code> function counts the number of products for each group.<\/li>\n<\/ul>\n\n\n\n<p>If you want to get the brand name instead of id, you can <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-inner-join\/\">join<\/a> the <code>products<\/code> table with the <code>brands<\/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  brand_name,\n  COUNT(product_id)\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;<\/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\/?q=U0VMRUNUIGJyYW5kX25hbWUsIENPVU5UKHByb2R1Y3RfaWQpIEZST00gcHJvZHVjdHMgSk9JTiBicmFuZHMgVVNJTkcgKGJyYW5kX2lkKSBHUk9VUCBCWSBicmFuZF9uYW1lOw%3D%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-14\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> brand_name | count\n<span class=\"hljs-comment\">------------+-------<\/span>\n Microsoft  |     <span class=\"hljs-number\">1<\/span>\n HP         |     <span class=\"hljs-number\">1<\/span>\n Sony       |     <span class=\"hljs-number\">3<\/span>\n Huawei     |     <span class=\"hljs-number\">1<\/span>\n Lenovo     |     <span class=\"hljs-number\">1<\/span>\n Samsung    |     <span class=\"hljs-number\">6<\/span>\n LG         |     <span class=\"hljs-number\">1<\/span>\n Dell       |     <span class=\"hljs-number\">4<\/span>\n Xiaomi     |     <span class=\"hljs-number\">1<\/span>\n Apple      |     <span class=\"hljs-number\">6<\/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<h3 class=\"wp-block-heading\" id='using-count-in-having-clause'>Using COUNT in HAVING Clause <a href=\"#using-count-in-having-clause\" class=\"anchor\" id=\"using-count-in-having-clause\" title=\"Anchor for Using COUNT in HAVING Clause\">#<\/a><\/h3>\n\n\n\n<p>The following statement gets the brands that have three or more products:<\/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  brand_name,\n  COUNT(product_id)\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\">HAVING<\/span>\n  COUNT(*) &gt; <span class=\"hljs-number\">3<\/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<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGJyYW5kX25hbWUsIENPVU5UKHByb2R1Y3RfaWQpIEZST00gcHJvZHVjdHMgSk9JTiBicmFuZHMgVVNJTkcgKGJyYW5kX2lkKSBHUk9VUCBCWSBicmFuZF9uYW1lIEhBVklORyBDT1VOVCgqKSA%2BIDM7\" 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-16\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> brand_name | count\n<span class=\"hljs-comment\">------------+-------<\/span>\n Samsung    |     <span class=\"hljs-number\">6<\/span>\n Dell       |     <span class=\"hljs-number\">4<\/span>\n Apple      |     <span class=\"hljs-number\">6<\/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>In this example, the <code>HAVING<\/code> clause filters the brands with the number of products more than 3.<\/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 <code>COUNT(column1)<\/code> to count the number of non-NULL values in <code>column1<\/code>, including duplicates.<\/li>\n\n\n\n<li>Use <code>COUNT(DISTINCT column1)<\/code> to count the number of distinct values in <code>column1<\/code>, including <code>NULL<\/code>.<\/li>\n\n\n\n<li>Use <code>COUNT(*)<\/code> to count the number of values, including <code>NULL<\/code> and duplicates.<\/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=count\"\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=\"993\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL COUNT Aggregate Function\"\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=\"993\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL COUNT Aggregate Function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: In this tutorial, you&#8217;ll learn how to use the PostgreSQL COUNT aggregate function to count the number of values in a set. Getting Started with the PostgreSQL COUNT Aggregate Function # The PostgreSQL COUNT aggregate function counts the number of values in a set. Here&#8217;s the syntax of the COUNT function: In this syntax, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":950,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-993","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 COUNT Aggregate Function<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL COUNT aggregate function to count the number of values in a set.\" \/>\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\/postgresql-count\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL COUNT Aggregate Function\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL COUNT aggregate function to count the number of values in a set.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-05T12:46:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png\" \/>\n\t<meta property=\"og:image:width\" content=\"159\" \/>\n\t<meta property=\"og:image:height\" content=\"254\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\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\\\/postgresql-count\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-count\\\/\",\"name\":\"PostgreSQL COUNT Aggregate Function\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-count\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-count\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products.png\",\"datePublished\":\"2024-12-15T14:04:44+00:00\",\"dateModified\":\"2025-01-05T12:46:59+00:00\",\"description\":\"In this tutorial, you'll learn how to use the PostgreSQL COUNT aggregate function to count the number of values in a set.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-count\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-count\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-count\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products.png\",\"contentUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products.png\",\"width\":159,\"height\":254,\"caption\":\"PostgreSQL Expression Index\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-count\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Aggregate Functions\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL COUNT Aggregate Function\"}]},{\"@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 COUNT Aggregate Function","description":"In this tutorial, you'll learn how to use the PostgreSQL COUNT aggregate function to count the number of values in a set.","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\/postgresql-count\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL COUNT Aggregate Function","og_description":"In this tutorial, you'll learn how to use the PostgreSQL COUNT aggregate function to count the number of values in a set.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-05T12:46:59+00:00","og_image":[{"width":159,"height":254,"url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","type":"image\/png"}],"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\/postgresql-count\/","url":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/","name":"PostgreSQL COUNT Aggregate Function","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/#primaryimage"},"image":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","datePublished":"2024-12-15T14:04:44+00:00","dateModified":"2025-01-05T12:46:59+00:00","description":"In this tutorial, you'll learn how to use the PostgreSQL COUNT aggregate function to count the number of values in a set.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/#primaryimage","url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","contentUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","width":159,"height":254,"caption":"PostgreSQL Expression Index"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-count\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Aggregate Functions","item":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL COUNT Aggregate Function"}]},{"@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\/993","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=993"}],"version-history":[{"count":9,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/993\/revisions"}],"predecessor-version":[{"id":1459,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/993\/revisions\/1459"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/950"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}