SQL LIKE Operator and Wildcards
= only finds exact matches. LIKE finds patterns: names that start with “A”, emails at a certain domain, product names that contain “Desk”. Two wildcard characters do the work: % for any number of characters and _ for exactly one.
- Use
%and_to match patterns - Write starts-with, ends-with and contains searches
- Escape a literal
%or_ - Know when to use
REGEXPand why leading%is slow
The two wildcards
| Wildcard | Matches | Example pattern | Matches |
|---|---|---|---|
% | zero or more characters | 'Desk%' | Desk, Desk Lamp, Desktop |
_ | exactly one character | '_at' | Bat, Cat, Hat (not Flat) |
Sample data
| customer_id | customer_name | city | joined_on | |
|---|---|---|---|---|
| 1 | Ananya Sharma | ananya@example.com | Mumbai | 2024-01-15 |
| 2 | Rohit Kumar | rohit.k@example.com | Delhi | 2024-02-03 |
| 3 | Fatima Khan | fatima@example.com | Hyderabad | 2024-03-21 |
| 4 | David Fernandes | NULL | Goa | 2024-05-09 |
| 5 | Ishita Bose | ishita@example.com | Kolkata | 2024-07-30 |
| 6 | Manoj Pillai | manoj.p@example.com | Chennai | 2025-01-11 |
| 7 | Sara Thomas | sara.t@example.com | Bengaluru | 2025-03-02 |
Starts with
Customers whose name starts with “R”:
SELECT customer_nameFROM customersWHERE customer_name LIKE 'R%';| customer_name |
|---|
| Rohit Kumar |
- The pattern
'R%'means: anR, then anything (or nothing). - Only Rohit Kumar starts with R.
- MySQL’s default collation is case insensitive, so
'r%'would match too.
Ends with
Customers with an example.com email address:
SELECT customer_name, emailFROM customersWHERE email LIKE '%@example.com';| customer_name | |
|---|---|
| Ananya Sharma | ananya@example.com |
| Rohit Kumar | rohit.k@example.com |
| Fatima Khan | fatima@example.com |
| Ishita Bose | ishita@example.com |
| Manoj Pillai | manoj.p@example.com |
| Sara Thomas | sara.t@example.com |
%at the start allows any text before@example.com.- David has no email (
NULL).NULL LIKE '...'is UNKNOWN, so his row is not returned: 6 rows.
Contains
Products whose name contains “Desk” anywhere:
SELECT product_nameFROM productsWHERE product_name LIKE '%Desk%';| product_name |
|---|
| Standing Desk |
| Desk Lamp |
%on both sides means “anything before and anything after”.- Standing Desk and Desk Lamp match.
The underscore: exactly one character
Employees whose first name has exactly four letters:
SELECT first_nameFROM employeesWHERE first_name LIKE '____';| first_name |
|---|
| Asha |
| Neha |
| Amit |
- Four underscores match exactly four characters, no more and no less.
- Asha, Neha and Amit have four-letter names.
Second letter is “a”:
SELECT first_nameFROM employeesWHERE first_name LIKE '_a%';| first_name |
|---|
| Rahul |
| Karan |
| Kavya |
NOT LIKE
SELECT product_nameFROM productsWHERE product_name NOT LIKE '%e%';| product_name |
|---|
| USB-C Hub |
| Monitor Arm |
NOT LIKE '%e%'keeps names that contain no “e” anywhere.- Because the match ignores case, a capital “E” would also count.
Escaping % and _
To search for a real percent sign or underscore, put a backslash in front of it:
CREATE TABLE coupons (code VARCHAR(20));INSERT INTO coupons VALUES ('SAVE10%'), ('SAVE100'), ('NEW_USER'), ('NEWUSER');SELECT code FROM coupons WHERE code LIKE '%10\%';SELECT code FROM coupons WHERE code LIKE 'NEW\_%';| code |
|---|
| SAVE10% |
| code |
|---|
| NEW_USER |
- In
'%10\%'the last%is escaped, so it means a literal percent sign. OnlySAVE10%ends with “10%”. - In
'NEW\_%'the underscore is literal, soNEWUSER(no underscore) is excluded.
LIKE vs REGEXP
For more complex patterns MySQL has REGEXP (also written RLIKE), which uses regular expressions:
SELECT customer_nameFROM customersWHERE customer_name REGEXP '^[A-F]';| customer_name |
|---|
| Ananya Sharma |
| Fatima Khan |
| David Fernandes |
^anchors the pattern at the start of the text.[A-F]matches one character from A to F, so names starting with A to F are returned.
Performance tip
A pattern with a fixed start, such as 'R%', can use an index on the column. A leading wildcard, '%son', cannot: MySQL must check every row. On big tables, consider a FULLTEXT index for word searches.
Exercise: Packs and pens
List the products whose name ends with “Pack” or contains “Pen”.
| product_name |
|---|
| Notebook Pack |
| Gel Pens |
Hint 1
Two LIKE conditions joined by OR.
SELECT product_nameFROM productsWHERE product_name LIKE '%Pack' OR product_name LIKE '%Pen%';| product_name |
|---|
| Notebook Pack |
| Gel Pens |
Exercise: Last names with “a” in second position
List employees whose last_name has the letter “a” as its second letter.
| first_name | last_name |
|---|---|
| Meera | Kapoor |
| Asha | Rao |
| Priya | Nair |
| Arjun | Das |
Hint 1
One underscore, then a, then %.
SELECT first_name, last_nameFROM employeesWHERE last_name LIKE '_a%';| first_name | last_name |
|---|---|
| Meera | Kapoor |
| Asha | Rao |
| Priya | Nair |
| Arjun | Das |
Quick quiz
1. What does % match in a LIKE pattern?
% can match nothing at all, so 'Desk%' matches “Desk”.
2. Which pattern finds names with exactly three characters?
Each underscore stands for exactly one character.
3. Does WHERE email LIKE '%' return rows where email is NULL?
NULL LIKE anything is UNKNOWN, so NULL rows are excluded.
4. Which search cannot use a normal index?
A leading wildcard means the start of the value is unknown.
LIKE operator FAQ
What are wildcards in SQL?
Wildcards are special characters in a LIKE pattern: % matches any sequence of characters and _ matches exactly one character.
Is LIKE case sensitive in MySQL?
Not with the default case-insensitive collation. For a case-sensitive match, use LIKE BINARY 'R%' or a column with a _cs or _bin collation.
How do I search for a percent sign with LIKE?
Escape it with a backslash: LIKE '%10\%'. You can also choose your own escape character: LIKE '%10!%' ESCAPE '!'.
What is the difference between LIKE and = in SQL?
= compares the whole value exactly. LIKE compares against a pattern with wildcards. Without wildcards, LIKE 'abc' behaves almost like = 'abc'.