Searching text with LIKE
The equals sign finds exact matches. Often you know only part of what you are looking for: names that start with A, products with "kg" in them, email addresses at a certain site. LIKE matches text against a pattern.
The two wildcards
A pattern is ordinary text plus two special characters.
| Wildcard | Stands for |
|---|---|
% | any run of characters, including none at all |
_ | exactly one character |
Starts with
Put the % at the end.
The pattern 'A%' means "an A, then anything".
Ends with
Put the % at the start.
SELECT name, email FROM customers WHERE email LIKE '%@example.com';Result
| name | |
|---|---|
| Asha Verma | asha@example.com |
| Ravi Kumar | ravi@example.com |
| Kabir Khan | kabir@example.com |
| Sunita Rao | sunita@example.com |
| Priya Nair | priya@example.com |
| Imran Ali | imran@example.com |
| Neha Joshi | neha@example.com |
| Vikram Das | vikram@example.com |
| Joy Thomas | joy@example.com |
9 rows
SELECT name FROM products WHERE name LIKE '%kg';Result
| name |
|---|
| Basmati Rice 5 kg |
| Toor Dal 1 kg |
2 rows
Contains
Put a % on both sides.
This finds the text anywhere: at the start, in the middle or at the end, because % can also stand for nothing.
One character with the underscore
Each underscore must match exactly one character. This finds names whose second letter is a.
SELECT name FROM customers WHERE name LIKE '_a%';Result
| name |
|---|
| Ravi Kumar |
| Kabir Khan |
| Tara Singh |
3 rows
Underscores are useful for codes with a fixed shape. The pattern '2026-0_-__' matches every date from January to September 2026.
SELECT id, order_date FROM orders WHERE order_date LIKE '2026-0_-1_';Result
| id | order_date |
|---|---|
| 2 | 2026-01-12 |
| 5 | 2026-02-14 |
| 8 | 2026-03-15 |
| 11 | 2026-04-18 |
| 13 | 2026-05-19 |
| 17 | 2026-08-11 |
| 19 | 2026-09-17 |
7 rows
That pattern picks the days from the 10th to the 19th of those months.
NOT LIKE
The pattern '% %' matches any text with a space in it, so NOT LIKE returns the names made of a single word.
Capital and small letters
How LIKE treats case depends on the database.
- In SQLite,
LIKEignores case for the English letters A to Z. That is how the practice box behaves. - In MySQL and SQL Server, with their usual settings, it also ignores case.
- In PostgreSQL and Oracle,
LIKEis case sensitive. PostgreSQL has a separate word,ILIKE, for a search that ignores case.
To be safe in any database, turn both sides to the same case, as the lesson on functions shows.
Combining patterns
LIKE conditions join with AND and OR like any others.
SELECT name
FROM customers
WHERE name LIKE 'A%' OR name LIKE 'S%';Result
| name |
|---|
| Asha Verma |
| Sunita Rao |
| Arjun Mehta |
3 rows
There is no way to give LIKE a list of patterns in standard SQL. Repeat the test with OR.
Searching for a real percent sign
What if the text you want contains % or _ itself? Name an escape character with ESCAPE. Whatever follows that character in the pattern is taken as itself.
SELECT '50% off' LIKE '%\%%' ESCAPE '\' AS has_percent,
'half off' LIKE '%\%%' ESCAPE '\' AS no_percent;Result
| has_percent | no_percent |
|---|---|
| 1 | 0 |
1 row
The result is 1 for true and 0 for false, which is how SQLite shows the answer to a test.
LIKE can be slow
A pattern that starts with % forces the database to read every row and look inside the text. On a table of a few thousand rows nobody notices. On millions of rows it is slow. A pattern with a fixed start, such as 'A%', can use an index and stays fast. Large systems that need "contains" searches use special full-text search features instead.
LIKE or equals?
If you know the whole value, use =. It is exact and fast. Use LIKE only when you need a wildcard. A LIKE without any wildcard works but tells the reader you meant something else.
Try it yourself
Exercise 1. Find the customers whose name ends with the surname Khan or Kumar. Show the names.
Show one answer
SELECT name FROM customers WHERE name LIKE '% Khan' OR name LIKE '% Kumar';| name |
|---|
| Ravi Kumar |
| Kabir Khan |
2 rows
Exercise 2. Find the products whose name contains 1 L. Show name and price.
Show one answer
SELECT name, price FROM products WHERE name LIKE '%1 L%';| name | price |
|---|---|
| Sunflower Oil 1 L | 145 |
| Steel Bottle 1 L | 450 |
2 rows
Exercise 3. Find the employees whose name starts with a letter followed by a as the second letter. Show the names.
Show one answer
SELECT name FROM employees WHERE name LIKE '_a%';| name |
|---|
| Manoj Yadav |
| Farah Sheikh |
| Harpreet Kaur |
3 rows
Examples on this page were run with SQLite 3.50.4 on 12 November 2026. The results shown are the real results.
This is an independent learning site. It is not connected with the makers of any database product. MySQL, PostgreSQL, SQL Server, Oracle and SQLite are named only so that you know where a piece of SQL works. The shop, its customers and its staff in the practice database are made up.