SQL Exercises: Find out the details of employees who are managers
From the following tables, write a SQL query to find those employees who are managers. Return all the fields of employees table.
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 | 2003-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 2005-09-21 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 2001-01-13 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 2006-01-03 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 2007-05-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 2005-06-25 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 2006-02-05 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 2007-02-07 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 2002-08-17 | FI_MGR | 12008.00 | 0.00 | 101 | 100 | | 109 | Daniel | Faviet | DFAVIET | 515.124.4169 | 2002-08-16 | FI_ACCOUNT | 9000.00 | 0.00 | 108 | 100 | | 110 | John | Chen | JCHEN | 515.124.4269 | 2005-09-28 | FI_ACCOUNT | 8200.00 | 0.00 | 108 | 100 | .................... +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Sample Solution:
-- Selecting all columns (*) from the 'employees' table
SELECT * 
-- Filtering rows based on the condition that the 'employee_id' is in the result set of a subquery
FROM employees 
-- Subquery to find distinct 'manager_id' values from the 'employees' table
WHERE employee_id IN 
  (SELECT DISTINCT manager_id FROM employees);
Sample Output:
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  |SKING|515.123.4568      |1987-06-18|AD_VP  |17000.00|          0.00|       100|           90|
        102|Lex       |De Haan  |SKING|515.123.4569      |1987-06-19|AD_VP  |17000.00|          0.00|       100|           90|
        103|Alexander |Hunold   |SKING|590.423.4567      |1987-06-20|IT_PROG| 9000.00|          0.00|       102|           60|
        114|Den       |Raphaely |SKING|515.127.4561      |1987-07-01|PU_MAN |11000.00|          0.00|       100|           30|
        108|Nancy     |Greenberg|SKING|515.999.4569      |1987-06-25|FI_MGR |12000.00|          0.00|       101|          100|
        145|John      |Russell  |SKING|011.44.1344.429268|1987-08-01|SA_MAN |14000.00|          0.40|       100|           80|
        146|Karen     |Partners |SKING|011.44.1344.467268|1987-08-02|SA_MAN |13500.00|          0.30|       100|           80|
        147|Alberto   |Errazuriz|SKING|011.44.1344.429278|1987-08-03|SA_MAN |12000.00|          0.30|       100|           80|
        148|Gerald    |Cambrault|SKING|011.44.1344.619268|1987-08-04|SA_MAN |11000.00|          0.30|       100|           80|
        149|Eleni     |Zlotkey  |SKING|011.44.1344.429018|1987-08-05|SA_MAN |10500.00|          0.20|       100|           80|
        120|Matthew   |Weiss    |SKING|650.123.1234      |1987-07-07|ST_MAN | 8000.00|          0.00|       100|           50|
        121|Adam      |Fripp    |SKING|650.123.2234      |1987-07-08|ST_MAN | 8200.00|          0.00|       100|           50|
        122|Payam     |Kaufling |SKING|650.123.3234      |1987-07-09|ST_MAN | 7900.00|          0.00|       100|           50|
        123|Shanta    |Vollman  |SKING|650.123.4234      |1987-07-10|ST_MAN | 6500.00|          0.00|       100|           50|
        124|Kevin     |Mourgos  |SKING|650.123.5234      |1987-07-11|ST_MAN | 5800.00|          0.00|       100|           50|
        201|Michael   |Hartstein|SKING|515.123.5555      |1987-09-26|MK_MAN |13000.00|          0.00|       100|           20|
        205|Shelley   |Higgins  |SKING|515.123.8080      |1987-09-30|AC_MGR |12000.00|          0.00|       101|          110|
Code Explanation:
The said query in SQL that retrieves all columns (*) from the 'employees' table where the "employee_id" is found in a subquery that returns the distinct "manager_id" values from the same "employees" table.
The subquery retrieves the distinct "manager_id" values from the 'employees' table and returns them as a list of values. The outer query then uses this list of values to find all the rows in the 'employees' table where the "employee_id" matches any of the subquery values.
Visual Presentation:

Alternative Statements:
Using Subquery with JOIN:
SELECT e1.* 
FROM employees e1
JOIN (
    SELECT DISTINCT manager_id
    FROM employees
) e2
ON e1.employee_id = e2.manager_id;
Using Subquery with EXISTS:
SELECT * 
FROM employees e1
WHERE EXISTS (
    SELECT 1
    FROM employees e2
    WHERE e1.employee_id = e2.manager_id
);
Go to:
PREV : Salary exceeds 50% of their departments total salary.
NEXT : Details of employees who manage a department.
Practice Online
Query Visualization:
Duration:

Rows:

Cost:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
