Bird
0
0

Which query correctly does this?

hard📝 Application Q15 of 15
SQL - Aggregate Functions
You have a table employees with a nullable bonus column. You want to calculate the total bonus, treating NULL as zero, but only for employees with a salary above 50000. Which query correctly does this?
ASELECT SUM(COALESCE(bonus, 0)) FROM employees WHERE salary > 50000;
BSELECT SUM(bonus) FROM employees WHERE COALESCE(salary, 0) > 50000;
CSELECT SUM(COALESCE(bonus, 0)) WHERE salary > 50000 FROM employees;
DSELECT SUM(bonus) FROM employees WHERE salary > 50000;
Step-by-Step Solution
Solution:
  1. Step 1: Use COALESCE to treat NULL bonus as zero

    SUM(COALESCE(bonus, 0)) replaces NULL bonuses with 0 before summing.
  2. Step 2: Filter employees with salary > 50000

    The WHERE clause correctly filters rows before aggregation.
  3. Step 3: Check query syntax

    SELECT SUM(COALESCE(bonus, 0)) FROM employees WHERE salary > 50000; has correct syntax.
  4. Final Answer:

    SELECT SUM(COALESCE(bonus, 0)) FROM employees WHERE salary > 50000; -> Option A
  5. Quick Check:

    Use COALESCE in SUM and filter with WHERE [OK]
Quick Trick: Use COALESCE in SUM and filter rows with WHERE [OK]
Common Mistakes:
MISTAKES
  • Placing WHERE clause after FROM incorrectly
  • Not using COALESCE to handle NULLs
  • Filtering on COALESCE(salary, 0) unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes