How does COUNT() work in SQL?

Instruction: Count employees and explain the difference between COUNT(*), COUNT(column), and COUNT(DISTINCT column).

Context: Practice SQL row counts, NULL handling, and distinct-value counts with a short employee example.

Updated

Example Answer

COUNT() counts every row in the result. COUNT(column) counts rows where that column is not NULL, and COUNT(DISTINCT column) counts distinct non-NULL values. For the total number of employees, I would use COUNT(). The choice matters when a column has missing or repeated values.

Small example

Assume employees has a department_id column:

SELECT
    COUNT(*) AS employee_count,
    COUNT(department_id) AS employees_with_department,
    COUNT(DISTINCT department_id) AS department_count
FROM employees;

With department values 10, 10, NULL, the three counts are 3, 2, 1. An empty input returns zero, rather than NULL.

Reference

PostgreSQL: aggregate functions

Related Questions