The COUNT function in SQL is used to count the number of rows that meet a certain condition. When you want to count non-NULL values in a specific column, you can use COUNT(column_name). This will count only the rows where the specified column has a non-NULL value.
Here's the basic syntax:
SELECT COUNT(column_name) AS count_non_nulls
FROM table_name
WHERE condition; -- Optional: include a condition to filter rows
Example:
Suppose you have a table called employees with a column named email. To count the number of employees who have a non-NULL email address, you would write:
SELECT COUNT(email) AS count_non_null_emails
FROM employees;
In this case, count_non_null_emails will return the number of rows in the employees table where the email column is not NULL. Note that rows where email is NULL will not be counted.
Additional Notes:
- COUNT(*) counts all rows in the result set, regardless of NULLs.
- If you need to count all rows including those with NULL values in a certain column, you would typically use COUNT(*), but for non-NULLs specifically, just use COUNT(column_name).