Bird
Raised Fist0
MySQLquery~5 mins

COUNT function in MySQL - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the COUNT function do in SQL?
The COUNT function counts the number of rows that match a specified condition or all rows if no condition is given.
Click to reveal answer
beginner
How do you count all rows in a table named 'employees'?
Use: SELECT COUNT(*) FROM employees; This counts every row in the 'employees' table.
Click to reveal answer
intermediate
What is the difference between COUNT(*) and COUNT(column_name)?
COUNT(*) counts all rows including those with NULLs. COUNT(column_name) counts only rows where the column is NOT NULL.
Click to reveal answer
beginner
Can COUNT be used with a WHERE clause? Give an example.
Yes. Example: SELECT COUNT(*) FROM employees WHERE department = 'Sales'; counts only employees in Sales.
Click to reveal answer
intermediate
What will SELECT COUNT(DISTINCT department) FROM employees; return?
It returns the number of unique departments in the employees table, ignoring duplicates.
Click to reveal answer
What does COUNT(*) count in a table?
AOnly rows with non-NULL values in the first column
BAll rows including those with NULL values
COnly rows with NULL values
DOnly distinct rows
Which query counts only rows where the 'age' column is NOT NULL?
ASELECT COUNT(age) FROM table;
BSELECT COUNT(*) FROM table;
CSELECT COUNT(DISTINCT age) FROM table;
DSELECT COUNT(NULL) FROM table;
How to count unique values in a column 'city'?
ASELECT COUNT(DISTINCT city) FROM table;
BSELECT COUNT(NULL) FROM table;
CSELECT COUNT(*) FROM table;
DSELECT COUNT(city) FROM table;
What does this query do? SELECT COUNT(*) FROM employees WHERE salary > 50000;
ACounts all employees
BCounts employees with NULL salary
CCounts employees with salary less than 50000
DCounts employees with salary greater than 50000
If a column has NULL values, which COUNT counts them?
ACOUNT(DISTINCT column_name)
BCOUNT(column_name)
CCOUNT(*)
DNone count NULL values
Explain how the COUNT function works and how it handles NULL values.
Think about counting all rows versus counting only non-NULL values in a column.
You got /4 concepts.
    Describe how to count unique values in a column using COUNT.
    Use DISTINCT keyword inside COUNT.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the COUNT(*) function do in MySQL?
      easy
      A. Counts all rows in the table, including those with NULL values
      B. Counts only rows where all columns are NOT NULL
      C. Counts only rows with NULL values in any column
      D. Counts the number of columns in the table

      Solution

      1. Step 1: Understand COUNT(*) behavior

        The COUNT(*) function counts every row in the table regardless of NULL values.
      2. Step 2: Compare with other COUNT variants

        Unlike COUNT(column), which counts only non-NULL values in that column, COUNT(*) counts all rows.
      3. Final Answer:

        Counts all rows in the table, including those with NULL values -> Option A
      4. Quick Check:

        COUNT(*) counts all rows [OK]
      Hint: COUNT(*) counts every row, NULL or not [OK]
      Common Mistakes:
      • Thinking COUNT(*) ignores rows with NULL
      • Confusing COUNT(*) with COUNT(column)
      • Assuming COUNT(*) counts columns
      2. Which of the following is the correct syntax to count non-NULL values in the column age from the table users?
      easy
      A. SELECT COUNT(*) FROM users;
      B. SELECT COUNT(age) FROM users WHERE age != NULL;
      C. SELECT COUNT(age) FROM users WHERE age = NULL;
      D. SELECT COUNT(age) FROM users;

      Solution

      1. Step 1: Identify correct COUNT syntax for non-NULL values

        COUNT(column) counts only non-NULL values in that column, so COUNT(age) counts non-NULL ages.
      2. Step 2: Check WHERE clause correctness

        SELECT COUNT(*) FROM users; uses COUNT(*) which counts all rows. Using WHERE age IS NOT NULL is valid but unnecessary with COUNT(age). Options B and C use incorrect NULL comparisons.
      3. Final Answer:

        SELECT COUNT(age) FROM users; -> Option D
      4. Quick Check:

        COUNT(column) counts non-NULL values [OK]
      Hint: Use COUNT(column) to count non-NULL values directly [OK]
      Common Mistakes:
      • Using WHERE age = NULL instead of IS NOT NULL
      • Using COUNT(*) to count non-NULL values
      • Using != NULL which is invalid in SQL
      3. Given the table orders with 5 rows where the discount column has 3 NULL values and 2 non-NULL values, what will this query return?
      SELECT COUNT(discount) FROM orders;
      medium
      A. 5
      B. 3
      C. 2
      D. 0

      Solution

      1. Step 1: Understand COUNT(column) counts non-NULL values

        The query counts only rows where discount is NOT NULL.
      2. Step 2: Apply to given data

        There are 2 non-NULL discount values, so the count is 2.
      3. Final Answer:

        2 -> Option C
      4. Quick Check:

        COUNT(discount) = 2 [OK]
      Hint: COUNT(column) ignores NULLs, counts only real values [OK]
      Common Mistakes:
      • Counting total rows instead of non-NULL values
      • Confusing NULL count with non-NULL count
      • Assuming COUNT(column) counts NULLs
      4. Identify the error in this query:
      SELECT COUNT(name) FROM employees WHERE name = NULL;
      medium
      A. Using COUNT(name) instead of COUNT(*)
      B. Incorrect NULL comparison with '=' instead of IS NULL
      C. Missing GROUP BY clause
      D. COUNT cannot be used with WHERE clause

      Solution

      1. Step 1: Check WHERE clause NULL comparison

        In SQL, NULL cannot be compared with '='; use IS NULL instead.
      2. Step 2: Validate COUNT usage

        COUNT(name) is valid and WHERE clause is allowed; no GROUP BY needed here.
      3. Final Answer:

        Incorrect NULL comparison with '=' instead of IS NULL -> Option B
      4. Quick Check:

        Use IS NULL for NULL checks [OK]
      Hint: Use IS NULL, not = NULL, to check for NULL values [OK]
      Common Mistakes:
      • Using '=' to compare NULL values
      • Thinking COUNT can't be combined with WHERE
      • Adding unnecessary GROUP BY
      5. You have a table sales with columns region and amount. You want to find how many sales records exist for each region, including regions with zero sales. Which query will correctly show all regions with their sales count, including zero counts?
      hard
      A. SELECT r.region, COUNT(s.amount) FROM regions r LEFT JOIN sales s ON r.region = s.region GROUP BY r.region;
      B. SELECT region, COUNT(*) FROM sales GROUP BY region;
      C. SELECT region, COUNT(amount) FROM sales GROUP BY region;
      D. SELECT region, COUNT(amount) FROM sales WHERE amount IS NOT NULL GROUP BY region;

      Solution

      1. Step 1: Understand requirement to include regions with zero sales

        To include regions with zero sales, we must use a LEFT JOIN from all regions to sales.
      2. Step 2: Analyze query options

        SELECT r.region, COUNT(s.amount) FROM regions r LEFT JOIN sales s ON r.region = s.region GROUP BY r.region; uses LEFT JOIN from regions table to sales, counting sales per region including zero counts. Options A, B, D only count existing sales records, missing zero counts.
      3. Final Answer:

        SELECT r.region, COUNT(s.amount) FROM regions r LEFT JOIN sales s ON r.region = s.region GROUP BY r.region; -> Option A
      4. Quick Check:

        LEFT JOIN + COUNT(column) includes zero counts [OK]
      Hint: Use LEFT JOIN with COUNT(column) to include zero counts [OK]
      Common Mistakes:
      • Using only sales table, missing regions with zero sales
      • Using COUNT(*) which counts joined rows, not zero
      • Not using LEFT JOIN to include all regions
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard