{"id":6063,"date":"2019-09-24T21:35:22","date_gmt":"2019-09-25T04:35:22","guid":{"rendered":"https:\/\/database.guide\/?p=6063"},"modified":"2019-11-19T22:57:03","modified_gmt":"2019-11-20T05:57:03","slug":"how-count_big-works-in-sql-server","status":"publish","type":"post","link":"https:\/\/database.guide\/how-count_big-works-in-sql-server\/","title":{"rendered":"How COUNT_BIG() Works in SQL Server"},"content":{"rendered":"<p>In <a href=\"https:\/\/database.guide\/what-is-sql-server\/\">SQL Server<\/a>, the <code>COUNT_BIG()<\/code> function returns the number of items found in a group. You can use it to find out how many rows are in a table or result set.<\/p>\n<p>This function works similar to the <a href=\"https:\/\/database.guide\/how-count-works-in-sql-server\/\"><code>COUNT()<\/code><\/a> function. The difference is that <code>COUNT()<\/code> returns its result as an <strong>int<\/strong>, whereas <code>COUNT_BIG()<\/code> returns its result as a <strong>bigint<\/strong>. <\/p>\n<p>Therefore <code>COUNT_BIG()<\/code> could come in handy if you expect your result set to have a very large number of rows (i.e. larger than 2,147,483,647). <\/p>\n<p><!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>The syntax goes like this:<\/p>\n<pre>\r\n-- Aggregation Function Syntax  \r\nCOUNT_BIG ( { [ [ ALL | DISTINCT ] expression ] | * } )  \r\n  \r\n-- Analytic Function Syntax  \r\nCOUNT_BIG ( [ ALL ] { expression | * } ) OVER ( [ <partition_by_clause> ] )\r\n<\/pre>\n<p><code>ALL<\/code> applies the aggregate function to all values. This is the default value.<\/p>\n<p><code>DISTINCT<\/code> specifies that the function returns the number of unique nonnull values.<\/p>\n<p><code><var>expression<\/var><\/code> is an expression of any type. Aggregate functions and subqueries are not supported in the expression.<\/p>\n<p><code>*<\/code> specifies that all rows should be counted and returned, including duplicate rows, and rows that contain null values. <code>COUNT(*)<\/code> takes no parameters and does not support the use of <code>DISTINCT<\/code>. It also doesn&#8217;t require an <var>expression<\/var> parameter (because it doesn&#8217;t use information about any particular column).<\/p>\n<p><code>OVER ( [ &lt;partition_by_clause&gt; ] <\/code> divides the result set produced by the <code>FROM<\/code> clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group.<\/p>\n<h2>Example 1 &#8211; Basic Usage<\/h2>\n<p>Here&#8217;s a basic example showing how this function works:<\/p>\n<pre>\r\nUSE WideWorldImportersDW;\r\nSELECT COUNT_BIG(*) AS 'Row Count' \r\nFROM Fact.[Order];\r\n<\/pre>\n<p>Result:<\/p>\n<pre>\r\n+-------------+\r\n| Row Count   |\r\n|-------------|\r\n| 231412      |\r\n+-------------+\r\n<\/pre>\n<p>In this case there are 231412 rows in the <samp>Fact.[Order]<\/samp> table.<\/p>\n<p>In this case, I could&#8217;ve used <code>COUNT()<\/code> to return the same result, because the row count is small enough for an <strong>int<\/strong> to handle.<\/p>\n<h2>Example 2 &#8211; A Larger Result Set<\/h2>\n<p>The real benefit of using <code>COUNT_BIG()<\/code> is when your result set is much larger than the previous example.<\/p>\n<p>Example:<\/p>\n<pre>\r\nSELECT COUNT_BIG(*) AS 'Row Count' \r\nFROM ReallyBigTable;\r\n<\/pre>\n<p>Result:<\/p>\n<pre>\r\n+-----------------+\r\n| Row Count       |\r\n|-----------------|\r\n| 9147483648      |\r\n+-----------------+\r\n<\/pre>\n<p>In this case, the row count is so big that an <strong>int<\/strong> wouldn&#8217;t be able to handle it. Fortunately we can use <code>COUNT_BIG()<\/code>, because it returns its result as a <strong>bigint<\/strong>.<\/p>\n<h2>More Examples<\/h2>\n<p>For more examples, see <a href=\"https:\/\/database.guide\/how-count-works-in-sql-server\/\">How <code>COUNT()<\/code> Works in SQL Server<\/a>. That article provides more examples than is listed here, all of which are also applicable to <code>COUNT_BIG()<\/code>.<\/p>\n<h2>An Alternative: APPROX_COUNT_DISTINCT()<\/h2>\n<p>If you&#8217;re working with very large data sets, you might consider using <a href=\"https:\/\/database.guide\/how-approx_count_distinct-works-in-sql-server\/\"><code>APPROX_COUNT_DISTINCT()<\/code><\/a> instead of <code>COUNT_BIG(DISTINCT )<\/code> in some cases. <\/p>\n<p><code>APPROX_COUNT_DISTINCT()<\/code> returns an approximate value, rather than a precise value. However, it&#8217;s designed to be much more responsive than <code>COUNT_BIG()<\/code>, so it could be useful for times that responsiveness is more important than precision.<\/p>\n<p>It&#8217;s designed to return unique, non-null values, so it would only be relevant for times where you would normally be using the <code>DISTINCT<\/code> clause with <code>COUNT_BIG()<\/code>.<\/p>\n<p>Also note that, at the time of writing <code>APPROX_COUNT_DISTINCT()<\/code> is in public preview status.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SQL Server, the COUNT_BIG() function returns the number of items found in a group. You can use it to find out how many rows are in a table or result set. This function works similar to the COUNT() function. The difference is that COUNT() returns its result as an int, whereas COUNT_BIG() returns its &#8230; <a title=\"How COUNT_BIG() Works in SQL Server\" class=\"read-more\" href=\"https:\/\/database.guide\/how-count_big-works-in-sql-server\/\" aria-label=\"Read more about How COUNT_BIG() Works in SQL Server\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,5],"tags":[93,77,61],"class_list":["post-6063","post","type-post","status-publish","format-standard","hentry","category-sql","category-sql-server","tag-functions","tag-mssql","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/6063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=6063"}],"version-history":[{"count":7,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/6063\/revisions"}],"predecessor-version":[{"id":6124,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/6063\/revisions\/6124"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=6063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=6063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=6063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}