Bird
0
0

Which of the following is the correct PostgreSQL query syntax to select department and total salary for departments where total salary exceeds 20000?

easy📝 Syntax Q3 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Which of the following is the correct PostgreSQL query syntax to select department and total salary for departments where total salary exceeds 20000?
ASELECT department, SUM(salary) FROM employees WHERE SUM(salary) > 20000 GROUP BY department;
BSELECT department, SUM(salary) FROM employees HAVING SUM(salary) > 20000 GROUP BY department;
CSELECT department, SUM(salary) FROM employees GROUP BY department HAVING SUM(salary) > 20000;
DSELECT department, SUM(salary) FROM employees GROUP BY department WHERE SUM(salary) > 20000;
Step-by-Step Solution
Solution:
  1. Step 1: GROUP BY clause

    Groups rows by department.
  2. Step 2: HAVING clause

    Filters groups where SUM(salary) > 20000.
  3. Step 3: Syntax order

    HAVING must come after GROUP BY.
  4. Final Answer:

    SELECT department, SUM(salary) FROM employees GROUP BY department HAVING SUM(salary) > 20000; -> Option C
  5. Quick Check:

    HAVING follows GROUP BY [OK]
Quick Trick: HAVING always follows GROUP BY [OK]
Common Mistakes:
  • Placing HAVING before GROUP BY
  • Using WHERE with aggregate functions
  • Misordering clauses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes