IN and NOT IN operators in MySQL - Time & Space Complexity
We want to understand how the time it takes to run queries using IN and NOT IN changes as the list or table size grows.
How does the number of checks grow when we use these operators?
Analyze the time complexity of the following code snippet.
SELECT * FROM employees
WHERE department_id IN (10, 20, 30);
SELECT * FROM employees
WHERE employee_id NOT IN (SELECT manager_id FROM managers);
The first query finds employees in certain departments using a fixed list. The second finds employees whose IDs are not in the managers list.
Look for repeated checks or scans in the queries.
- Primary operation: Checking each employee against the list or subquery results.
- How many times: Once for each employee row in the table.
As the number of employees grows, the database checks more rows against the list or subquery.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks against the list or subquery |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of checks grows directly with the number of employees.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the number of rows grows.
[X] Wrong: "IN and NOT IN always run instantly no matter how big the list or table is."
[OK] Correct: The database must check each row against the list or subquery, so more rows mean more work.
Understanding how IN and NOT IN scale helps you write queries that stay fast as data grows, a useful skill in real projects.
"What if the list inside IN was replaced by a large subquery returning thousands of values? How would the time complexity change?"
Practice
IN operator do in a MySQL query?Solution
Step 1: Understand the purpose of IN operator
The IN operator is used to test if a value exists within a set of values.Step 2: Compare with other options
The options describing comparisons with all values (greater or less than all) are incorrect, as IN checks if a value matches any in the list. The option about equality to a single value is also wrong, since IN handles multiple values.Final Answer:
Checks if a value matches any value in a list -> Option CQuick Check:
IN operator = check value in list [OK]
- Confusing IN with equality (=) operator
- Thinking IN checks all values instead of any
- Mixing IN with greater than or less than comparisons
status is either 'active', 'pending', or 'closed'?Solution
Step 1: Check correct IN syntax
The correct syntax usesINfollowed by parentheses containing a comma-separated list of values.Step 2: Identify errors in other options
SELECT * FROM table WHERE status = IN ('active', 'pending', 'closed'); has an extra '=' which is invalid. SELECT * FROM table WHERE status NOT IN ('active', 'pending', 'closed'); uses NOT IN which selects rows not matching the list. SELECT * FROM table WHERE status IN 'active', 'pending', 'closed'; misses parentheses around the list.Final Answer:
SELECT * FROM table WHERE status IN ('active', 'pending', 'closed'); -> Option AQuick Check:
Correct IN syntax uses parentheses and no '=' [OK]
- Using '=' before IN
- Omitting parentheses around the list
- Confusing IN with NOT IN
employees with a column department, what will this query return?SELECT name FROM employees WHERE department NOT IN ('HR', 'Finance');Solution
Step 1: Understand NOT IN operator
NOT IN selects rows where the column value is not in the given list.Step 2: Apply to the query context
The query returns employee names whose department is not 'HR' or 'Finance'.Final Answer:
Names of employees not in HR or Finance departments -> Option BQuick Check:
NOT IN excludes listed values [OK]
- Thinking NOT IN returns only listed values
- Confusing NOT IN with IN
- Assuming syntax error with NOT IN
SELECT * FROM orders WHERE order_status NOT IN 'shipped', 'delivered';
Solution
Step 1: Check syntax of NOT IN usage
NOT IN must be followed by parentheses containing the list of values.Step 2: Identify missing parentheses
The query lists values without parentheses, causing syntax error.Final Answer:
Missing parentheses around the list after NOT IN -> Option AQuick Check:
NOT IN requires parentheses around values [OK]
- Omitting parentheses around value list
- Using quotes incorrectly around column names
- Confusing IN and NOT IN usage
NOT IN to achieve this?Solution
Step 1: Understand the goal
We want customers excluding those from USA, Canada, and Mexico.Step 2: Choose correct NOT IN syntax
SELECT customer_id FROM orders WHERE country NOT IN ('USA', 'Canada', 'Mexico'); uses NOT IN with parentheses and a list, correctly excluding those countries.Step 3: Identify errors in other options
SELECT customer_id FROM orders WHERE country IN ('USA', 'Canada', 'Mexico'); includes those countries (wrong). SELECT customer_id FROM orders WHERE country NOT IN 'USA', 'Canada', 'Mexico'; misses parentheses (syntax error). SELECT customer_id FROM orders WHERE country != ('USA', 'Canada', 'Mexico'); uses != with a list, which is invalid syntax.Final Answer:
SELECT customer_id FROM orders WHERE country NOT IN ('USA', 'Canada', 'Mexico'); -> Option DQuick Check:
NOT IN with parentheses excludes listed values [OK]
- Using != with multiple values
- Omitting parentheses after NOT IN
- Confusing IN and NOT IN
Ready to prove it?
Pass the test and this pattern moves unknown
