SQL Quest › SQL Interview Questions › Subqueries & CTEs

Department Budget Analysis

HardProQuerying BasicsSubqueries & CTEsAggregation & Grouping

Using a CTE named dept_stats, calculate each department's total_salary and avg_salary (rounded to nearest integer). Then from that CTE, show only departments where avg_salary exceeds the company-wide average salary (also rounded). Show department, total_salary, and avg_salary. Sort by avg_salary descending.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".

Schema

employees

emp_idnamedepartmentpositionsalaryhire_datemanager_idperformance_rating
1Alice JohnsonEngineeringSenior Developer950002019-03-1554.5
2Bob SmithEngineeringDeveloper750002020-06-0113.8
3Carol WilliamsMarketingMarketing Manager850002018-09-20NULL4.2

Expected output: Engineering appears because 87000 > 72000

Hint

This is a Pro challenge — the hint, the step-by-step tutor and the reference solution open in the app.

Concepts

SELECT CTE Aggregation GROUP BY Subquery

Practise the topic: SQL practice questions · CTE practice · GROUP BY exercises · Advanced SQL interview questions

Read the concept: What is a CTE?

In these company practice sets

Snowflake

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Genres in Common (INTERSECT)Hard · ProEngagement Streaks (3+ Orders, ≤7-Day Gaps)Hard · ProTop-N Products per CategoryHard · ProTop Spender per Membership TierHard · ProRecursive Org Chart TraversalHard · ProIsland Length ClassificationHard · Pro

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test