LIKE and Wildcards
The LIKE operator searches for a pattern in a text column.
💡 LIKE is great for finding text that matches a pattern, like 'starts with A' or 'contains .com'.
The Wildcards
| Wildcard | Description | Example |
|---|---|---|
% | Matches zero or more characters | 'J%' → John, Jane |
_ | Matches exactly one character | 'J_ne' → Jane, Jone |
Examples
-- Starts with 'A'
SELECT * FROM customers WHERE first_name LIKE 'A%';
-- Ends with 'son'
SELECT * FROM customers WHERE last_name LIKE '%son';
-- Contains 'mail'
SELECT * FROM customers WHERE email LIKE '%mail%';
-- Exactly 4 letters starting with 'J'
SELECT * FROM customers WHERE first_name LIKE 'J___';
-- NOT LIKE
SELECT * FROM customers WHERE first_name NOT LIKE 'A%';
Common Patterns
| Pattern | Finds |
|---|---|
'A%' | Starts with 'A' |
'%a' | Ends with 'a' |
'%word%' | Contains 'word' |
'A_%' | Starts with 'A', at least 2 chars |
'_a%' | 'a' is the second character |
✅ Key Takeaways
- LIKE searches for patterns in text
- % matches any number of characters
- _ matches exactly one character
- Use NOT LIKE to exclude patterns
- Case sensitivity depends on the database