Subquery with IN operator in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using a subquery with the IN operator, we want to know how the time to run the query changes as the data grows.
We ask: How does the database handle checking many values inside the IN list?
Analyze the time complexity of the following code snippet.
SELECT employee_name
FROM employees
WHERE department_id IN (
SELECT department_id
FROM departments
WHERE location = 'New York'
);
This query finds employees who work in departments located in New York by using a subquery inside the IN operator.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each employee, the database checks if their department_id is in the list returned by the subquery.
- How many times: This check happens once per employee row, so it repeats as many times as there are employees.
Explain the growth pattern intuitively.
| Input Size (n employees) | Approx. Operations |
|---|---|
| 10 | About 10 checks against the subquery list |
| 100 | About 100 checks against the subquery list |
| 1000 | About 1000 checks against the subquery list |
Pattern observation: The number of checks grows directly with the number of employees.
Time Complexity: O(n * m)
This means the time to run the query grows roughly in proportion to the number of employees times the number of departments in New York.
[X] Wrong: "The subquery runs once and does not affect the total time much."
[OK] Correct: The subquery result is checked for each employee, so its size and how the database handles it impact the total time.
Understanding how subqueries with IN work helps you explain query performance clearly and shows you can think about how databases handle data behind the scenes.
"What if we replaced the IN operator with an EXISTS clause? How would the time complexity change?"
Practice
IN operator do when used with a subquery in SQL?Solution
Step 1: Understand the role of IN operator
The IN operator compares a value to a list of values and returns true if it matches any of them.Step 2: Understand subquery usage
The subquery returns a list of values that the main query uses to filter rows with the IN operator.Final Answer:
It checks if a value matches any value returned by the subquery. -> Option AQuick Check:
IN with subquery = match any value [OK]
- Thinking IN updates or deletes rows
- Confusing IN with JOIN
- Assuming IN creates new tables
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. Options A, B, and D have syntax errors.Final Answer:
SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments); -> Option CQuick Check:
IN syntax = IN (subquery) [OK]
- Adding = before IN
- Missing parentheses around subquery
- Placing IN before column name
Employees(emp_id, name, dept_id)Departments(dept_id, dept_name)What will this query return?
SELECT name FROM Employees WHERE dept_id IN (SELECT dept_id FROM Departments WHERE dept_name = 'Sales');
Solution
Step 1: Understand subquery filtering
The subquery selects dept_id values where dept_name is 'Sales'.Step 2: Main query filters employees
The main query selects employee names whose dept_id matches any dept_id from the subquery.Final Answer:
Names of employees who work in the Sales department. -> Option BQuick Check:
IN filters employees by Sales dept_id [OK]
- Thinking it returns employees outside Sales
- Assuming subquery causes error with multiple rows
- Ignoring subquery filtering condition
SELECT emp_id FROM Employees WHERE dept_id IN SELECT dept_id FROM Departments;
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, causing a 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
- Using = instead of IN for multiple values
Customers(customer_id, name)Orders(order_id, customer_id, product_id)Products(product_id, category)Which query correctly uses a subquery with IN to get these customers?
Solution
Step 1: Understand the relationships
Customers link to Orders by customer_id; Orders link to Products by product_id.Step 2: Analyze the nested subqueries
The innermost subquery selects product_ids in 'Electronics'. The middle subquery selects customer_ids from Orders with those product_ids. The outer query selects customer names with those customer_ids.Final Answer:
SELECT name FROM Customers WHERE customer_id IN (SELECT customer_id FROM Orders WHERE product_id IN (SELECT product_id FROM Products WHERE category = 'Electronics')); -> Option AQuick Check:
Nested IN filters customers by Electronics orders [OK]
- Using = instead of IN for multiple customer_ids
- Comparing customer_id with product_id or order_id
- Missing nested subquery for product filtering
