http://www.w3schools.com/sql/sql_func_count.asp
The COUNT() function returns the number of rows that matches a specified criteria.
SQL COUNT(column_name) Syntax
The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column:
SQL COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table:
SQL COUNT(DISTINCT column_name) Syntax
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.
Demo Database
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 |
SQL COUNT(column_name) Example
The following SQL statement counts the number of orders from "CustomerID"=7 from the "Orders" table:
Example
WHERE CustomerID=7;
Try it yourself »
SQL COUNT(*) Example
The following SQL statement counts the total number of orders in the "Orders" table:
SQL COUNT(DISTINCT column_name) Example
The following SQL statement counts the number of unique customers in the "Orders" table:
'SQL' 카테고리의 다른 글
LAST() Function (0) | 2015.06.20 |
---|---|
FIRST() Function (0) | 2015.06.20 |
AVG() Function (0) | 2015.06.20 |
Functions (0) | 2015.06.20 |
Data Types for Various DBs (0) | 2015.06.20 |