SQL Aggregate Function COUNT()
In SQL, the COUNT()
function is an aggregate function that returns the number of rows that match a specified condition. The function takes a single argument, which is the name of the column or expression to be counted.
Here is an example of using the COUNT()
function to count the number of rows in a employees
table:
SELECT COUNT(*) AS num_employees FROM employees;
In this example, the COUNT()
function is used to count the number of rows in the employees
table. The AS
keyword is used to alias the column name as num_employees
.
The COUNT()
function can also be used with the WHERE
clause to count the number of rows that match a specified condition. For example, to count the number of employees with a salary greater than $50,000, we can use the following query:
SELECT COUNT(*) AS num_employees_above_50k FROM employees WHERE salary > 50000;
In this example, the COUNT()
function is used with the WHERE
clause to count the number of rows where the salary
column is greater than $50,000. The result of the query is a single row with a single column, which contains the number of employees that meet the condition.
It's worth noting that COUNT()
will include NULL values in the count, but there is also a variant called COUNT(column_name)
which only counts non-NULL values in the specified column.