Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

PostgreSQL Subquery: Find all the information of the employees whose salary greater than the average salary for all departments


10. Write a SQL subquery to find all the information of the employees whose salary greater than the average salary of all departments.

Sample Solution:

Code:

SELECT * 
FROM employees 
WHERE salary > 
ALL(SELECT avg(salary) 
FROM employees 
GROUP BY department_id);

Sample table: employees


Output:

pg_exercises=# SELECT *
pg_exercises-# FROM employees
pg_exercises-# WHERE salary >
pg_exercises-# ALL(SELECT avg(salary)
pg_exercises(# FROM employees
pg_exercises(# GROUP BY department_id);
 employee_id | first_name | last_name |     email     | phone_number | hire_date  | job_id  |  salary  | commission_pct | manager_id | department_id
-------------+------------+-----------+---------------+--------------+------------+---------+----------+----------------+------------+---------------
         100 | Steven     | King      | not available | 515.123.4567 | 1987-06-17 | AD_PRES | 24000.00 |           0.00 |          0 |            90
(1 row)

Practice Online


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a SQL subquery to find all the information of the employees who draws the same salary as the minimum salary for all departments.
Next: Write a subquery to find the first_name, last_name, job_id and salary of the employees who draws a salary that is higher than the salary of all the Shipping Clerk (JOB_ID = 'SH_CLERK'). Sort the results on salary from the lowest to highest.

What is the difficulty level of this exercise?