WHERE with LIKE pattern matching in SQL - Time & Space Complexity
When using WHERE with LIKE in SQL, we want to know how the search time changes as the data grows.
How does the database find matching rows when using pattern matching?
Analyze the time complexity of the following SQL query.
SELECT *
FROM employees
WHERE name LIKE 'A%';
This query finds all employees whose names start with the letter 'A'.
Look for repeated checks or scans in the query.
- Primary operation: Checking each row's name against the pattern.
- How many times: Once per row in the employees table.
As the number of employees grows, the database checks more names.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to find matches grows in a straight line with the number of rows.
[X] Wrong: "LIKE with a pattern always uses an index and is very fast."
[OK] Correct: If the pattern starts with a wildcard (like '%abc'), the database cannot use an index efficiently and must check every row.
Understanding how pattern matching affects search time helps you explain database performance clearly and confidently.
"What if we changed the pattern to '%A'? How would the time complexity change?"
Practice
SELECT * FROM employees WHERE name LIKE 'A%';Solution
Step 1: Understand the purpose of LIKE
The LIKE keyword is used to match text patterns in SQL queries.Step 2: Identify the filtering clause
The WHERE clause filters rows, and combined with LIKE, it filters rows matching a pattern.Final Answer:
WHERE with LIKE -> Option CQuick Check:
WHERE with LIKE = B [OK]
- Confusing LIKE with GROUP BY
- Using LIKE without WHERE
- Thinking ORDER BY filters rows
Solution
Step 1: Understand the pattern for names starting with 'Jo'
Names starting with 'Jo' means 'Jo' followed by any characters, so pattern is 'Jo%'.Step 2: Match the correct syntax
SELECT * FROM users WHERE name LIKE 'Jo%'; uses LIKE 'Jo%' which matches names starting with 'Jo'. Others do not match this pattern.Final Answer:
SELECT * FROM users WHERE name LIKE 'Jo%'; -> Option BQuick Check:
Start with 'Jo' = LIKE 'Jo%' [OK]
- Using % before text to match start
- Using _ instead of %
- Misplacing wildcards
SELECT * FROM products WHERE product_code LIKE '_A%';
Solution
Step 1: Understand the pattern '_A%'
The underscore (_) matches exactly one character, so the first character can be anything. 'A' is the second character. '%' means any characters after.Step 2: Interpret the query result
The query returns rows where the second character of product_code is 'A', regardless of the first character or what follows.Final Answer:
Rows where product_code has 'A' as the second character -> Option DQuick Check:
_A% means second char is 'A' [OK]
- Thinking _ matches zero or multiple characters
- Confusing position of 'A' in pattern
- Assuming LIKE matches anywhere without wildcards
SELECT * FROM customers WHERE city LIKE '%York_';
It should find cities ending with 'York' followed by exactly one character.
Solution
Step 1: Analyze the pattern '%York_'
The % matches any characters before 'York', then 'York' literal, then _ matches exactly one character after.Step 2: Check if it matches cities ending with 'York' plus one char
This pattern correctly matches cities ending with 'York' plus exactly one character, so the query is correct.Final Answer:
The pattern is correct; no error -> Option AQuick Check:
'%York_' matches 'York' + 1 char [OK]
- Thinking _ matches multiple characters
- Confusing placement of % and _
- Assuming pattern must end with %
Solution
Step 1: Understand the requirement
The email should end with '.com' and have exactly one character before '.com'.Step 2: Analyze each pattern
SELECT * FROM employees WHERE email LIKE '%.com'; matches any email ending with '.com' but no restriction on characters before.
SELECT * FROM employees WHERE email LIKE '%_.com'; uses '%_.com' meaning any characters, then exactly one character, then '.com' at the end.
SELECT * FROM employees WHERE email LIKE '%.__.com'; expects two characters before '.com' which is incorrect.
SELECT * FROM employees WHERE email LIKE '%_.com%'; allows any characters after '.com' which is incorrect for ending.Final Answer:
SELECT * FROM employees WHERE email LIKE '%_.com'; -> Option AQuick Check:
Use _ before '.com' for exactly one char [OK]
- Using % instead of _ for single character
- Placing % after '.com' allowing extra chars
- Miscounting characters before '.com'
Ready to prove it?
Pass the test and this pattern moves unknown
