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:
-- This SQL query retrieves the first name, last name, and salary of employees whose salary is greater than the average salary of all employees.
SELECT first_name, -- Selects the first_name column from the employees table
last_name, -- Selects the last_name column from the employees table
salary -- Selects the salary column from the employees table
FROM employees -- Specifies the table from which to retrieve data, in this case, the employees table
WHERE salary >( -- Filters the rows to include only those where the salary is greater than the average salary of all employees
SELECT AVG(salary) -- Subquery: Calculates the average salary of all employees
FROM employees
);
Explanation:
- This SQL query retrieves the first name, last name, and salary of employees whose salary is greater than the average salary of all employees.
- The outer SELECT statement retrieves the first name, last name, and salary from the employees table.
- The WHERE clause filters the rows to include only those where the salary is greater than the average salary obtained from the subquery.
- The subquery calculates the average salary of all employees using the AVG() function.
- The subquery is enclosed in parentheses and executed before the outer query.
Sample table: employees
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | 100 | Steven | King | SKING | 515.123.4567 | 1987-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 1987-06-18 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 1987-06-19 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 1987-06-20 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 1987-06-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 1987-06-22 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 1987-06-23 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 1987-06-24 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 1987-06-25 | FI_MGR | 12000.00 | 0.00 | 101 | 100 | .......... | 206 | William | Gietz | WGIETZ | 515.123.8181 | 1987-10-01 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
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 | 9000.00 Den | Raphaely | 11000.00 Steven | King | 24000.00 Neena | Kochhar | 17000.00 Janette | King | 10000.00 Patrick | Sully | 9500.00 Allan | McEwen | 9000.00 Lindsey | Smith | 8000.00 Louise | Doran | 7500.00 Sarath | Sewall | 7000.00 Clara | Vishney | 10500.00 Mattea | Marvins | 7200.00 Payam | Kaufling | 7900.00 Shanta | Vollman | 6500.00 Ellen | Abel | 11000.00 Alyssa | Hutton | 8800.00 John | Russell | 14000.00 Karen | Partners | 13500.00 Alberto | Errazuriz | 12000.00 Gerald | Cambrault | 11000.00 Eleni | Zlotkey | 10500.00 ... | ... | ... Hermann | Baer | 10000.00 Shelley | Higgins | 12000.00 William | Gietz | 8300.00 (50 rows)
Go to:
PREV : Write a SQL subquery to find the first_name and last_name of the employees who are working as a manager.
NEXT : Write a SQL subquery to find the first_name, last_name and salary, which is equal to the minimum salary for this post, he/she is working on.
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
