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

MySQL Subquery Exercises: List the department ID and name of all the departments where no employee is working

MySQL Subquery: Exercise-19 with Solution

Write a query to list the department ID and name of all the departments where no employee is working.

Sample table: employees


Sample table : departments


Code:

SELECT * FROM departments 
WHERE department_id 
NOT IN (select department_id FROM employees);

Explanation:

MySQL Subquery Syntax :

MySQL subquery syntax

- The subquery (inner query) executes once before the main query (outer query) executes.
- The main query (outer query) use the subquery result.

MySQL SubQueries: Query to list department number, name for all the departments in which there are no employees in the department.

 

MySQL Code Editor:

Structure of 'hr' database:

hr database

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

Previous:Write a query to select last 10 records from a table.
Next:Write a query to get 3 maximum salaries.

What is the difficulty level of this exercise?