What if you could ask your database complex questions in one simple step instead of hours of manual work?
Why Subquery in WHERE clause in SQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a huge list of customers and a separate list of orders. You want to find all customers who made purchases over $100. Doing this by hand means flipping back and forth between two big lists, checking each order for each customer.
Manually scanning and matching data is slow and tiring. It's easy to miss some orders or customers, and updating the list when new orders come in means starting all over again. This manual method is error-prone and wastes a lot of time.
Using a subquery in the WHERE clause lets you ask the database to find customers based on their orders automatically. The subquery finds the orders over $100, and the main query picks customers linked to those orders. This saves time and avoids mistakes.
For each customer:
Check all orders
If any order > 100, add customer to listSELECT * FROM customers WHERE customer_id IN (SELECT customer_id FROM orders WHERE amount > 100);This lets you quickly filter data based on related information, making complex questions easy to answer with just one query.
A store manager wants to send a special offer only to customers who spent more than $100 last month. Using a subquery in the WHERE clause, they can instantly get that list without manual checks.
Manually matching data across lists is slow and error-prone.
Subqueries in WHERE clauses automate filtering based on related data.
This makes complex data questions simple and fast to answer.
Practice
WHERE clause do in SQL?Solution
Step 1: Understand the role of subqueries in WHERE clause
A subquery inside a WHERE clause is used to filter rows by comparing values to the results of another query.Step 2: Compare with other SQL operations
Creating tables, deleting, or updating rows are different SQL operations and not the purpose of subqueries in WHERE.Final Answer:
Filters rows based on the results of another query -> Option BQuick Check:
Subquery in WHERE = filter rows [OK]
- Thinking subquery creates or modifies tables
- Confusing subquery with JOIN
- Assuming subquery always returns one value
WHERE clause to find employees in departments with ID 10 or 20?Solution
Step 1: Identify correct subquery syntax with IN
The subquery must be enclosed in parentheses and used with IN to match multiple values.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.Final Answer:
SELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_id IN (10, 20)); -> Option DQuick Check:
Subquery with IN needs parentheses [OK]
- Using = instead of IN for multiple values
- Omitting parentheses around subquery
- Incorrect WHERE clause conditions inside subquery
employees(emp_id, name, department_id)departments(department_id, name)What will this query return?
SELECT name FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE name = 'Sales');
Solution
Step 1: Understand the subquery
The subquery selects department_id from departments where name is 'Sales'. This returns IDs of Sales departments.Step 2: Apply subquery results in main query
The main query selects employee names where their department_id matches any of those returned by the subquery.Final Answer:
Names of employees who work in the Sales department -> Option AQuick Check:
Subquery filters employees by Sales department [OK]
- Thinking subquery returns employee names
- Confusing department names with employee names
- Assuming subquery causes error with multiple rows
SELECT * FROM orders WHERE customer_id = (SELECT customer_id FROM customers WHERE city = 'New York');
Solution
Step 1: Analyze subquery result
The subquery selects customer_id from customers where city is 'New York'. This can return multiple customer IDs.Step 2: Check operator compatibility
The main query uses '=' which expects a single value, but subquery returns multiple rows, causing an error.Final Answer:
Subquery returns multiple rows causing an error with '=' operator -> Option CQuick Check:
Use IN for multiple subquery results [OK]
- Using = with subquery returning multiple rows
- Assuming subquery always returns one value
- Ignoring error messages about subquery results
products(product_id, name)orders(order_id, product_id)Which query correctly uses a subquery in the WHERE clause to find these products?
Solution
Step 1: Understand the goal
We want products that have never been ordered, so their product_id should NOT appear in orders.Step 2: Use NOT IN with subquery
The subquery selects all product_ids from orders. Using NOT IN filters products not in that list.Step 3: Check other options
SELECT name FROM products WHERE product_id IN (SELECT product_id FROM orders); finds products that have been ordered (opposite). SELECT name FROM products WHERE product_id = (SELECT product_id FROM orders); uses = which expects one value, causing error. SELECT name FROM products WHERE product_id NOT = (SELECT product_id FROM orders); uses NOT = which is invalid syntax for multiple values.Final Answer:
SELECT name FROM products WHERE product_id NOT IN (SELECT product_id FROM orders); -> Option AQuick Check:
NOT IN filters products never ordered [OK]
- Using = instead of IN for multiple values
- Confusing NOT IN with NOT =
- Selecting wrong table columns in subquery
