Recall & Review
beginner
What does the IN operator do in a SQL query?
The IN operator checks if a value matches any value in a list or the result of a subquery. It helps filter rows where a column's value is in a set of values.
Click to reveal answer
beginner
Write a simple example of a subquery using the IN operator.
SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE location = 'New York');<br>This finds employees who work in departments located in New York.
Click to reveal answer
intermediate
Why use a subquery with IN instead of multiple OR conditions?
Using IN with a subquery is cleaner and easier to read. It also adapts automatically if the list of values changes, unlike hardcoded OR conditions.
Click to reveal answer
intermediate
Can the subquery inside IN return multiple columns?
No, the subquery inside an IN operator must return only one column. This column provides the list of values to compare against.
Click to reveal answer
intermediate
What happens if the subquery inside IN returns no rows?
If the subquery returns no rows, the IN condition evaluates to false for all rows, so no rows match the condition.
Click to reveal answer
What does this query do?<br>SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE city = 'Paris');
✗ Incorrect
The subquery finds customer IDs from Paris, and the main query finds orders from those customers.
Which is true about the subquery in an IN operator?
✗ Incorrect
The subquery must return one column with multiple rows allowed, to provide values for the IN list.
If the subquery returns no rows, what is the result of the IN condition?
✗ Incorrect
No values to match means the IN condition is false for every row.
Which is a benefit of using IN with a subquery?
✗ Incorrect
IN with subquery makes queries easier to write and read compared to many OR conditions.
What does this query return?<br>SELECT product_name FROM products WHERE category_id IN (SELECT id FROM categories WHERE name = 'Electronics');
✗ Incorrect
It returns products whose category matches the Electronics category IDs.
Explain how the IN operator works with a subquery in SQL.
Think about how you check if something is in a list of items.
You got /4 concepts.
Describe a real-life example where using a subquery with IN is helpful.
Imagine you want to find people connected to a group defined by another table.
You got /4 concepts.