58. Departments Beating the Company Average (HAVING + Subquery)
HardSubqueriessubquery in HAVINGAVGGROUP BYROUND
A subquery in HAVING filters aggregated groups against a value computed by another query — here, the company-wide average salary.
Task: return departments whose average salary is strictly greater than the overall company average salary.
The average salary is not necessarily an integer, so round it to 2 decimals with ROUND(AVG(salary), 2).
Return columns dept, avg_salary. Order by avg_salary descending, then dept ascending.
Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…
This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases — 5test cases in all. A sloppy query that only fits the example won't pass.
SQL· SQLite · runs in your browser
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.