What if you could find all matching data with just one simple query instead of endless manual checks?
Why Subqueries with IN operator in MySQL? - Purpose & Use Cases
Imagine you have a big list of customers and a separate list of orders. You want to find all customers who have placed orders. Doing this by hand means checking each customer against every order one by one.
Manually comparing each customer to every order is slow and tiring. It's easy to miss some matches or make mistakes, especially when the lists are large. This wastes time and causes errors.
Using a subquery with the IN operator lets the database do the hard work. It quickly finds all customers whose IDs appear in the orders list, saving you time and avoiding mistakes.
SELECT * FROM customers WHERE id = 1 OR id = 2 OR id = 3;
SELECT * FROM customers WHERE id IN (SELECT customer_id FROM orders);
This lets you easily find related data across tables without writing long, complicated checks.
A store manager wants to see all customers who made purchases this month. Using a subquery with IN quickly lists those customers by matching customer IDs in the orders table.
Manually checking data across lists is slow and error-prone.
Subqueries with IN let the database find matches efficiently.
This makes querying related data simple and reliable.
Practice
IN operator do when used with a subquery in MySQL?Solution
Step 1: Understand the IN operator
The IN operator checks if a value exists within a list of values.Step 2: Role of subquery with IN
The subquery returns a list of values, and IN checks if the main query's value matches any of these.Final Answer:
Checks if a value matches any value returned by the subquery -> Option AQuick Check:
IN with subquery = value match list [OK]
- Confusing IN with JOIN
- Thinking IN updates or deletes rows
- Assuming IN returns a single value
Solution
Step 1: Review correct IN syntax
The IN operator must be followed by parentheses enclosing the subquery.Step 2: Check each option
SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments); correctly uses IN with parentheses and a subquery. Others have syntax errors.Final Answer:
SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments); -> Option BQuick Check:
IN + (subquery) = correct syntax [OK]
- Adding = before IN
- Missing parentheses around subquery
- Placing IN before column name
employees(id, name, department_id)departments(id, name)What will this query return?
SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales');
Solution
Step 1: Analyze subquery
The subquery selects department IDs where the department name is 'Sales'.Step 2: Main query filters employees
The main query selects employee names whose department_id matches any ID from the subquery.Final Answer:
Names of employees who work in the Sales department -> Option CQuick Check:
IN filters employees by Sales department IDs [OK]
- Thinking IN causes error with multiple rows
- Ignoring subquery filter condition
- Assuming it returns all employees
SELECT * FROM orders WHERE customer_id IN SELECT id FROM customers WHERE city = 'New York';
Solution
Step 1: Check IN operator syntax
The IN operator requires the subquery to be enclosed in parentheses.Step 2: Identify missing parentheses
The query lacks parentheses around the subquery after IN, causing syntax error.Final Answer:
Missing parentheses around the subquery after IN -> Option DQuick Check:
IN needs (subquery) [OK]
- Omitting parentheses around subquery
- Confusing IN with EXISTS
- Assuming table names cause error
Given tables:
products(id, name)orders(id, product_id, customer_id)customers(id, city)Which query correctly uses a subquery with IN to get the product names?
Solution
Step 1: Understand the relationships
Products link to orders by product_id; orders link to customers by customer_id.Step 2: Build nested subqueries
Inner subquery gets customer IDs from Chicago; middle subquery gets product_ids from orders by those customers; outer query selects product names with those IDs.Final Answer:
SELECT name FROM products WHERE id IN (SELECT product_id FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE city = 'Chicago')); -> Option AQuick Check:
Nested IN subqueries correctly filter products [OK]
- Adding = before IN
- Missing parentheses around subqueries
- Using wrong columns in subqueries
Ready to prove it?
Pass the test and this pattern moves unknown
