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 the name of the employees who are managers


4. Write a SQL subquery to find the first_name and last_name of the employees who are working as a manager.

Sample Solution:

Code:

SELECT first_name, last_name 
FROM employees 
WHERE (employee_id 
IN (SELECT manager_id 
FROM employees));

Sample table: employees


Output:

pg_exercises=# SELECT first_name, last_name
pg_exercises-# FROM employees
pg_exercises-# WHERE (employee_id
pg_exercises(# IN (SELECT manager_id
pg_exercises(# FROM employees));

 first_name | last_name
------------+-----------
 Alexander  | Hunold
 Den        | Raphaely
 Steven     | King
 Neena      | Kochhar
 Payam      | Kaufling
 Shanta     | Vollman
 Kevin      | Mourgos
 John       | Russell
 Karen      | Partners
 Alberto    | Errazuriz
 Gerald     | Cambrault
 Eleni      | Zlotkey
 Lex        | De Haan
 Matthew    | Weiss
 Adam       | Fripp
 Nancy      | Greenberg
 Michael    | Hartstein
 Shelley    | Higgins
(18 rows)

Practice Online


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

Previous: Write a SQL subquery to find the first_name and last_name of the employees under a manager who works for a department based in the United States.
Next: Write a SQL subquery to find the first_name, last_name and salary, which is greater than the average salary of the employees.

What is the difficulty level of this exercise?