LIKE Operator and Pattern Matching
You'll learn how to search text fields flexibly using wildcards to match partial strings and patterns in SQL queries.
The Core Idea
Sometimes you don't know the exact text you're searching for—maybe you want all customers whose names start with "John" or all products containing "laptop" anywhere in their description. The LIKE operator lets you search text fields using wildcards instead of exact matches.
Think of it like a word search puzzle: instead of looking for the complete word letter-by-letter, you might search for words that start with "CAT" or contain "RUN" somewhere inside them.
The Two Wildcards
SQL's LIKE operator uses two special characters:
%(percent sign): Matches zero or more characters. It's like a blank space that can be anything of any length._(underscore): Matches exactly one character.
Common Patterns
Here's what typical pattern matching looks like in a WHERE clause:
-- Names starting with 'A'
WHERE name LIKE 'A%'
-- Emails ending with '.com'
WHERE email LIKE '%.com'
-- Product descriptions containing 'wireless'
WHERE description LIKE '%wireless%'
-- Phone numbers in format '555-____' (last 4 digits vary)
WHERE phone LIKE '555-____'
-- Names exactly 5 letters long starting with 'J'
WHERE name LIKE 'J____'
Case Sensitivity
Be aware: LIKE behavior with uppercase/lowercase varies by database system. Some databases are case-sensitive by default, others aren't.
Key Takeaway: The LIKE operator with % (any characters) and _ (one character) wildcards enables flexible pattern-based text searching beyond exact equality comparisons.