w3resource

PostgreSQL Subquery: Find the names and salaries of the employees who have higher salary than the employee whose last_name is Bull


1. Write a query to find the first_name, last_name and salaries of the employees who have a higher salary than the employee whose last_name is Bull.

Sample Solution:

Code:

SELECT first_name, last_name, salary 
FROM employees 
WHERE salary > 
(SELECT salary 
FROM employees 
WHERE last_name = 'Bull');

Sample table: employees


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 = 'Bull');
 first_name  | last_name  |  salary
-------------+------------+----------
 Alexander   | Hunold     |  9030.00
 Bruce       | Ernst      |  6030.00
 David       | Austin     |  4830.00
 Valli       | Pataballa  |  4830.00
 Diana       | Lorentz    |  4230.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
 Kevin       | Mourgos    |  5830.00
 Ellen       | Abel       | 11030.00
 Alyssa      | Hutton     |  8830.00
 ...
  Hermann     | Baer       | 10030.00
 Shelley     | Higgins    | 12030.00
 William     | Gietz      |  8330.00
(62 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