The COUNT() function returns the number of rows that matches a specified criteria.
The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column:
The COUNT(*) function returns the number of records in a table:
The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column:
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.
In this tutorial we will use the well-known Northwind sample database.
Below is a selection from the "Orders" table:
| OrderID | CustomerID | EmployeeID | OrderDate | ShipperID |
|---|---|---|---|---|
| 10265 | 7 | 2 | 1996-07-25 | 1 |
| 10266 | 87 | 3 | 1996-07-26 | 3 |
| 10267 | 25 | 4 | 1996-07-29 | 1 |
The following SQL statement counts the number of orders from "CustomerID"=7 from the "Orders" table:
The following SQL statement omits the WHERE clause and counts the total number of orders from the "Orders" table:
Now we want to count the number of unique customers in the "Orders" table.
We use the following SQL statement:
The result-set will look like this:
| NumberOfCustomers |
|---|
| 89 |
which is the number of unique customers in the "Orders" table.
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The HTML5 Certificate documents your knowledge of advanced HTML5.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
Your message has been sent to W3Schools.