Find Departments with More Than 10 Employees and an Average Salary Above 50,000
Company: Accenture
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
You are given two tables, `departments` and `employees`. Each employee belongs to a department through `department_id` and has a `salary`.
Write a SQL query that finds every department meeting both of these conditions:
- The department has more than 10 employees.
- The department's average salary is greater than \$50,000.
For each such department, return its id, its name, its number of employees and its average salary rounded to 2 decimal places.
```hint Where the conditions belong
Both conditions describe a whole department, not a single employee row. Think about which part of a query is evaluated after the rows have been grouped.
```
### Input Table(s)
`departments`
| Column | Type | Meaning |
|---|---|---|
| department_id | INT | Department identifier (primary key) |
| department_name | VARCHAR(100) | Department name, never NULL |
`employees`
| Column | Type | Meaning |
|---|---|---|
| employee_id | INT | Employee identifier (primary key) |
| employee_name | VARCHAR(100) | Employee name, never NULL |
| department_id | INT | The employee's department, a foreign key to `departments.department_id` |
| salary | DECIMAL(10,2) | The employee's salary, never NULL |
Keys: `departments.department_id` and `employees.employee_id` are primary keys. `employees.department_id` references `departments.department_id`.
### Output Contract
Return these columns, in this order:
| Column | Meaning |
|---|---|
| department_id | The department's id |
| department_name | The department's name |
| employee_count | Number of employees in the department |
| avg_salary | Average salary of the department's employees, rounded to 2 decimal places |
Return one row per qualifying department. Sort by `avg_salary` descending (the rounded value that is returned), then by `department_id` ascending. Because `department_id` is unique, this order is fully determined.
### Example
`departments`
| department_id | department_name |
|---|---|
| 1 | Engineering |
| 2 | Sales |
| 3 | Legal |
| 4 | Research |
| 5 | Facilities |
`employees`, shown in summary form. Each line stands for the given number of rows that share a department and a salary. Employee names do not affect the result, so they are left out.
| employee_id | department_id | salary | rows |
|---|---|---|---|
| 1 to 10 | 1 | 55000.00 | 10 |
| 11 | 1 | 70000.00 | 1 |
| 12 to 17 | 2 | 40000.00 | 6 |
| 18 to 23 | 2 | 60000.00 | 6 |
| 24 to 33 | 3 | 90000.00 | 10 |
| 34 to 45 | 4 | 80000.00 | 12 |
Expected result:
| department_id | department_name | employee_count | avg_salary |
|---|---|---|---|
| 4 | Research | 12 | 80000.00 |
| 1 | Engineering | 11 | 56363.64 |
Research has 12 employees at \$80,000 each. Engineering has 11 employees whose salaries sum to \$620,000, an average of \$56,363.636..., returned as 56363.64. Sales has 12 employees, but its average is exactly \$50,000, which is not greater than \$50,000. Legal has a high average but exactly 10 employees, one short of the threshold. Facilities has no employees at all.
### Constraints and Clarifications
- There are 1 to 1,000 departments and 1 to 100,000 employees.
- `salary` is between 0 and 1,000,000 and is never NULL.
- Every employee belongs to exactly one department, and every `employees.department_id` matches a row in `departments`.
- A department with no employees cannot qualify and does not appear in the result.
- Both thresholds are strict. A department with exactly 10 employees, or with an average of exactly \$50,000, is excluded.
- The salary condition compares the exact average before rounding. Rounding applies only to the returned `avg_salary`.
- `department_name` is not declared unique, so treat departments as distinct by `department_id`, even if two share a name.
Overview: Write a SQL query over a departments table and an employees table that returns every department with more than 10 employees and an average salary above 50,000, along with its headcount and rounded average. It tests grouping, filtering on aggregate values and a fully deterministic sort order.