Beginner LevelSQL & MySQL
How do LIKE wildcards (% and _) execute pattern matching in SQL?
❓ Interview Prompt: Explain `LIKE` operator, percent wildcard (`%`), underscore wildcard (`_`), case sensitivity, and index performance pitfalls.
The 'LIKE' operator filters string columns based on pattern matching wildcards:
Wildcard Symbols:
•
'%' (Percent): Matches zero, one, or multiple characters (e.g. ''A%'' matches ''Alice'', ''Bob%'' matches ''Bobby'').
•
'_' (Underscore): Matches exactly one single character (e.g. ''c_t'' matches ''cat'' or ''cut'').
•
⚠️ Index Caveat: Prefix wildcards ('LIKE '%term'') CANNOT use standard B-Tree indexes, triggering full table scans!
Code Implementation Example
sql
-- Efficient leading-text search (uses B-Tree index)
SELECT id, email
FROM users
WHERE email LIKE 'admin@%';💡Pattern matching emails with leading text using LIKE.
🎯 Interview Key Takeaway
Avoid leading wildcard searches (LIKE '%term') on large tables as they bypass B-Tree indexes and cause full table scans.
#SQL#LIKE#Wildcards#Pattern Matching#Beginner
Back to all SQL & MySQL Questions→