Which of the following SQL queries is valid according to the GROUP BY SELECT list constraint?
B
Step-by-Step Solution
Key idea: This is a bounding question on the strict syntactic rules of the GROUP BY clause.
Step 1: Recall the GROUP BY SELECT list constraint. Every column in the SELECT list must either be included in the GROUP BY clause or be an argument to an aggregate function.
Step 2: Evaluate Option A: name is in SELECT but not in GROUP BY and not aggregated. (Invalid)
Step 3: Evaluate Option B: dept_id is in GROUP BY. MAX(salary) is aggregated. (Valid)
Step 4: Evaluate Option C: name is in SELECT but not in GROUP BY and not aggregated. (Invalid)
Step 5: Evaluate Option D: name and salary are in SELECT but not in GROUP BY and not aggregated. (Invalid)
Answer: SELECT dept_id, MAX(salary) FROM emp GROUP BY dept_id