Subquery with EXISTS operator in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time needed to run a query with an EXISTS subquery changes as the data grows.
Specifically, how does checking for existence inside a subquery affect performance?
Analyze the time complexity of the following code snippet.
SELECT employee_id, employee_name
FROM employees e
WHERE EXISTS (
SELECT 1
FROM sales s
WHERE s.employee_id = e.employee_id
);
This query finds all employees who have made at least one sale by checking if a matching record exists in the sales table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each employee, the database checks the sales table to see if a matching sale exists.
- How many times: This check happens once per employee row.
As the number of employees grows, the database performs more existence checks.
| Input Size (n employees) | Approx. Operations |
|---|---|
| 10 | About 10 existence checks in sales |
| 100 | About 100 existence checks in sales |
| 1000 | About 1000 existence checks in sales |
Pattern observation: The number of checks grows roughly in direct proportion to the number of employees.
Time Complexity: O(n)
This means the time to run the query grows roughly in a straight line as the number of employees increases.
[X] Wrong: "The EXISTS subquery runs once and checks all sales at the same time."
[OK] Correct: Actually, the EXISTS check runs separately for each employee, so the work grows with the number of employees.
Understanding how subqueries with EXISTS scale helps you write efficient queries and explain your reasoning clearly in interviews.
"What if we replaced EXISTS with IN? How would the time complexity change?"
Practice
EXISTS operator do in an SQL query?Solution
Step 1: Understand the purpose of EXISTS
The EXISTS operator checks if the subquery returns at least one row.Step 2: Compare with other options
Counting rows, joining tables, or deleting rows are different SQL operations unrelated to EXISTS.Final Answer:
Checks if a subquery returns any rows and returns TRUE or FALSE. -> Option AQuick Check:
EXISTS = TRUE if rows found [OK]
- Thinking EXISTS counts rows instead of checking existence
- Confusing EXISTS with JOIN operations
- Assuming EXISTS deletes or modifies data
EXISTS in a WHERE clause?Solution
Step 1: Review correct EXISTS syntax
EXISTS is used as EXISTS (subquery) without any operator like =, IN, or LIKE.Step 2: Identify incorrect options
Options B, C, and D misuse operators (=, IN, LIKE) with EXISTS, causing syntax errors.Final Answer:
SELECT * FROM table WHERE EXISTS (SELECT column FROM table2); -> Option AQuick Check:
EXISTS uses only parentheses for subquery [OK]
- Adding = or IN after EXISTS
- Using LIKE with EXISTS
- Forgetting parentheses around subquery
Customers(id, name) and Orders(id, customer_id), what does this query return?SELECT name FROM Customers c WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.customer_id = c.id);
Solution
Step 1: Understand the EXISTS subquery condition
The subquery checks if there is at least one order with customer_id matching the customer's id.Step 2: Determine which customers are returned
Only customers with matching orders exist, so only those customers' names are returned.Final Answer:
All customers who have placed at least one order. -> Option BQuick Check:
EXISTS filters customers with orders [OK]
- Thinking it returns all customers regardless of orders
- Confusing with JOIN that returns all orders
- Assuming syntax error due to subquery
SELECT * FROM Employees e WHERE EXISTS SELECT * FROM Salaries s WHERE s.emp_id = e.id;
Solution
Step 1: Check EXISTS syntax
EXISTS requires the subquery to be enclosed in parentheses.Step 2: Identify the missing parentheses
The query lacks parentheses around the subquery after EXISTS, causing a syntax error.Final Answer:
Missing parentheses around the subquery after EXISTS. -> Option CQuick Check:
EXISTS (subquery) needs parentheses [OK]
- Omitting parentheses around subquery
- Thinking SELECT * is invalid inside EXISTS
- Misunderstanding alias usage
Products(id, name) and Orders(id, product_id), which query correctly uses EXISTS to find these products?Solution
Step 1: Understand the goal
We want products with no matching orders, so the subquery should check for orders and we want those where no such orders exist.Step 2: Use NOT EXISTS correctly
NOT EXISTS with a subquery checking orders for the product returns products never ordered.Step 3: Evaluate other options
SELECT name FROM Products p WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.product_id = p.id); returns products with orders, C has invalid syntax, D uses NOT IN which can cause issues with NULLs.Final Answer:
SELECT name FROM Products p WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.product_id = p.id); -> Option DQuick Check:
NOT EXISTS finds missing related rows [OK]
- Using EXISTS instead of NOT EXISTS for missing data
- Incorrect syntax like EXISTS NOT
- Using NOT IN which may fail with NULLs
