SQL LIKE: search for a pattern in text
The equality operator = only finds identical text. To find the names that start with a letter, the emails that contain a domain or the products that end with a word, you use LIKE with wildcard characters.
Syntax
WHERE column LIKE 'pattern'How it works
A LIKE pattern can contain two wildcards:
| Wildcard | Stands for | Example | Finds |
|---|---|---|---|
% | any sequence of characters (even empty) | 'B%' | Benali, Bonnet, B |
_ | exactly one character | '_ade' | Jade, Wade |
The most common patterns: 'abc%' (starts with), '%abc' (ends with) and '%abc%' (contains).
In SQLite, LIKE ignores case for unaccented letters: 'b%' also finds “Bernard”. This behaviour depends on the database: PostgreSQL is case-sensitive (it offers ILIKE), MySQL depends on the collation.
Diagram
| prenom | nom |
|---|---|
| Léa | Benali |
| Hugo | Martin |
| Inès | Bonnet |
| Adam | Roux |
| prenom | nom |
|---|---|
| Léa | Benali |
| Inès | Bonnet |
Simplified diagram to show the idea. The examples below use the real SQL Quest practice database (a fictional online shop: see the full schema).
Glossary: the sample database uses French names
clients- customers
commandes- orders
lignes_commande- order lines
produits- products
fournisseurs- suppliers
employes- employees
prenom / nom- first name / last name (or name)
ville- city
pays- country
prix- price
prix_unitaire- unit price
quantite- quantity
categorie- category
date_commande- order date
poste- job title
departement- department
salaire- salary
statut- status: livrée = delivered, expédiée = shipped, en cours = in progress, annulée = cancelled
Examples with their results
1. Starts with
The customers whose last name starts with “B”.
SELECT prenom, nom
FROM clients
WHERE nom LIKE 'B%'
LIMIT 8; Try it in the sandbox| prenom | nom |
|---|---|
| Léa | Blanc |
| Sarah | Benali |
| Lucas | Blanc |
| Léa | Benali |
| Karim | Bernard |
| Sarah | Blanc |
| Chloé | Bonnet |
| Emma | Bernard |
8 rows in total
2. Contains
The products whose name contains “de”.
SELECT nom, categorie
FROM produits
WHERE nom LIKE '%de%'; Try it in the sandbox| nom | categorie |
|---|---|
| Tapis de yoga | Sport |
| Gourde inox | Sport |
| Corde à sauter | Sport |
| Ballon de foot | Sport |
| Guide SQL | Livres |
| Livre de cuisine | Livres |
6 rows in total
3. Ends with
The products whose name ends with “e”.
SELECT nom
FROM produits
WHERE nom LIKE '%e'; Try it in the sandbox| nom |
|---|
| Clavier mécanique |
| Bougie parfumée |
| Cafetière |
| Vase céramique |
| Raquette |
| BD aventure |
| Livre de cuisine |
| Huile d'olive |
8 rows in total
4. One exact character with _
The 4-letter first names that end with “a”.
SELECT DISTINCT prenom
FROM clients
WHERE prenom LIKE '___a'; Try it in the sandbox| prenom |
|---|
| Emma |
| Lina |
2 rows in total
Common mistakes
Forgetting the % wildcard
Without %, LIKE 'B' looks for exactly the text “B” and finds nothing.
SELECT * FROM clients WHERE nom LIKE 'B';SELECT * FROM clients WHERE nom LIKE 'B%';Using = with a wildcard
The = sign does not understand wildcards: it literally looks for the % character.
SELECT * FROM clients WHERE nom = 'B%';SELECT * FROM clients WHERE nom LIKE 'B%';Searching without the accents
LIKE does not treat “é” as “e”. '%Lea%' does not find “Léa”.
SELECT * FROM clients WHERE prenom LIKE 'Lea';SELECT * FROM clients WHERE prenom LIKE 'Léa';Exercises
Try writing the query yourself before opening the solution.
Challenge 1. Show the customers whose email address contains “martin”.
Show a solution
SELECT prenom, nom, email FROM clients WHERE email LIKE '%martin%';| prenom | nom | |
|---|---|---|
| Samir | Martin | samir.martin2@mail.com |
| Lucas | Martin | lucas.martin41@mail.com |
| Théo | Martin | theo.martin62@mail.com |
| Hugo | Martin | hugo.martin75@mail.com |
| Lina | Martin | lina.martin82@mail.com |
| Omar | Martin | omar.martin100@mail.com |
| Théo | Martin | theo.martin107@mail.com |
7 rows in total
Try it in the sandboxChallenge 2. Show the products whose name starts with “C”.
Show a solution
SELECT nom, prix FROM produits WHERE nom LIKE 'C%';| nom | prix |
|---|---|
| Clavier mécanique | 15 |
| Casque gaming | 98 |
| Coussin | 10.86 |
| Cafetière | 47.62 |
| Corde à sauter | 43.19 |
| Café en grains | 21.79 |
| Chocolat noir | 22.31 |
7 rows in total
Try it in the sandboxThe interactive exercises are checked automatically, with a tutor who explains your mistakes. A short lesson stays next to the editor.
Frequently asked questions
Is LIKE case-sensitive?
In SQLite, not for unaccented letters. In PostgreSQL, yes (use ILIKE to ignore case). In MySQL, it depends on the column's collation.
How do I search for an actual % character?
With the ESCAPE clause: LIKE '%10\%%' ESCAPE '\' finds the text containing “10%”.
Is LIKE '%word%' slow?
On large tables, yes: a pattern starting with % prevents the use of an index. For advanced text search, databases offer full-text indexes.