Pattern matching with LIKE and ILIKE in PostgreSQL - Time & Space Complexity
When we use pattern matching with LIKE or ILIKE in PostgreSQL, the database checks each row to see if the text matches the pattern.
We want to understand how the time it takes grows as the number of rows increases.
Analyze the time complexity of the following query.
SELECT *
FROM products
WHERE name LIKE 'A%';
This query finds all products whose names start with the letter 'A'.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row's 'name' column against the pattern.
- How many times: Once for every row in the 'products' table.
As the number of rows grows, the database must check more names against the pattern.
| 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 run the query grows in a straight line as the table gets bigger.
[X] Wrong: "LIKE queries always use indexes and run fast regardless of table size."
[OK] Correct: If the pattern starts with a wildcard (like '%abc'), indexes usually can't help, so the database checks every row, making the query slower as the table grows.
Understanding how pattern matching scales helps you explain query performance clearly and shows you know how databases work under the hood.
"What if we changed the pattern from 'A%' to '%A'? How would the time complexity change and why?"
Practice
LIKE and ILIKE in PostgreSQL is true?Solution
Step 1: Understand the difference between LIKE and ILIKE
LIKEmatches patterns considering case, so 'A' and 'a' are different.ILIKEignores case differences.Step 2: Evaluate the options
LIKEis case-sensitive, whileILIKEis case-insensitive correctly states the case sensitivity difference. The other options are incorrect based on PostgreSQL behavior.Final Answer:
LIKEis case-sensitive, whileILIKEis case-insensitive. -> Option AQuick Check:
LIKE case-sensitive = A [OK]
- Confusing LIKE and ILIKE case sensitivity
- Thinking ILIKE works in all SQL databases
- Assuming LIKE ignores case by default
name starts with 'Jo' using case-insensitive matching?Solution
Step 1: Identify the pattern for names starting with 'Jo'
The pattern should be 'Jo%' to match strings starting with 'Jo'.Step 2: Choose case-insensitive matching syntax
UseILIKEfor case-insensitive matching in PostgreSQL. SELECT * FROM users WHERE name ILIKE 'Jo%'; usesILIKE 'Jo%', which is correct.Final Answer:
SELECT * FROM users WHERE name ILIKE 'Jo%'; -> Option CQuick Check:
ILIKE + 'Jo%' = C [OK]
- Using LIKE instead of ILIKE for case-insensitive search
- Using '%Jo' which matches end, not start
- Forgetting the % wildcard
products with a column code, what will the following query return?SELECT code FROM products WHERE code LIKE '_A%';Assuming the
code values are: '1A23', 'BA45', 'CA67', 'AA89', 'AB12'.Solution
Step 1: Understand the pattern '_A%'
The underscore (_) matches exactly one character, 'A' is literal, and % matches any sequence after. So the second character must be 'A'.Step 2: Check each code value
'1A23' (second char 'A') matches. 'BA45' (second 'A') matches. 'CA67' (second 'A') matches. 'AA89' (second 'A') matches. 'AB12' (second 'B') does not match.Final Answer:
1A23, BA45, CA67, AA89 -> Option DQuick Check:
Second char 'A' = D [OK]
- Confusing _ with %
- Ignoring position of characters in pattern
- Missing some matching rows
SELECT * FROM users WHERE email LIKE '%@example.com';But it misses emails like 'User@Example.com'. What is the fix?
Solution
Step 1: Identify why LIKE misses case differences
LIKEis case-sensitive, so 'User@Example.com' does not match '%@example.com'.Step 2: Explore fixes for case-insensitive matching
Change LIKE to ILIKE:email ILIKE '%@example.com'usesILIKEwhich ignores case. Add LOWER function:LOWER(email) LIKE '%@example.com'converts email to lowercase before matching. Use UPPER function:UPPER(email) LIKE '%@EXAMPLE.COM'converts email to uppercase and matches uppercase pattern. All achieve case-insensitive matching.Step 3: Confirm all options work
All three approaches fix the problem, so the correct answer is all of the above.Final Answer:
All of the above -> Option BQuick Check:
Case-insensitive fix = B [OK]
- Using LIKE without case conversion
- Forgetting to match case in pattern
- Assuming LIKE is case-insensitive
inventory where the code contains exactly three letters followed by any digits, ignoring case. Which query correctly uses pattern matching?Solution
Step 1: Understand the pattern requirements
We need exactly three letters (any case) followed by digits, and the match should be case-insensitive.Step 2: Evaluate pattern matching options
LIKE '___%' and ILIKE '___%' match any three characters followed by anything, but do not restrict to letters followed by digits. ILIKE '???%' uses invalid wildcard syntax (? is not supported in SQL LIKE/ILIKE). The ~* '^[a-zA-Z]{3}[0-9]+$' uses regex with case-insensitive flag to match exactly three letters then one or more digits to end of string, which fits perfectly.Step 3: Confirm correct syntax and behavior
~*is PostgreSQL's case-insensitive regex match operator with correct pattern.Final Answer:
SELECT * FROM inventory WHERE code ~* '^[a-zA-Z]{3}[0-9]+$'; -> Option AQuick Check:
Regex with ~* matches exact pattern = A [OK]
- Using LIKE with wildcards but no character restrictions
- Using invalid wildcard characters like '?'
- Not using case-insensitive matching properly
Ready to prove it?
Pass the test and this pattern moves unknown
