The COUNT() function returns the number of rows that matches a specified criteria.SQL COUNT(column_name) SyntaxThe COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column:
SQL COUNT(*) SyntaxThe COUNT(*) function returns the number of records in a table:
SQL COUNT(DISTINCT column_name) SyntaxThe COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column:
SQL COUNT(column_name) ExampleWe have the following "Orders" table:
Now we want to count the number of orders from "Customer Nilsen". We use the following SQL statement:
The result of the SQL statement above will be 2, because the customer Nilsen has made 2 orders in total:
SQL COUNT(*) ExampleIf we omit the WHERE clause, like this:
The result-set will look like this:
which is the total number of rows in the table. SQL COUNT(DISTINCT column_name) ExampleNow 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:
which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders" table. | ||||||||||||||||||||||||||||||||||||||||||||||||||||