Introduction
The COUNT function helps you find out how many rows or values are in a table or result. It is useful to quickly count items without looking at each one.
Jump into concepts and practice - no test required
COUNT(column_name) COUNT(*) COUNT(DISTINCT column_name)
SELECT COUNT(*) FROM employees;
SELECT COUNT(email) FROM customers;
SELECT COUNT(DISTINCT department) FROM employees;
CREATE TABLE sales ( id INT, product VARCHAR(20), quantity INT ); INSERT INTO sales VALUES (1, 'Apple', 10), (2, 'Banana', NULL), (3, 'Apple', 5), (4, NULL, 7); SELECT COUNT(*) AS total_rows FROM sales; SELECT COUNT(product) AS product_count FROM sales; SELECT COUNT(DISTINCT product) AS unique_products FROM sales;
COUNT(*) do when used in a query?COUNT(*) function counts every row in the table regardless of NULL values in any column.COUNT(column), which skips NULLs, COUNT(*) includes all rows.age from the table persons?COUNT(column) counts only non-NULL values in that column.SELECT COUNT(age) FROM persons; correctly counts non-NULL age values.employees with the column department containing values: ['HR', 'IT', NULL, 'IT', 'HR', 'Finance', NULL], what is the result of the query SELECT COUNT(DISTINCT department) FROM employees;?SELECT COUNT(employee_id) FROM staff; but it returns 0 even though the table has rows. What is the most likely reason?employee_id values are NULL, COUNT returns 0 even if rows exist.COUNT(DISTINCT customer_id), which counts unique non-NULL values correctly.