{"id":10537,"date":"2026-02-11T04:36:11","date_gmt":"2026-02-11T04:36:11","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=10537"},"modified":"2026-02-11T08:27:05","modified_gmt":"2026-02-11T08:27:05","slug":"how-to-get-multiple-counts-in-single-query","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/","title":{"rendered":"How to Get Multiple Counts in Single Query"},"content":{"rendered":"\n<p>MySQL database is commonly used to aggregate data, get summaries and statistics. If you need to get multiple counts from same table, then database developers tend to use separate queries for each count, especially if these counts are conditional. But did you know that you can get multiple counts in single query? In this article, we will learn how to multiple counts in single query.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#Why_Multiple_Counts_are_Needed_in_Single_Query\" >Why Multiple Counts are Needed in Single Query<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#How_to_Get_Multiple_Counts_in_Single_Query\" >How to Get Multiple Counts in Single Query<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#1_Using_SUM_with_CASE\" >1. Using SUM with CASE<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#2_Using_COUNT_with_IF\" >2. Using COUNT with IF<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#3_Using_Sub_query\" >3. Using Sub query<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Multiple_Counts_are_Needed_in_Single_Query\"><\/span>Why Multiple Counts are Needed in Single Query<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Sometimes database developers are required to get multiple counts from one or more tables. Some of these counts may depend on certain conditions while others may depend on some other conditions. Typically, database developers run separate queries to get each count. Here&#8217;s an example to illustrate it. Let us say you have the following sales table, and you want separate sum of orders where product is A and product is B<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; create table sales(id int, product varchar(255),orders int);<br><br><br>mysql&gt; insert into sales(id,product,orders) <br>       values(1, 'A',50),<br>             (2,'B',60),<br>             (3,'A',70),<br>             (4,'B',40),<br>             (5,'C',50);<br><br><br>mysql&gt; select * from sales;<br>+------+---------+--------+<br>| id   | product | orders |<br>+------+---------+--------+<br>|    1 | A       |     50 |<br>|    2 | B       |     60 |<br>|    3 | A       |     70 |<br>|    4 | B       |     40 |<br>|    5 | C       |     50 |<br>+------+---------+--------+<\/pre>\n\n\n\n<p>Typically, you would use separate queries to get the above mentioned sums, as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; select sum(orders) from sales where product='A';<br>+-------------+<br>| sum(orders) |<br>+-------------+<br>|         120 |<br>+-------------+<br><br>mysql&gt; select sum(orders) from sales where product='B';<br>+-------------+<br>| sum(orders) |<br>+-------------+<br>|         100 |<br>+-------------+<\/pre>\n\n\n\n<p>But if your tables are large with many columns and rows, then it can be time consuming to just run one query, let alone running multiple queries. In such cases, it is better to get all sums\/counts using a single query. This is commonly required in <a href=\"https:\/\/ubiq.co\/business-analytics-software-tools\">business analytics &amp; reporting systems<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Get_Multiple_Counts_in_Single_Query\"><\/span>How to Get Multiple Counts in Single Query<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are 3 main ways to get multiple counts in single query &#8211; using SUM with CASE, using COUNT with IF and using sub query. Let us look at each of them in detail. We will learn how to get multiple SUM in single query. If you want to get row count, you can use COUNT() function instead of SUM().<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_SUM_with_CASE\"><\/span>1. Using SUM with CASE<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this solution, we use conditional statements within a <a href=\"https:\/\/www.w3schools.com\/sql\/sql_case.asp\" target=\"_blank\" rel=\"noreferrer noopener\">CASE<\/a> statement to obtain multiple counts. This is a very efficient way to solve the problem, since it scans the table only once. Here is the query to get separate sums of orders where product is A and product is B.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT sum(orders) as total,<br>            SUM(CASE WHEN product = 'A' THEN orders ELSE 0 END) AS A_count,<br>            SUM(CASE WHEN product = 'B' THEN orders ELSE 0 END) AS B_count<br>      FROM sales;<br>+-------+---------+---------+<br>| total | A_count | B_count |<br>+-------+---------+---------+<br>|   270 |     120 |     100 |<br>+-------+---------+---------+<\/pre>\n\n\n\n<p>In the above query, we use two sum() functions, one each for product A and product B. In each sum() function, we use a CASE statement. For each row, the first CASE statement(used for 2nd column) results in <em>orders<\/em> column value if the product column value is A for that row, else it results in 0. <\/p>\n\n\n\n<p>The second CASE statement(used for 3rd column) results in <em>orders<\/em> column value if product column value is B, else it results in 0. The sum() function adds up this value to calculate total sales where product is A and product is B.<\/p>\n\n\n\n<p>If you want to get a row count instead of sum, then modify the query as shown. Instead of using orders column name in your query, use 1 and 0.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT count(*) as total,<br>       SUM(CASE WHEN product = 'A' THEN 1 ELSE 0 END) AS A_count,                        <br>       SUM(CASE WHEN product = 'B' THEN 1 ELSE 0 END) AS B_count<br>       FROM sales;<br>+-------+---------+---------+<br>| total | A_count | B_count |<br>+-------+---------+---------+<br>|     5 |       5 |       2 |<br>+-------+---------+---------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_COUNT_with_IF\"><\/span>2. Using COUNT with IF<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This solution is similar to the above one. In this case, we use IF statement instead of using CASE. For each separate sum, we use IF() condition. If it evaluates to be true, then it returns one value, else it returns another value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;  SELECT sum(orders) AS total,<br>        sum(IF(product='A', orders, NULL)) AS A_count,<br>        sum(IF(product='B', orders, NULL)) AS B_count<br>        FROM sales;<br>+-------+---------+---------+<br>| total | A_count | B_count |<br>+-------+---------+---------+<br>|   270 |     120 |     100 |<br>+-------+---------+---------+<\/pre>\n\n\n\n<p>In the above query, for second column, we use if() function within sum() function. For each row, the IF condition results in orders column value if product=A, else it returns null. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT count(*) AS total,<br>       sum(IF(product='A', 1, NULL)) AS A_count,<br>       sum(IF(product='B', 1, NULL)) AS B_count<br>       FROM sales;<br>+-------+---------+---------+<br>| total | A_count | B_count |<br>+-------+---------+---------+<br>|     5 |       2 |       2 |<br>+-------+---------+---------+<\/pre>\n\n\n\n<p>Similarly, for third column, for each row, the if() function results in orders column value if product=B, else it returns null.<\/p>\n\n\n\n<p>If you need to calculate row count instead of sum of orders column, then replace orders with 1 in the above query, as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<p>These 2 solutions are generally the recommended ways to calculate multiple counts using a single query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_Sub_query\"><\/span>3. Using Sub query<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This method is mentioned here only for the sake of completeness. In this solution, we use separate sub queries to calculate separate counts. It is not as efficient as the above two methods, since it requires the database engine to scan the entire table each time for a sub query. If you have large tables, then this solution will be very slow. So use it only if you are unable to use the above mentioned solutions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;  SELECT sum(orders) AS total,<br>          (select sum(orders) from sales where product='A') AS A_count,<br>          (select sum(orders) from sales where product='B') AS B_count<br>     FROM sales;<br>+-------+---------+---------+<br>| total | A_count | B_count |<br>+-------+---------+---------+<br>|   270 |     120 |     100 |<br>+-------+---------+---------+<\/pre>\n\n\n\n<p>In the above query, we use sub queries to calculate sum of orders column where product=A and product=B.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt how to get multiple counts in single SQL query in MySQL. We have learnt why it is better to use a single query to get multiple counts, instead of using separate ones. We have learnt how to do this using CASE statement, using IF statement and using sub query. It is especially useful if you have a large table and you need to get multiple counts without running separate queries on them. <\/p>\n\n\n\n<p>Both the solutions, using CASE statement and using IF statement require only single table scans, whereas the one using sub queries requires multiple table scans. Depending on your requirement, you can use any of these methods. Please note, you can use these solutions in other database systems too.<\/p>\n\n\n\n<p>Also read:<br><a href=\"https:\/\/ubiq.co\/tech-blog\/top-5-online-resources-to-learn-mysql\/\">Top 5 Online Courses to Learn MySQL<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/mysql-select-top-n-rows-per-group\/\">MySQL Select Top N Rows Per Group<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/enable-remote-access-mysql\/\">How to Enable Remote Access in MySQL<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you may need to get multiple counts in single query in MySQL. Here are the different ways to do this.<\/p>\n","protected":false},"author":1,"featured_media":10570,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324],"tags":[445],"class_list":["post-10537","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql","tag-multiple-counts"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Get Multiple Counts in Single Query - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Sometimes you may need to get multiple counts in single query in MySQL. Here are the different ways to do this.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Get Multiple Counts in Single Query - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Sometimes you may need to get multiple counts in single query in MySQL. Here are the different ways to do this.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-11T04:36:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-11T08:27:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/02\/multiple-counts-single-query-mysql.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"301\" \/>\n\t<meta property=\"og:image:height\" content=\"201\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Get Multiple Counts in Single Query\",\"datePublished\":\"2026-02-11T04:36:11+00:00\",\"dateModified\":\"2026-02-11T08:27:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/\"},\"wordCount\":852,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/multiple-counts-single-query-mysql.jpg?fit=301%2C201&ssl=1\",\"keywords\":[\"multiple counts\"],\"articleSection\":[\"MySQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/\",\"name\":\"How to Get Multiple Counts in Single Query - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/multiple-counts-single-query-mysql.jpg?fit=301%2C201&ssl=1\",\"datePublished\":\"2026-02-11T04:36:11+00:00\",\"dateModified\":\"2026-02-11T08:27:05+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Sometimes you may need to get multiple counts in single query in MySQL. Here are the different ways to do this.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/multiple-counts-single-query-mysql.jpg?fit=301%2C201&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/multiple-counts-single-query-mysql.jpg?fit=301%2C201&ssl=1\",\"width\":301,\"height\":201,\"caption\":\"multiple counts single query mysql\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-get-multiple-counts-in-single-query\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Get Multiple Counts in Single Query\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Get Multiple Counts in Single Query - Ubiq BI","description":"Sometimes you may need to get multiple counts in single query in MySQL. Here are the different ways to do this.","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:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/","og_locale":"en_US","og_type":"article","og_title":"How to Get Multiple Counts in Single Query - Ubiq BI","og_description":"Sometimes you may need to get multiple counts in single query in MySQL. Here are the different ways to do this.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2026-02-11T04:36:11+00:00","article_modified_time":"2026-02-11T08:27:05+00:00","og_image":[{"width":301,"height":201,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/02\/multiple-counts-single-query-mysql.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Get Multiple Counts in Single Query","datePublished":"2026-02-11T04:36:11+00:00","dateModified":"2026-02-11T08:27:05+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/"},"wordCount":852,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/02\/multiple-counts-single-query-mysql.jpg?fit=301%2C201&ssl=1","keywords":["multiple counts"],"articleSection":["MySQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/","name":"How to Get Multiple Counts in Single Query - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/02\/multiple-counts-single-query-mysql.jpg?fit=301%2C201&ssl=1","datePublished":"2026-02-11T04:36:11+00:00","dateModified":"2026-02-11T08:27:05+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Sometimes you may need to get multiple counts in single query in MySQL. Here are the different ways to do this.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/02\/multiple-counts-single-query-mysql.jpg?fit=301%2C201&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/02\/multiple-counts-single-query-mysql.jpg?fit=301%2C201&ssl=1","width":301,"height":201,"caption":"multiple counts single query mysql"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-get-multiple-counts-in-single-query\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Get Multiple Counts in Single Query"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2026\/02\/multiple-counts-single-query-mysql.jpg?fit=301%2C201&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-2JX","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/10537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=10537"}],"version-history":[{"count":31,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/10537\/revisions"}],"predecessor-version":[{"id":10577,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/10537\/revisions\/10577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/10570"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=10537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=10537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=10537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}