Bird
0
0

Which of the following is the correct syntax to use a subquery in the WHERE clause to find employees in departments with ID 10 or 20?

easy📝 Syntax Q12 of 15
SQL - Subqueries
Which of the following is the correct syntax to use a subquery in the WHERE clause to find employees in departments with ID 10 or 20?
ASELECT * FROM employees WHERE department_id IN SELECT department_id FROM departments WHERE department_id IN (10, 20);
BSELECT * FROM employees WHERE department_id = (SELECT department_id FROM departments WHERE department_id IN (10, 20));
CSELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_id = 10 OR 20);
DSELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_id IN (10, 20));
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct subquery syntax with IN

    The subquery must be enclosed in parentheses and used with IN to match multiple values.
  2. Step 2: Check each option's syntax

    SELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_id IN (10, 20)); correctly uses IN with a subquery in parentheses. SELECT * FROM employees WHERE department_id = (SELECT department_id FROM departments WHERE department_id IN (10, 20)); uses = which expects one value, causing error. SELECT * FROM employees WHERE department_id IN SELECT department_id FROM departments WHERE department_id IN (10, 20); misses parentheses around subquery. SELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_id = 10 OR 20); has incorrect WHERE clause syntax.
  3. Final Answer:

    SELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_id IN (10, 20)); -> Option D
  4. Quick Check:

    Subquery with IN needs parentheses [OK]
Quick Trick: Use IN with parentheses for subqueries returning multiple values [OK]
Common Mistakes:
MISTAKES
  • Using = instead of IN for multiple values
  • Omitting parentheses around subquery
  • Incorrect WHERE clause conditions inside subquery

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes