Introduction
COUNT(*) counts all rows, while COUNT(column) counts only rows where the column has a value. This helps you understand data completeness.
Jump into concepts and practice - no test required
SELECT COUNT(*) FROM table_name; SELECT COUNT(column_name) FROM table_name;
SELECT COUNT(*) FROM employees;
SELECT COUNT(email) FROM employees;
SELECT COUNT(*) AS total_rows, COUNT(phone) AS phone_count FROM contacts;
CREATE TABLE people ( id INT, name VARCHAR(50), email VARCHAR(50) ); INSERT INTO people (id, name, email) VALUES (1, 'Alice', 'alice@example.com'), (2, 'Bob', NULL), (3, 'Charlie', 'charlie@example.com'), (4, 'Diana', NULL); SELECT COUNT(*) AS total_rows, COUNT(email) AS email_count FROM people;
COUNT(*) and COUNT(column_name) in SQL?COUNT(*)COUNT(*) counts every row in the table, including those with NULL values in any column.COUNT(column_name)COUNT(column_name) counts only rows where the specified column is NOT NULL, ignoring rows where that column is NULL.COUNT(*) counts all rows; COUNT(column_name) counts only non-NULL values in that column. -> Option Demail is NOT NULL?COUNT(email)COUNT(email) counts only rows where email is NOT NULL, so it already filters NULLs.WHERE email IS NOT NULL is redundant with COUNT(email), so SELECT COUNT(email) FROM users; is correct and simpler.orders with 5 rows where the discount column has values: 10, NULL, 5, NULL, 0, what will be the result of SELECT COUNT(*) AS total, COUNT(discount) AS discount_count FROM orders;?COUNT(*)COUNT(*) counts all 5 rows regardless of NULLs.discount values with COUNT(discount)SELECT COUNT(column_name) FROM table_name; but it returns 0. The column has some NULL and some non-NULL values. What is the most likely problem?COUNT(column_name) returns 0 but column has non-NULL values, likely the column name is wrong or missing.COUNT(column_name) counts non-NULL values; if column exists and has non-NULLs, result won't be zero.employees with 100 rows. The phone_number column has 80 non-NULL values and 20 NULLs. You want to find how many employees have a phone number and how many total employees there are. Which query gives both counts correctly?COUNT(phone_number) counts only non-NULL phone numbers, so it returns 80.COUNT(*) counts all rows, so it returns 100.