Matthew
Matthew

Reputation: 45

Finding an alternate solution by using join tables in subqueries

So, here is what the problem reads: Write a subquery that returns a set of rows to find all departments that do actually have one or more employees assigned to them.

Here are snips from the tables (employees and department):

    +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
| 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 |


+---------------+----------------------+------------+-------------+
| DEPARTMENT_ID | DEPARTMENT_NAME      | MANAGER_ID | LOCATION_ID |
+---------------+----------------------+------------+-------------+
|            10 | Administration       |        200 |        1700 |
|            20 | Marketing            |        201 |        1800 |
|            30 | Purchasing           |        114 |        1700 |
|            40 | Human Resources      |        203 |        2400 |
|            50 | Shipping             |        121 |        1500 |
|            60 | IT                   |        103 |        1400 |
|            70 | Public Relations     |        204 |        2700 |
|            80 | Sales                |        145 |        2500 |
|            90 | Executive            |        100 |        1700 |
|           100 | Finance              |        108 |        1700 |

Here is the sample solution:

SELECT  department_name 
FROM departments 
WHERE department_id IN 
(SELECT DISTINCT(department_id) 
FROM employees);

I was attempting something like this:

SELECT department_name, count(*) AS dep_count
FROM departments
WHERE dep_count >= (SELECT department_name
                    FROM employees JOIN departments
                    ON employees.department_id=departments.department_id
                    WHERE department_id >=1)
GROUP BY department_name;

I am looking for a solution to the join table subquery.

Upvotes: 0

Views: 60

Answers (2)

Nick
Nick

Reputation: 147206

You could use an EXISTS subquery in your WHERE clause:

SELECT *
FROM departments d
WHERE EXISTS (SELECT * 
              FROM employees e
              WHERE e.department_id = d.department_id)

Or alternatively a simple JOIN from departments to employees, selecting the DISTINCT rows from departments:

SELECT DISTINCT d.*
FROM departments d
JOIN employees e ON e.department_id = d.department_id

Output (for the sample data you have provided):

DEPARTMENT_ID   DEPARTMENT_NAME     MANAGER_ID  LOCATION_ID
60              IT                  103         1400
90              Executive           100         1700
100             Finance             108         1700

Demo on SQLFiddle

Note if you only want the department_name, just replace * and d.* in the above queries with department_name.

Upvotes: 2

Tregoreg
Tregoreg

Reputation: 22256

SELECT department_name
FROM departments JOIN employees USING (department_id)
GROUP BY department_name

Or, equivalently:

SELECT DISTINCT department_name
FROM departments JOIN employees USING (department_id)

Upvotes: 1

Related Questions