COUNT returns the number of records from a group, column, or query.
This function returns an integer data value.
COUNT ignores NULL values.
This example returns the total number of products.
SELECT COUNT(Id) AS 'Product Count'
FROM Product
| Product Count |
|---|
| 78 |
Syntax of the COUNT function.
COUNT(value)
value -- a value, column, or subquery.
| ORDER |
|---|
| Id |
| OrderDate |
| OrderNumber |
| CustomerId |
| TotalAmount |
SELECT MONTH(OrderDate) AS 'Month',
COUNT(Id) AS 'Items Sold'
FROM [Order]
WHERE YEAR(OrderDate) = 2013
GROUP BY MONTH(OrderDate)
| Month | Items Sold |
|---|---|
| 1 | 33 |
| 2 | 29 |
| 3 | 30 |
| 4 | 31 |
| 5 | 32 |
![]() |
|
| PRODUCT |
|---|
| Id |
| ProductName |
| SupplierId |
| UnitPrice |
| Package |
| IsDiscontinued |
| SUPPLIER |
|---|
| Id |
| CompanyName |
| ContactName |
| City |
| Country |
| Phone |
| Fax |
SELECT S.CompanyName,
COUNT(P.Id) AS 'Number of Products'
FROM Product P
JOIN Supplier S ON S.Id = P.SupplierId
GROUP BY S.CompanyName
| CompanyName | Number of Products |
|---|---|
| Aux joyeux ecclésiastiques | 2 |
| Bigfoot Breweries | 3 |
| Cooperativa de Quesos 'Las Cabras' | 2 |
| Escargots Nouveaux | 1 |
| Exotic Liquids | 3 |
![]() |
|