Subqueries with EXISTS in MySQL - Time & Space Complexity
When using subqueries with EXISTS, we want to know how the time to run the query changes as the data grows.
We ask: How many checks does the database do as the tables get bigger?
Analyze the time complexity of the following code snippet.
SELECT customer_id, customer_name
FROM customers c
WHERE EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);
This query finds all customers who have at least one order by checking if an order exists for each customer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each customer, the database checks the orders table to find a matching order.
- How many times: This check happens once per customer, so as many times as there are customers.
As the number of customers grows, the database does more checks in the orders table for each customer.
| Input Size (n customers) | Approx. Operations |
|---|---|
| 10 | Checks orders for 10 customers |
| 100 | Checks orders for 100 customers |
| 1000 | Checks orders for 1000 customers |
Pattern observation: The work grows roughly in direct proportion to the number of customers.
Time Complexity: O(n * m)
This means the time to run the query grows linearly with the number of customers and the number of orders per customer.
[X] Wrong: "The EXISTS subquery runs once and then applies to all customers."
[OK] Correct: The subquery runs separately for each customer to check if an order exists, so it repeats many times.
Understanding how EXISTS works helps you explain query performance clearly and shows you can think about how databases handle data step by step.
"What if we added an index on orders.customer_id? How would the time complexity change?"
Practice
EXISTS keyword do in a MySQL query?Solution
Step 1: Understand the purpose of EXISTS
EXISTS checks if the subquery returns at least one row.Step 2: Compare with other options
Counting rows, deleting rows, and joining tables are different operations.Final Answer:
Checks if a subquery returns any rows and returns TRUE or FALSE -> Option CQuick Check:
EXISTS means row presence check = B [OK]
- Confusing EXISTS with COUNT
- Thinking EXISTS deletes rows
- Mixing EXISTS with JOIN
EXISTS in a WHERE clause?Solution
Step 1: Recall EXISTS syntax
EXISTS must be followed by a subquery in parentheses.Step 2: Check each option
SELECT * FROM table WHERE EXISTS (SELECT column FROM table2); uses correct syntax with parentheses; others have syntax errors.Final Answer:
SELECT * FROM table WHERE EXISTS (SELECT column FROM table2); -> Option DQuick Check:
EXISTS needs parentheses around subquery = C [OK]
- Omitting parentheses after EXISTS
- Using '=' or 'IN' with EXISTS
- Writing subquery without parentheses
customers(id, name) and orders(id, customer_id), what does this query return?SELECT name FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.id);
Solution
Step 1: Understand the EXISTS subquery
The subquery checks if there is at least one order with customer_id matching customers.id.Step 2: Interpret the WHERE clause effect
Only customers with matching orders exist, so only those customers are returned.Final Answer:
All customers who have placed at least one order -> Option BQuick Check:
EXISTS filters customers with orders = D [OK]
- Thinking it returns all customers
- Confusing orders with customers
- Assuming syntax error in subquery
SELECT * FROM employees WHERE EXISTS SELECT * FROM departments WHERE departments.id = employees.dept_id;
Solution
Step 1: Check EXISTS syntax
EXISTS requires the subquery to be enclosed in parentheses.Step 2: Verify other parts
SELECT * is allowed inside EXISTS; WHERE clause is valid; EXISTS works with multiple tables.Final Answer:
Missing parentheses around the subquery after EXISTS -> Option AQuick Check:
EXISTS needs parentheses = A [OK]
- Omitting parentheses after EXISTS
- Thinking SELECT * is invalid inside EXISTS
- Misunderstanding WHERE clause usage
products(id, name) and orders(id, product_id), which query correctly uses EXISTS to achieve this?Solution
Step 1: Understand the goal
We want products with no matching orders, so use NOT EXISTS with matching product_id.Step 2: Analyze each option
SELECT name FROM products WHERE NOT EXISTS (SELECT 1 FROM orders WHERE orders.product_id = products.id); correctly uses NOT EXISTS with orders.product_id = products.id. SELECT name FROM products WHERE EXISTS (SELECT 1 FROM orders WHERE orders.product_id = products.id); finds products with orders. Options C and D compare wrong columns.Final Answer:
SELECT name FROM products WHERE NOT EXISTS (SELECT 1 FROM orders WHERE orders.product_id = products.id); -> Option AQuick Check:
NOT EXISTS filters products without orders = A [OK]
- Using EXISTS instead of NOT EXISTS
- Comparing wrong columns in subquery
- Confusing product_id with order id
Ready to prove it?
Pass the test and this pattern moves unknown
