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?



Inviting useful, relevant, well-written and unique guest posts