{"id":484,"date":"2009-12-27T12:45:02","date_gmt":"2009-12-27T12:45:02","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=484"},"modified":"2024-01-05T19:06:24","modified_gmt":"2024-01-06T02:06:24","slug":"mysql-count","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/","title":{"rendered":"MySQL COUNT() Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the MySQL <code>COUNT()<\/code> function to return the number of rows in a table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the MySQL COUNT() function<\/h2>\n\n\n\n<p>The <code>COUNT()<\/code> function is an <a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/\">aggregate function<\/a> that returns the number of rows in a table. The <code>COUNT()<\/code> function allows you to count all rows or only rows that match a specified condition.<\/p>\n\n\n\n<p>The <code>COUNT()<\/code> function has three forms: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>COUNT(*)<\/code> <\/li>\n\n\n\n<li><code>COUNT(expression)<\/code> <\/li>\n\n\n\n<li><code>COUNT(DISTINCT expression)<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">COUNT(*) function<\/h3>\n\n\n\n<p>The <code>COUNT(*)<\/code> function returns the number of rows in a result set returned by a <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">SELECT<\/a><\/code> statement. The <code>COUNT(*)<\/code> returns the number of rows including duplicate, non-NULL and <code>NULL<\/code> rows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">COUNT(expression)<\/h3>\n\n\n\n<p>The <code>COUNT(expression)<\/code> returns the number of rows that do not contain <code>NULL<\/code> values as the result of the expression.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">COUNT(DISTINCT expression)<\/h3>\n\n\n\n<p>The <code>COUNT(DISTINCT expression)<\/code> returns the number of distinct rows that do not contain <code>NULL<\/code> values as the result of the expression.<\/p>\n\n\n\n<p>The return type of the <code>COUNT()<\/code> function is <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-int\/\">BIGINT<\/a><\/code>. The <code>COUNT()<\/code>&nbsp; function returns 0 if there is no matching row found.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL COUNT() function illustration<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up a sample table<\/h3>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a table<\/a> called <code>count_demos<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> count_demos (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    val <span class=\"hljs-built_in\">INT<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert-multiple-rows\/\">insert some rows<\/a> into the <code>count_demos<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> count_demos(val) \n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-number\">1<\/span>),(<span class=\"hljs-number\">1<\/span>),(<span class=\"hljs-number\">2<\/span>),(<span class=\"hljs-number\">2<\/span>),(<span class=\"hljs-literal\">NULL<\/span>),(<span class=\"hljs-number\">3<\/span>),(<span class=\"hljs-number\">4<\/span>),(<span class=\"hljs-literal\">NULL<\/span>),(<span class=\"hljs-number\">5<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">query data<\/a> from the <code>count_demos<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> count_demos;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8088\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-sample-table.png\" alt=\"\" width=\"98\" height=\"200\"><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL COUNT(*) example<\/h3>\n\n\n\n<p>The following statement uses the <code>COUNT(*)<\/code> function to return all rows from the <code>count_demos<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(*) <span class=\"hljs-keyword\">FROM<\/span> count_demos;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"84\" height=\"39\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-star-example.png\" alt=\"MySQL COUNT star example\" class=\"wp-image-8089\"\/><\/figure>\n\n\n\n<p>This example uses the <code>COUNT(*)<\/code> function with a <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-where\/\">WHERE<\/a><\/code> clause to specify a condition to count only rows whose value in the column\u00a0<code>val<\/code> is 2:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(*) \n<span class=\"hljs-keyword\">FROM<\/span> count_demos\n<span class=\"hljs-keyword\">WHERE<\/span> val = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"85\" height=\"38\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-star-with-a-WHERE-clause.png\" alt=\"MySQL COUNT star with a WHERE clause\" class=\"wp-image-8090\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL COUNT(expression) example<\/h3>\n\n\n\n<p>If you specify the <code>val<\/code> column in the <code>COUNT()<\/code> function, the <code>COUNT()<\/code> function will count only rows with non-NULL values in the <code>val<\/code> column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(val) \n<span class=\"hljs-keyword\">FROM<\/span> count_demos;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"92\" height=\"36\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-expr-example.png\" alt=\"\" class=\"wp-image-8091\"\/><\/figure>\n\n\n\n<p>Notice that two <code>NULL<\/code> values are not counted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL COUNT(DISTINCT expression) example<\/h3>\n\n\n\n<p>This example uses <code>COUNT(DISTINCT expression)<\/code> to count non-NULL and distinct values in the column <code>val<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(<span class=\"hljs-keyword\">DISTINCT<\/span> val)\n<span class=\"hljs-keyword\">FROM<\/span> count_demos;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"198\" height=\"40\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-DISTINCT-expr-example.png\" alt=\"MySQL COUNT DISTINCT expr example\" class=\"wp-image-8092\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL COUNT() function practical examples<\/h2>\n\n\n\n<p>We&#8217;ll use the <code>products<\/code> table from the <a href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a> for the next examples:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/products.svg\" alt=\"\" class=\"wp-image-10788\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using MySQL COUNT(*) function with a GROUP BY example<\/h3>\n\n\n\n<p>The <code>COUNT(*)<\/code> function is often used with a <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-group-by\/\">GROUP BY<\/a><\/code> clause to return the number of elements in each group.<\/p>\n\n\n\n<p>For example, this statement uses the <code>COUNT()<\/code> function with the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-group-by\/\"><code>GROUP BY<\/code><\/a> clause to return the number of products in each product line:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    productLine, \n    <span class=\"hljs-keyword\">COUNT<\/span>(*)\n<span class=\"hljs-keyword\">FROM<\/span>\n    products\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> productLine;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"161\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-with-GROUP-BY-clause-example.png\" alt=\"MySQL COUNT with GROUP BY clause example\" class=\"wp-image-8098\"\/><\/figure>\n\n\n\n<p>Similarly, this example uses the <code>COUNT(*)<\/code> function to find the number of products supplied by each vendor:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    productVendor, \n    <span class=\"hljs-keyword\">COUNT<\/span>(*)\n<span class=\"hljs-keyword\">FROM<\/span>\n    products\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> productVendor\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(*) <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"224\" height=\"239\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-with-GROUP-BY-clause-example-2.png\" alt=\"\" class=\"wp-image-8099\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-with-GROUP-BY-clause-example-2.png 224w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-with-GROUP-BY-clause-example-2-187x200.png 187w\" sizes=\"auto, (max-width: 224px) 100vw, 224px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using MySQL COUNT(*) with a HAVING clause example<\/h3>\n\n\n\n<p>To find vendors who supply at least 9 products, you use the <code>COUNT(*)<\/code> function in the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-having\/\">HAVING<\/a><\/code> clause as shown in the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    productVendor, \n    <span class=\"hljs-keyword\">COUNT<\/span>(*)\n<span class=\"hljs-keyword\">FROM<\/span>\n    products\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> productVendor\n<span class=\"hljs-keyword\">HAVING<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(*) &gt;= <span class=\"hljs-number\">9<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(*) <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"228\" height=\"137\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-with-HAVING-clause-example.png\" alt=\"MySQL COUNT with HAVING clause example\" class=\"wp-image-8100\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-with-HAVING-clause-example.png 228w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-with-HAVING-clause-example-200x120.png 200w\" sizes=\"auto, (max-width: 228px) 100vw, 228px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3) MySQL COUNT IF example<\/h3>\n\n\n\n<p>You can use a control flow expression and functions e.g., <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-if-function\/\">IF<\/a><\/code>, <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-ifnull\/\">IFNULL<\/a><\/code>, and <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/\">CASE<\/a><\/code>&nbsp;in the <code>COUNT()<\/code> function to count rows whose values match a condition.<\/p>\n\n\n\n<p>See the following <code>orders<\/code> table from the sample database:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"144\" height=\"184\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/orders.png\" alt=\"\" class=\"wp-image-8094\"\/><\/figure>\n\n\n\n<p>The following query use <code>COUNT()<\/code> with <code>IF<\/code> function to find the number of canceled, on hold, and disputed orders from the <code>orders<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">COUNT<\/span>(<span class=\"hljs-keyword\">IF<\/span>(<span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Cancelled'<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-literal\">NULL<\/span>)) <span class=\"hljs-string\">'Cancelled'<\/span>,\n    <span class=\"hljs-keyword\">COUNT<\/span>(<span class=\"hljs-keyword\">IF<\/span>(<span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'On Hold'<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-literal\">NULL<\/span>)) <span class=\"hljs-string\">'On Hold'<\/span>,\n    <span class=\"hljs-keyword\">COUNT<\/span>(<span class=\"hljs-keyword\">IF<\/span>(<span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Disputed'<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-literal\">NULL<\/span>)) <span class=\"hljs-string\">'Disputed'<\/span>\n<span class=\"hljs-keyword\">FROM<\/span>\n    orders;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>IF()<\/code>\u00a0function returns 1 if the order&#8217;s status is canceled, on hold, or disputed, otherwise, it returns <code>NULL<\/code>.<\/p>\n\n\n\n<p>The <code>COUNT<\/code> function only counts 1, not NULL values, therefore, the query returns the number of orders based on the corresponding status.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"276\" height=\"39\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-IF-example.png\" alt=\"MySQL COUNT IF example\" class=\"wp-image-8101\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-IF-example.png 276w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-IF-example-200x28.png 200w\" sizes=\"auto, (max-width: 276px) 100vw, 276px\" \/><\/figure>\n\n\n\n<p>In this tutorial, you have learned various techniques to count the number of rows in a table using the MySQL <code>COUNT<\/code> function.<\/p>\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=\"484\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/\"\n\t\t\t\tdata-post-title=\"MySQL COUNT() 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=\"484\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/\"\n\t\t\t\tdata-post-title=\"MySQL COUNT() 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>This tutorial shows you how to use the MySQL COUNT function to count the number rows in a table.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":509,"menu_order":1,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-484","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL COUNT<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the MySQL COUNT function to count the number of rows in a table that match specified conditions.\" \/>\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.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL COUNT\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the MySQL COUNT function to count the number of rows in a table that match specified conditions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-06T02:06:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-sample-table.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-count\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-count\\\/\",\"name\":\"MySQL COUNT\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-count\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-count\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/MySQL-COUNT-sample-table.png\",\"datePublished\":\"2009-12-27T12:45:02+00:00\",\"dateModified\":\"2024-01-06T02:06:24+00:00\",\"description\":\"This tutorial shows you how to use the MySQL COUNT function to count the number of rows in a table that match specified conditions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-count\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-count\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-count\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/MySQL-COUNT-sample-table.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/MySQL-COUNT-sample-table.png\",\"width\":98,\"height\":200},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-count\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Aggregate Functions\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL COUNT() Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?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":"MySQL COUNT","description":"This tutorial shows you how to use the MySQL COUNT function to count the number of rows in a table that match specified conditions.","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.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/","og_locale":"en_US","og_type":"article","og_title":"MySQL COUNT","og_description":"This tutorial shows you how to use the MySQL COUNT function to count the number of rows in a table that match specified conditions.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-06T02:06:24+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-sample-table.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/","url":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/","name":"MySQL COUNT","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-sample-table.png","datePublished":"2009-12-27T12:45:02+00:00","dateModified":"2024-01-06T02:06:24+00:00","description":"This tutorial shows you how to use the MySQL COUNT function to count the number of rows in a table that match specified conditions.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-sample-table.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-COUNT-sample-table.png","width":98,"height":200},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Aggregate Functions","item":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/"},{"@type":"ListItem","position":3,"name":"MySQL COUNT() Function"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=484"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/484\/revisions"}],"predecessor-version":[{"id":14288,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/484\/revisions\/14288"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/509"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}