w3resource
SQL exercises

SQL Subquery Exercises: Display a set of rows to find all departments that do actually have one or more employees assigned to them

SQL SUBQUERY: Exercise-26 with Solution

Write a subquery that returns a set of rows to find all departments that do actually have one or more employees assigned to them.

Sample table: employees


Sample table: departments


Sample Solution:

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

Sample Output:

department_name
Administration
Marketing
Purchasing
Human Resources
Shipping
IT
Public Relations
Sales
Executive
Finance
Accounting

Practice Online


Query Visualization:

Duration:

Query visualization of Display a set of rows to find all departments that do actually have one or more employees assigned to them - Duration

Rows:

Query visualization of Display a set of rows to find all departments that do actually have one or more employees assigned to them - Rows

Cost:

Query visualization of Display a set of rows to find all departments that do actually have one or more employees assigned to them - Cost

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

Previous: Write a query to display the employee id, name ( first name and last name ), SalaryDrawn, AvgCompare (salary - the average salary of all employees) and the SalaryStatus column with a title HIGH and LOW respectively for those employees whose salary is more than and less than the average salary of all employees.
Next: Write a query that will identify all employees who work in departments located in the United Kingdom.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming