Filter Employees Using Subquery in WHERE Clause
📖 Scenario: You work in the HR department of a company. You have a database table called employees that stores employee details including their id, name, and department_id. You also have a departments table that stores department_id and department_name.Your manager wants to see the list of employees who work in the 'Sales' department.
🎯 Goal: Build a SQL query that uses a subquery in the WHERE clause to find all employees who belong to the 'Sales' department.
📋 What You'll Learn
Create a table called
departments with columns department_id and department_name.Create a table called
employees with columns id, name, and department_id.Insert the exact data provided into both tables.
Write a SQL query that selects
id and name from employees where department_id matches the department_id of the 'Sales' department using a subquery in the WHERE clause.💡 Why This Matters
🌍 Real World
Filtering employees by department is a common task in HR databases to generate reports or manage staff.
💼 Career
Understanding subqueries in WHERE clauses is essential for database querying roles, data analysis, and backend development.
Progress0 / 4 steps