w3resource

PostgreSQL Subquery: Find the name and salary, which is greater than the average salary of the employees


5. Write a SQL subquery to find the first_name, last_name and salary, which is greater than the average salary of the employees.

Sample Solution:

Code:

SELECT first_name, last_name, salary 
FROM employees 
WHERE salary > (
SELECT AVG(salary) 
FROM employees);

Sample table: employees


Output:

pg_exercises=# SELECT first_name, last_name, salary
pg_exercises-# FROM employees
pg_exercises-# WHERE salary > (
pg_exercises(# SELECT AVG(salary)
pg_exercises(# FROM employees);
 first_name  | last_name  |  salary
-------------+------------+----------
 Alexander   | Hunold     |  9030.00
 Den         | Raphaely   | 11030.00
 Steven      | King       | 24030.00
 Neena       | Kochhar    | 17030.00
 Janette     | King       | 10030.00
 Patrick     | Sully      |  9530.00
 Allan       | McEwen     |  9030.00
 Lindsey     | Smith      |  8030.00
 Louise      | Doran      |  7530.00
 Sarath      | Sewall     |  7030.00
 Clara       | Vishney    | 10530.00
 Mattea      | Marvins    |  7230.00
 Payam       | Kaufling   |  7930.00
 Shanta      | Vollman    |  6530.00
 Ellen       | Abel       | 11030.00
 Alyssa      | Hutton     |  8830.00
 John        | Russell    | 14030.00
 Karen       | Partners   | 13530.00
 Alberto     | Errazuriz  | 12030.00
 Gerald      | Cambrault  | 11030.00
 Eleni       | Zlotkey    | 10530.00
...
 Hermann     | Baer       | 10030.00
 Shelley     | Higgins    | 12030.00
 William     | Gietz      |  8330.00
(50 rows)

Practice Online


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

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming