SQL Command
SQL Command
SELECT COUNT(expression)
FROM tables
WHERE predicates;
Note:
The COUNT function will only count those records in which the field in the brackets is NOT NULL.
While the result for the next query will only return 1, since there is only one row in the suppliers tab
Simple Example:
For example, you might wish to know how many employees have a salary that is above $25,000 / ye
In this example, we've aliased the count(*) field as "Number of employees". As a result, "Number of
You can use the DISTINCT clause within the COUNT function.
For example, the SQL statement below returns the number of unique departments where at least on
Again, the count(DISTINCT department) field is aliased as "Unique departments". This is the field na
In some cases, you will be required to use a GROUP BY clause with the COUNT function.
For example, you could use the COUNT function to return the name of the department and the num
Because you have listed one column in your SELECT statement that is not encapsulated in the COUN
Since the COUNT function will return the same results regardless of what NOT NULL field(s) you inclu
For example, based on the example above, the following syntax would result in better performance:
Now, the COUNT function does not need to retrieve all fields from the employees table as it had to w
Based on the employees table populated with the following data, count the number of employees w
Solution:
Although inefficient in terms of performance, the following SQL statement would return the number
Number of employees
3
A more efficient implementation of the same solution would be the following SQL statement:
Now, the COUNT function does not need to retrieve all of the fields from the table (ie: employee_nu
Based on the suppliers table populated with the following data, count the number of distinct cities in
Solution:
The following SQL statement would return the number of distinct cities in the suppliers table:
Distinct Cities
4
Based on the customers table populated with the following data, count the number of distinct cities
Solution:
The following SQL statement would return the number of distinct cities for each customer_name in
ne row in the suppliers table where the State field is NOT NULL.
s". As a result, "Number of employees" will display as the field name when the result set is returned.
UNT function.
e department and the number of employees (in the associated department) that make over $25,000 / year.
encapsulated in the COUNT function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP B
NOT NULL field(s) you include as the COUNT function parameters (ie: within the brackets), you can change the syntax of the COUNT fun
ployees table as it had to when you used the COUNT(*) syntax. It will merely retrieve the numeric value of 1 for each record that meets
he table (ie: employee_number, employee_name, and salary), but rather whenever the condition is met, it will retrieve the numeric va
hange the syntax of the COUNT function to COUNT(1) to get better performance as the database engine will not have to fetch back the