PostgreSQL Subquery: Display the information for all the departments where no employee is working
18. Write a subquery to display the information for all the departments where no employee is working.
Sample Solution:
Code:
-- This SQL query retrieves all departments that do not have any associated employees.
SELECT * -- Selects all columns from the departments table
FROM departments -- Specifies the table from which to retrieve data, in this case, the departments table
WHERE department_id NOT IN ( -- Filters the rows to include only those departments whose department_id does not exist in the subquery result
SELECT department_id -- Subquery: Selects department_id from employees table
FROM employees -- Specifies the table from which to retrieve data, in this case, the employees table
);
Explanation:
- This SQL query retrieves all departments that do not have any associated employees.
- The outermost SELECT statement selects all columns from the departments table.
- The WHERE clause filters the rows to include only those departments whose department_id does not exist in the subquery result.
- The subquery selects department_id from the 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 | 1987-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 1987-06-18 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 1987-06-19 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 1987-06-20 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 1987-06-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 1987-06-22 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 1987-06-23 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 1987-06-24 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 1987-06-25 | FI_MGR | 12000.00 | 0.00 | 101 | 100 | .......... | 206 | William | Gietz | WGIETZ | 515.123.8181 | 1987-10-01 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Sample table: departments
+---------------+----------------------+------------+-------------+ | 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 | ........ | 270 | Payroll | 0 | 1700 | +---------------+----------------------+------------+-------------+
Output:
pg_exercises=# SELECT * FROM departments
pg_exercises-# WHERE department_id
pg_exercises-# NOT IN (select department_id
pg_exercises(# FROM employees);
department_id | department_name | manager_id | location_id
---------------+----------------------+------------+-------------
120 | Treasury | 0 | 1700
130 | Corporate Tax | 0 | 1700
140 | Control And Credit | 0 | 1700
150 | Shareholder Services | 0 | 1700
160 | Benefits | 0 | 1700
170 | Manufacturing | 0 | 1700
180 | Construction | 0 | 1700
190 | Contracting | 0 | 1700
200 | Operations | 0 | 1700
210 | IT Support | 0 | 1700
220 | NOC | 0 | 1700
230 | IT Helpdesk | 0 | 1700
240 | Government Sales | 0 | 1700
250 | Retail Sales | 0 | 1700
260 | Recruiting | 0 | 1700
270 | Payroll | 0 | 1700
(16 rows)
Go to:
PREV : Write a subquery to select last 10 records from a table.
NEXT : Write a query to get three maximum salaries.
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
