AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL COUNT |
|
SQL > SQL Functions >
Count
The COUNT function in SQL is used to calculate the number of rows returned from the SQL statement.
Key Takeaway: The SQL COUNT function counts the number of rows in a result set. Use COUNT(*) to count all rows, COUNT(column) to exclude NULLs, and COUNT(DISTINCT column) to count only unique values.
SyntaxThe syntax for the COUNT function is, <expression> can be a column name, an arithmetic operation, or a star (*). When we use COUNT(*), we mean "count everything." It is also possible to have one or more columns in addition to the COUNT function in the SELECT statement. In those cases, these columns need to be part of the GROUP BY clause as well: COUNT is often combined with DISTINCT to calculate the number of unique values. The syntax for this is as follows: ExamplesWe use the following table for our examples. Table Store_Information
Example 1: Simple COUNT operationTo find the number of rows in this table, we key in, Result:
Please note that we can also use COUNT(*) instead of COUNT(Store_Name). In this case the two will generate the same answer because none of the values in the Store_Name field is NULL. Example 2: COUNT function with a GROUP BY clauseTo get the number of records for each store, we type in, Result:
Example 3: Use COUNT with DISTINCTCOUNT and DISTINCT can be used together in a statement to retrieve the number of distinct entries in a table. For example, if we want to find out the number of distinct stores in the Store_Information table, we type in, Result:
Frequently Asked Questions
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.