Tutorials › SQL Tutorial › SQL LIKE and Wildcards

SQL LIKE and Wildcards

8 min read · Lesson 10 of 24

The = operator only finds exact matches. But what if you want every product with "USB" somewhere in its name, or every customer whose contact person's name starts with "B"? For pattern matching, SQL has the LIKE operator.

Syntax

SELECT column1, ...
FROM table_name
WHERE column LIKE pattern;

A pattern is ordinary text plus wildcards, special characters that stand in for other characters.

The two wildcards

WildcardMatchesExample patternMatches
%any number of characters, including none'K%'Kisumu, Kampala, Kigali, K
_exactly one character'_akuru'Nakuru

Starts with

Customers whose city starts with "K":

Example SQL
SELECT name, city
FROM customers
WHERE city LIKE 'K%';
Try it Yourself »

Ends with

Put the % first to match the end of a value. Cities ending in "a":

Example SQL
SELECT DISTINCT city
FROM customers
WHERE city LIKE '%a';
Try it Yourself »

Contains

Put % on both sides to find text anywhere in the value. Every product with "USB" in its name, wherever it appears:

Example SQL
SELECT name, price
FROM products
WHERE name LIKE '%USB%';
Try it Yourself »

Products whose name contains the word "Drive":

Example SQL
SELECT name, category
FROM products
WHERE name LIKE '%Drive%';
Try it Yourself »

Matching exactly one character with _

The underscore matches exactly one character. Product names that have "o" as their second letter:

Example SQL
SELECT name
FROM products
WHERE name LIKE '_o%';
Try it Yourself »

This finds Noise-Cancelling Headset and Portable SSD 1TB. The pattern is checked from the start of the value: one character (any), then an "o", then anything at all.

You can repeat _ to require a fixed length. Order dates in June 2024 ('2024-06-' followed by exactly two characters):

Example SQL
SELECT order_id, order_date
FROM orders
WHERE order_date LIKE '2024-06-__';
Try it Yourself »

NOT LIKE

Add NOT to find values that do not match. Customers whose name doesn't contain "Tech":

Example SQL
SELECT name
FROM customers
WHERE name NOT LIKE '%Tech%';
Try it Yourself »

Case sensitivity

Note: In SQLite and MySQL (with default settings), LIKE ignores the case of English letters, so '%usb%' also finds "USB-C Hub". In PostgreSQL, LIKE is case-sensitive; there you use ILIKE for a case-insensitive match.
Example SQL
SELECT name
FROM products
WHERE name LIKE '%usb%';
Try it Yourself »

Searching for a literal % or _

What if the text you are searching for contains a % or _? Use ESCAPE to choose a character that means "the next character is literal":

WHERE note LIKE '%50!%%' ESCAPE '!'

Here !% means a real percent sign, so the pattern finds any text containing "50%".

Tip: A pattern that starts with % (like '%USB%') forces the database to check every row, because it cannot use an index to jump to the right place. On large tables, prefer "starts with" patterns where you can, or a full-text search feature.

Other pattern tools

Some databases offer more powerful matching. SQL Server allows character lists like [abc] inside LIKE; SQLite has GLOB (case-sensitive, using * and ?); MySQL and PostgreSQL support regular expressions. LIKE with % and _ is the part that works everywhere.

Exercise

Find customers whose name starts with 'Victoria'.

SELECT name FROM customers WHERE name LIKE ;
Exercise

Find products with 'Desk' anywhere in the name.

SELECT name FROM products WHERE name  '%Desk%';
Sign in or create a free account to track progress, earn XP, badges and a certificate.