Bird
0
0

Find the error in this SQL query:

medium📝 Debug Q6 of 15
SQL - GROUP BY and HAVING

Find the error in this SQL query:

SELECT department, MAX(salary) FROM employees GROUP BY department HAVING salary > 50000;
AUsing column 'salary' in HAVING without aggregation
BGROUP BY clause is missing
CMAX() function cannot be used in SELECT
DHAVING clause should be replaced with WHERE
Step-by-Step Solution
Solution:
  1. Step 1: Check HAVING clause usage

    The HAVING clause filters groups based on aggregate results or grouped columns.
  2. Step 2: Identify invalid reference

    The query uses salary directly in HAVING, but salary is neither aggregated nor grouped.
  3. Step 3: Correct usage

    The condition should use an aggregate function like MAX(salary) > 50000 in HAVING.
  4. Final Answer:

    Using column 'salary' in HAVING without aggregation -> Option A
  5. Quick Check:

    HAVING must use aggregates or grouped columns [OK]
Quick Trick: HAVING needs aggregates or grouped columns [OK]
Common Mistakes:
MISTAKES
  • Using non-aggregated columns in HAVING
  • Confusing WHERE and HAVING clauses
  • Placing HAVING before GROUP BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes