w3resource

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:

SQL Subqueries: Display the first and last name, salary, and department ID for all those employees who work in that department where the employee works who hold the ID 201.

Practice Online


Query Visualization:

Duration:

Query visualization of Display the first and last name, and department code for all employees who work in the department Marketing - Duration

Rows:

Query visualization of Display the first and last name, and department code for all employees who work in the department Marketing - Rows

Cost:

Query visualization of Display the first and last name, and department code for all employees who work in the department Marketing - 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.



Follow us on Facebook and Twitter for latest update.

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

 





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