Bird
0
0

Which query gives both counts correctly?

hard📝 Application Q15 of 15
SQL - Aggregate Functions
You have a table 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?
ASELECT COUNT(phone_number) + COUNT(*) AS total FROM employees;
BSELECT COUNT(*) AS with_phone, COUNT(phone_number) AS total_employees FROM employees;
CSELECT COUNT(phone_number) AS with_phone, COUNT(*) AS total_employees FROM employees;
DSELECT COUNT(phone_number) AS total_employees, COUNT(*) AS with_phone FROM employees;
Step-by-Step Solution
Solution:
  1. Step 1: Count employees with phone numbers

    COUNT(phone_number) counts only non-NULL phone numbers, so it returns 80.
  2. Step 2: Count total employees

    COUNT(*) counts all rows, so it returns 100.
  3. Final Answer:

    SELECT COUNT(phone_number) AS with_phone, COUNT(*) AS total_employees FROM employees; -> Option C
  4. Quick Check:

    COUNT(phone_number) = 80, COUNT(*) = 100 [OK]
Quick Trick: Use COUNT(column) for non-NULL, COUNT(*) for total rows [OK]
Common Mistakes:
MISTAKES
  • Swapping counts in SELECT clause
  • Adding counts instead of separate columns
  • Confusing which count counts NULLs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes