w3resource

PostgreSQL Subquery: Find the name and salary of the employees who draw a more salary than Bell


8. Write a SQL subquery to find the first_name, last_name and salary of the employees who draw a more salary than the employee, which the last name is Bell.

Sample Solution:

Code:

SELECT first_name, last_name, salary 
FROM employees 
WHERE salary > 
(SELECT salary 
FROM employees 
WHERE last_name = 'Bell') 
ORDER BY first_name;

Sample table: employees


Sample table: departments


Output:

pg_exercises=# SELECT first_name, last_name, salary
pg_exercises-# FROM employees
pg_exercises-# WHERE salary >
pg_exercises-# (SELECT salary
pg_exercises(# FROM employees
pg_exercises(# WHERE last_name = 'Bell')
pg_exercises-# ORDER BY first_name;
 first_name  | last_name  |  salary
-------------+------------+----------
 Adam        | Fripp      |  8230.00
 Alberto     | Errazuriz  | 12030.00
 Alexander   | Hunold     |  9030.00
 Alexis      | Bull       |  4130.00
 Allan       | McEwen     |  9030.00
 Alyssa      | Hutton     |  8830.00
 Amit        | Banda      |  6230.00
 Bruce       | Ernst      |  6030.00
 Charles     | Johnson    |  6230.00
 Christopher | Olsen      |  8030.00
 Clara       | Vishney    | 10530.00
 Daniel      | Faviet     |  9030.00
 David       | Lee        |  6830.00
 David       | Bernstein  |  9530.00
 David       | Austin     |  4830.00
 Den         | Raphaely   | 11030.00
 Diana       | Lorentz    |  4230.00
 Eleni       | Zlotkey    | 10530.00
 Elizabeth   | Bates      |  7330.00
 Ellen       | Abel       | 11030.00
 Gerald      | Cambrault  | 11030.00
...
 Valli       | Pataballa  |  4830.00
 William     | Smith      |  7430.00
 William     | Gietz      |  8330.00
(63 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