SQL Exercises: Employees who work in the department Marketing
SQL SUBQUERY: Exercise-41 with Solution
From the following table, write a SQL query to find those employees who work in the department 'Marketing'. Return first name, last name and department ID.
Sample table: employees
Sample table: departments
Sample Solution:
SELECT first_name, last_name, department_id
FROM employees
WHERE department_id =
(SELECT department_id
FROM departments
WHERE department_name = 'Marketing');
Sample Output:
first_name last_name department_id Michael Hartstein 20 Pat Fay 20
Code Explanation:
The said query in SQL that retrieves the first name, last name, and department id of all employees who belong to the department named "Marketing".
In this case, the WHERE clause is checks if the department_id of each employee matches the department_id of the department named "Marketing". The condition uses a subquery in the WHERE clause to retrieve the department_id of the "Marketing" department, and then compare it to the department_id of each employee.
Visual Presentation:

Practice Online
Query Visualization:
Duration:

Rows:

Cost:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: Employee who works in that department which ID is 40.
Next SQL Exercise: Employees earning more than departments minimum wage.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
SQL: Tips of the Day
MySQL export schema without data
mysqldump -h yourhostnameorIP -u root -p --no-data dbname > schema.sql
Ref: https://bit.ly/3xzB9dS
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook