Question

    Which SQL query would return the top three departments

    with the highest total salaries from an employees table with columns department , salary , and employee_id ?
    A SELECT department, SUM(salary) FROM employees GROUPBY department ORDERBYSUM(salary) DESC LIMIT 3; Correct Answer Incorrect Answer
    B .SELECT department, salary FROM employees WHERE salary =MAX(salary) GROUPBY department LIMIT 3; Correct Answer Incorrect Answer
    C SELECT department, COUNT(salary) FROM employees GROUPBY department HAVINGCOUNT(salary) >3; Correct Answer Incorrect Answer
    D SELECTDISTINCT department, salary FROM employees ORDERBY salary DESC LIMIT 3; Correct Answer Incorrect Answer
    E SELECTdepartment FROM employees GROUPBY department ORDERBYMAX(salary) DESC LIMIT 3; Correct Answer Incorrect Answer

    Solution

    Explanation: The correct query uses SQL’s GROUP BY to aggregate data by department , calculates the sum of salary for each department using SUM(salary) , and orders the results in descending order with ORDER BY . Finally, the LIMIT 3 clause retrieves only the top three departments. This approach is efficient and aligns with database query standards, ensuring the highest salaries are calculated accurately and in rank order. Option B: MAX(salary) cannot be used without a GROUP BY clause for the entire table, and the syntax is incorrect. Option C: COUNT(salary) measures frequency, not total salary. Option D: DISTINCT eliminates duplicates but does not aggregate data for rankings. Option E: MAX(salary) sorts by individual highest salaries, not aggregated totals, leading to incorrect results.

    Practice Next