0
0
MySQLquery~5 mins

Subqueries with IN operator in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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');
AFinds customers who ordered from Paris
BFinds customers who live anywhere except Paris
CFinds orders placed in Paris
DFinds orders from customers who live in Paris
Which is true about the subquery in an IN operator?
AIt can return multiple columns
BIt must return exactly one column
CIt cannot use WHERE clause
DIt must return only one row
If the subquery returns no rows, what is the result of the IN condition?
AFalse for all rows
BTrue for all rows
CError in query
DReturns NULL
Which is a benefit of using IN with a subquery?
AAllows multiple columns in subquery
BRuns faster than JOIN always
CSimplifies queries with many OR conditions
DReturns duplicate rows
What does this query return?<br>SELECT product_name FROM products WHERE category_id IN (SELECT id FROM categories WHERE name = 'Electronics');
AProducts in Electronics category
BCategories named Electronics
CAll products
DProducts not in Electronics category
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.