SQL Tutorial › Querying Data › LIKE & Wildcards

SQL LIKE Operator and Wildcards

BeginnerLesson 14 of 699 min read8 queries run on MySQL 8.4

= 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.

Advertisement
In this lesson
  • Use % and _ to match patterns
  • Write starts-with, ends-with and contains searches
  • Escape a literal % or _
  • Know when to use REGEXP and why leading % is slow

The two wildcards

WildcardMatchesExample patternMatches
%zero or more characters'Desk%'Desk, Desk Lamp, Desktop
_exactly one character'_at'Bat, Cat, Hat (not Flat)

Sample data

customers · 7 rows
customer_idcustomer_nameemailcityjoined_on
1Ananya Sharmaananya@example.comMumbai2024-01-15
2Rohit Kumarrohit.k@example.comDelhi2024-02-03
3Fatima Khanfatima@example.comHyderabad2024-03-21
4David FernandesNULLGoa2024-05-09
5Ishita Boseishita@example.comKolkata2024-07-30
6Manoj Pillaimanoj.p@example.comChennai2025-01-11
7Sara Thomassara.t@example.comBengaluru2025-03-02

Starts with

Customers whose name starts with “R”:

SQL
SELECT customer_nameFROM customersWHERE customer_name LIKE 'R%';
ResultMySQL 8.4
customer_name
Rohit Kumar
1 row in set
How this query works, step by step
  1. The pattern 'R%' means: an R, then anything (or nothing).
  2. Only Rohit Kumar starts with R.
  3. MySQL’s default collation is case insensitive, so 'r%' would match too.

Ends with

Customers with an example.com email address:

SQL
SELECT customer_name, emailFROM customersWHERE email LIKE '%@example.com';
ResultMySQL 8.4
customer_nameemail
Ananya Sharmaananya@example.com
Rohit Kumarrohit.k@example.com
Fatima Khanfatima@example.com
Ishita Boseishita@example.com
Manoj Pillaimanoj.p@example.com
Sara Thomassara.t@example.com
6 rows in set
How this query works, step by step
  1. % at the start allows any text before @example.com.
  2. David has no email (NULL). NULL LIKE '...' is UNKNOWN, so his row is not returned: 6 rows.

Contains

Products whose name contains “Desk” anywhere:

SQL
SELECT product_nameFROM productsWHERE product_name LIKE '%Desk%';
ResultMySQL 8.4
product_name
Standing Desk
Desk Lamp
2 rows in set
How this query works, step by step
  1. % on both sides means “anything before and anything after”.
  2. Standing Desk and Desk Lamp match.

The underscore: exactly one character

Employees whose first name has exactly four letters:

SQL
SELECT first_nameFROM employeesWHERE first_name LIKE '____';
ResultMySQL 8.4
first_name
Asha
Neha
Amit
3 rows in set
How this query works, step by step
  1. Four underscores match exactly four characters, no more and no less.
  2. Asha, Neha and Amit have four-letter names.

Second letter is “a”:

SQL
SELECT first_nameFROM employeesWHERE first_name LIKE '_a%';
ResultMySQL 8.4
first_name
Rahul
Karan
Kavya
3 rows in set

NOT LIKE

SQL
SELECT product_nameFROM productsWHERE product_name NOT LIKE '%e%';
ResultMySQL 8.4
product_name
USB-C Hub
Monitor Arm
2 rows in set
How this query works, step by step
  1. NOT LIKE '%e%' keeps names that contain no “e” anywhere.
  2. 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:

MySQL
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\_%';
ResultMySQL 8.4
mysql> CREATE TABLE coupons (code VARCHAR(20))
Query OK, 0 rows affected
mysql> INSERT INTO coupons VALUES ('SAVE10%'), ('SAVE100'), ('NEW_USER'), ('N…
Query OK, 4 rows affected
mysql> SELECT code FROM coupons WHERE code LIKE '%10\%'
code
SAVE10%
1 row in set
mysql> SELECT code FROM coupons WHERE code LIKE 'NEW\_%'
code
NEW_USER
1 row in set
How this query works, step by step
  1. In '%10\%' the last % is escaped, so it means a literal percent sign. Only SAVE10% ends with “10%”.
  2. In 'NEW\_%' the underscore is literal, so NEWUSER (no underscore) is excluded.

LIKE vs REGEXP

For more complex patterns MySQL has REGEXP (also written RLIKE), which uses regular expressions:

MySQL
SELECT customer_nameFROM customersWHERE customer_name REGEXP '^[A-F]';
ResultMySQL 8.4
customer_name
Ananya Sharma
Fatima Khan
David Fernandes
3 rows in set
How this query works, step by step
  1. ^ anchors the pattern at the start of the text.
  2. [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”.

Expected resultMySQL 8.4
product_name
Notebook Pack
Gel Pens
2 rows in set
Hint 1

Two LIKE conditions joined by OR.

Exercise: Last names with “a” in second position

List employees whose last_name has the letter “a” as its second letter.

Expected resultMySQL 8.4
first_namelast_name
MeeraKapoor
AshaRao
PriyaNair
ArjunDas
4 rows in set
Hint 1

One underscore, then a, then %.

Quick quiz

1. What does % match in a LIKE pattern?

2. Which pattern finds names with exactly three characters?

3. Does WHERE email LIKE '%' return rows where email is NULL?

4. Which search cannot use a normal index?

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'.