Departments With Many Employees AND High Average Salary
SQL coding challenge · Difficulty: medium · Topic: Aggregation · +100 XP
Problem
The real estate team allocates premium offices to departments that are both large AND well-compensated. Criteria: more than 3 employees AND average salary above 60,000.
Tables
Table: departments
| department_id | department_name | | --- | --- | | 1 | Engineering | | 2 | Marketing | | 3 | HR | | 4 | Finance |
Table: employees
| employee_id | first_name | department_id | salary | hire_date | manager_id | | --- | --- | --- | --- | --- | --- | | 1 | Alice | 1 | 90000 | 2021-03-10 | NULL | | 2 | Bob | 1 | 75000 | 2021-03-20 | 1 | | 3 | Charlie | 1 | 80000 | 2021-03-25 | 1 | | 4 | Victor | 1 | 65000 | 2022-01-15 | 1 | | 5 | Diana | 2 | 70000 | 2022-06-05 | 1 | | 6 | Eve | 2 | 65000 | 2021-11-15 | 5 | | 7 | Frank | 3 | 60000 | 2023-02-28 | 1 |
Expected Output
| department_id | department_name | employee_count | avg_salary | | --- | --- | --- | --- | | 1 | Engineering | 4 | 77500.0000 |
- Return:
department_id,department_name,emp_count,avg_salary - Only departments with emp_count > 3 AND avg_salary > 60000
- Sort by
avg_salarydescending
What this SQL challenge teaches you
“Departments With Many Employees AND High Average Salary” is a medium-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with HAVING, COUNT, AVG, Multiple conditions — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- HAVING
- COUNT
- AVG
- Multiple conditions
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- Your result should return: department_id, department_name, emp_count, avg_salary.
- Order the output avg_salary descending.
- Compare your output to the Expected Output — the columns, values and row order must match exactly.
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Microsoft, Databricks, Google. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Find Duplicate Emails
- HR: Average Salary by Department
- Logistics: Count Shipments by Status
- Count Total Orders Placed by Each Customer
- Find Average Order Amount for Each Customer
- Find Customers Who Placed More Than 5 Orders (HAVING)
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated medium and covers Aggregation.