Junior — Senior
Get the names of departments with an average salary above 100,000
livecode
Task condition
Formulate an SQL query that returns the name of each department where the average salary of employees exceeds 100,000, as well as the calculated value of this average salary. If employees are not linked to a department (department_id is NULL or NONE) and their average salary also exceeds 100,000, such records should also be included in the result; instead of the department name, output the string "Without department".
Expected output for the provided data examples:
department avg_salary
Financial Department 130000
Without department 112500
IT Department 250000
Given are two tables — department and employees — with the following schemas:
CREATE TABLE department (id serial, name varchar not null);
CREATE TABLE employees (id serial, salary int not null, department_id smallint);
Example table contents:
| id | name |
|---|---|
| 1 | Accounting |
| 2 | Legal Department |
| 3 | IT Department |
| 4 | Security |
| 5 | Financial Department |
| id | salary | department_id |
|---|---|---|
| 1 | 135000 | 5 |
| 2 | 60000 | 1 |
| 3 | 30000 | 4 |
| 4 | 180000 | NULL |
| 5 | 80000 | 2 |
| 6 | 250000 | 3 |
| 7 | 35000 | 4 |
| 8 | 80000 | NULL |
| 9 | 90000 | 5 |