{"id":395,"date":"2008-04-16T21:11:00","date_gmt":"2008-04-16T21:11:00","guid":{"rendered":"\/blogs\/conor\/post\/COUNT(star)-vs-COUNT(column)-vs-COUNT(DISTINCT)-vs-COUNT(col)-OVER().aspx"},"modified":"2008-04-16T21:11:00","modified_gmt":"2008-04-16T21:11:00","slug":"countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/","title":{"rendered":"COUNT(star) vs COUNT(column) vs. COUNT(DISTINCT) vs. COUNT(col) OVER(&#8230;)"},"content":{"rendered":"<p>So COUNT isn&#39;t an operator, but you should view this post as a &quot;what is this operator&quot; kind of post since I talk about how these things work and what they mean.<\/p>\n<p>One of the benefits of building database engines is that all (well, most of) the syntax rules end up stuck in your head, but I know that many of these things are completely non-obvious, so I think it&#39;s worth mentioning a few things about what each of these things mean since I see them misused in lots of queries, blogs, etc.<\/p>\n<p>&#8212; Returns the count of rows from the table at the time that the query is run in the transactional isolation mode in which the query is run (usually read committed for SQL Server)<br \/>\nSELECT COUNT(*) FROM Table<\/p>\n<p>So that example is easy enough&#8230;<\/p>\n<p>SELECT COUNT(column1) FROM Table<\/p>\n<p>This is actually NOT the same query as COUNT(*), in general.&nbsp; It means &quot;count the number of non-null column1 rows&quot;.&nbsp; <\/p>\n<p>(Now for something cool.&nbsp; If you run this on a column that is non-nullable, then SQL Server converts it into the count(*) case because it is faster to run that form since it doesn&#39;t have to examine the data in each row and can instead just count rows).<\/p>\n<p>SELECT COUNT(DISTINCT column1) will count the number of UNIQUE non-null column1 values.&nbsp; It does not count NULL.&nbsp; I don&#39;t believe SQL Server completely removes the DISTINCT operation for non-null columns in all cases.&nbsp; It can in some cases.<\/p>\n<p>COUNT(col) OVER (&#8230;) is a completely different beast.&nbsp; It runs a count computation using the rules you&#39;ve seen above but it does not collapse the rows &#8211; it adds a new column on all the rows with the computed count.&nbsp; <\/p>\n<p>Remember that these are semantically different operations.<\/p>\n<p>Conor<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So COUNT isn&#39;t an operator, but you should view this post as a &quot;what is this operator&quot; kind of post since I talk about how these things work and what they mean. One of the benefits of building database engines is that all (well, most of) the syntax rules end up stuck in your head, [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-395","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>COUNT(star) vs COUNT(column) vs. COUNT(DISTINCT) vs. COUNT(col) OVER(...) - Conor Cunningham<\/title>\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.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"COUNT(star) vs COUNT(column) vs. COUNT(DISTINCT) vs. COUNT(col) OVER(...) - Conor Cunningham\" \/>\n<meta property=\"og:description\" content=\"So COUNT isn&#039;t an operator, but you should view this post as a &quot;what is this operator&quot; kind of post since I talk about how these things work and what they mean. One of the benefits of building database engines is that all (well, most of) the syntax rules end up stuck in your head, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/\" \/>\n<meta property=\"og:site_name\" content=\"Conor Cunningham\" \/>\n<meta property=\"article:published_time\" content=\"2008-04-16T21:11:00+00:00\" \/>\n<meta name=\"author\" content=\"Conor Cunningham\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Conor Cunningham\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/\",\"name\":\"COUNT(star) vs COUNT(column) vs. COUNT(DISTINCT) vs. COUNT(col) OVER(...) - Conor Cunningham\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#website\"},\"datePublished\":\"2008-04-16T21:11:00+00:00\",\"dateModified\":\"2008-04-16T21:11:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/f9106e03423de6b5157295891b8c3ae3\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"COUNT(star) vs COUNT(column) vs. COUNT(DISTINCT) vs. COUNT(col) OVER(&#8230;)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/\",\"name\":\"Conor Cunningham\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/f9106e03423de6b5157295891b8c3ae3\",\"name\":\"Conor Cunningham\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d9c37eff231ec89c1b244347d966860875eea8b55b366911d2694e8cd9913e57?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d9c37eff231ec89c1b244347d966860875eea8b55b366911d2694e8cd9913e57?s=96&d=mm&r=g\",\"caption\":\"Conor Cunningham\"},\"url\":\"https:\/\/www.sqlskills.com\/blogs\/conor\/author\/conor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"COUNT(star) vs COUNT(column) vs. COUNT(DISTINCT) vs. COUNT(col) OVER(...) - Conor Cunningham","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.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/","og_locale":"en_US","og_type":"article","og_title":"COUNT(star) vs COUNT(column) vs. COUNT(DISTINCT) vs. COUNT(col) OVER(...) - Conor Cunningham","og_description":"So COUNT isn&#39;t an operator, but you should view this post as a &quot;what is this operator&quot; kind of post since I talk about how these things work and what they mean. One of the benefits of building database engines is that all (well, most of) the syntax rules end up stuck in your head, [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/","og_site_name":"Conor Cunningham","article_published_time":"2008-04-16T21:11:00+00:00","author":"Conor Cunningham","twitter_misc":{"Written by":"Conor Cunningham","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/","url":"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/","name":"COUNT(star) vs COUNT(column) vs. COUNT(DISTINCT) vs. COUNT(col) OVER(...) - Conor Cunningham","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#website"},"datePublished":"2008-04-16T21:11:00+00:00","dateModified":"2008-04-16T21:11:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/f9106e03423de6b5157295891b8c3ae3"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/countstar-vs-countcolumn-vs-countdistinct-vs-countcol-over\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/conor\/"},{"@type":"ListItem","position":2,"name":"COUNT(star) vs COUNT(column) vs. COUNT(DISTINCT) vs. COUNT(col) OVER(&#8230;)"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/conor\/","name":"Conor Cunningham","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/conor\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/f9106e03423de6b5157295891b8c3ae3","name":"Conor Cunningham","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/conor\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d9c37eff231ec89c1b244347d966860875eea8b55b366911d2694e8cd9913e57?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d9c37eff231ec89c1b244347d966860875eea8b55b366911d2694e8cd9913e57?s=96&d=mm&r=g","caption":"Conor Cunningham"},"url":"https:\/\/www.sqlskills.com\/blogs\/conor\/author\/conor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/posts\/395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/comments?post=395"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/posts\/395\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/media?parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/categories?post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/conor\/wp-json\/wp\/v2\/tags?post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}