In this step by step SQL Tutorial I will show you how to use COUNT aggregate function. For the purpose of this tutorial I will use SQL Server 2012.
Other Versions of SQL Server:
- SQL Server 2012
- This blog post has been written using SQL Server 2012
- SQL Server 2008 / R2
- The examples we used should work in SQL Server 2008 and R2
- SQL Server 2005
- We suspect that this will work in SQL Server 2005 as well
Who is it for?
- People who would like to learn basics of using functions.
- Those who are preparing for Microsoft certifications and need to cover this topic.
- COUNT Function Introduction
- COUNT Function Example
COUNT Function - Is used to count rows in a table or count rows based using specific logic.
COUNT (<<Input Field>>)
<<Input Field>> - Provide field name that will be used as your input.
In this example I will use AdventureWorksDW2012 database, dbo.DimCustomer table and show you how to use COUNT aggregate function. In my select I put COUNT and inside the brackets I type asterisk (*) after the asterisk type new column name.
COUNT(*) counts all rows in result set we get (which can be limited by WHERE clause)
I hope that helps