SQL LIKE: pattern matching with wildcards, explained and practiced
LIKE lets you search for part of a text value: every product with "black" in its name, every customer whose name ends in Ltd, every order from March. Instead of an exact value you write a pattern with wildcards.
In short: WHERE name LIKE '%black%' returns every row whose name contains "black". % matches any number of characters, including none; _ matches exactly one. NOT LIKE flips the condition.
What you should know first: filtering rows with WHERE and combining conditions with AND and OR.
Try it now
An equals sign only finds values spelled exactly the same. To ask "does it appear anywhere?", you need LIKE and the percent sign.
Task A customer on the phone wants to know what you stock in black. Show the name and price of every product with the word black anywhere in its name.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
How LIKE works
LIKE goes into the WHERE clause where you would otherwise put =. On its right comes a pattern in single quotes. This query finds every product whose name starts with B:
SELECT name, price
FROM products
WHERE name LIKE 'B%';
| name | price |
|---|---|
| Blue ballpoint pens, 10-pack | 3.49 |
| Black ink cartridge | 24.9 |
| Black toner XL | 89.9 |
A pattern has to fit the whole value, so 'B%' reads as "a B, then anything". Where you put the % changes the meaning completely:
| Pattern | Meaning | Matches in products |
|---|---|---|
'B%' | starts with B | the three rows above |
'%XL' | ends with XL | Black toner XL |
'%paper%' | contains paper | Copy paper, 500 sheets |
'Gr_y' | Gr, exactly one character, then y | (for example Gray and Grey) |
The underscore shows up less often, but it's handy when you know the length: order_date LIKE '2026-03-__' matches any day in March 2026, because exactly two characters follow the month.
Another one: starts with
Dates in the practice database are stored as text in YYYY-MM-DD form. That means a single pattern can pick out a whole month.
Task Accounting is closing the books for March. Show order_id, order_date and status for every order placed in March 2026. Dates are stored as text in the format YYYY-MM-DD.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
NOT LIKE: everything except the pattern
NOT LIKE returns the rows the pattern doesn't fit. Say you want every customer outside the Boston 617 area code: WHERE phone NOT LIKE '617%'. You get 7 of the 12 customers. The two Boston numbers are gone, as intended, but so are the three customers who have no phone number at all. An empty cell in SQL is NULL, and for NULL neither LIKE nor NOT LIKE is true. The next lesson shows how to deal with that.
Upper and lower case
Whether LIKE treats B and b as the same letter depends on the database (see the table further down). These exercises run on SQLite, where LIKE ignores case for the plain letters A to Z. That's why '%black%' in the first exercise also found "Black". Characters outside that range are compared exactly, so in SQLite 'café' LIKE 'CAFÉ' is false while 'cafe' LIKE 'CAFE' is true. In PostgreSQL, plain LIKE always cares about case. The safest habit is to write the pattern the way the data is written.
One more to puzzle over: ends with
Two patterns joined with OR. Think about which part of the name has to be fixed and which part is free.
Task Compliance is checking Companies House records and needs every customer whose name ends in Ltd or in LLP. Show name and city.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
Common mistakes
Using = instead of LIKE
name = '%black%' looks for a name that literally starts with a percent sign. The exercise tells you "With = SQL looks for the text literally, including the %. Wildcards only work with LIKE."
A pattern without wildcards
name LIKE 'black' behaves almost like =: the whole name would have to be "black". You'll see "Your query returns no rows". For "contains", put a % on both sides.
The % on the wrong side
'Black%' means "starts with Black". It happens to find both products in the visible data, but a product named "Gel pens, black, 3-pack" would be missed. That's exactly what the hidden test data checks, and the exercise answers with "The result is right with this data but not with other data".
Double quotes around the pattern
LIKE "%black%" works by accident in some databases. In PostgreSQL and SQL Server double quotes mean a column name, so it fails. The exercise points this out and shows the version with single quotes.
OR without a second LIKE
name LIKE '%Ltd' OR '%LLP' reads like English but isn't a condition: the second pattern isn't compared with anything. The exercise reminds you that after OR each condition needs a column again: each side of OR gets its own column and its own LIKE.
LIKE in MySQL, PostgreSQL, SQL Server and SQLite
% and _ mean the same everywhere. The differences are in case handling and extras:
| Database | Case sensitivity of LIKE | Extras |
|---|---|---|
| MySQL | depends on the collation; the default utf8mb4_0900_ai_ci is case-insensitive | default escape character is the backslash |
| PostgreSQL | LIKE is case-sensitive; ILIKE ignores case according to the active locale | ILIKE is a PostgreSQL extension; default escape character is the backslash |
| SQL Server | depends on the collation | also [a-f] (one character from the set) and [^a-f] (one character not in it) |
| SQLite (these exercises) | A to Z ignore case; other characters such as é or ö are case-sensitive | escape character only via ESCAPE |
Sources: MySQL Reference Manual (String Comparison Functions, Case Sensitivity in String Searches), PostgreSQL documentation (Pattern Matching), Microsoft Learn (LIKE, Transact-SQL), sqlite.org (SQL Language Expressions).
FAQ
How do I search for an actual % or _?
Pick an escape character and declare it: WHERE note LIKE '%10!%%' ESCAPE '!' matches "10% off" but not "100 sheets". The ! says the next % is a real percent sign. The ESCAPE clause works in all four databases.
Can I test several patterns at once?
Not in one LIKE. Combine conditions with OR: name LIKE '%Ltd' OR name LIKE '%LLP'. As soon as an AND joins in, put parentheses around the OR part.
What's the difference between LIKE and =?
= compares the whole value exactly, with no wildcards. LIKE compares against a pattern where % and _ leave parts open. Without wildcards the two are almost the same, except that LIKE can be more relaxed about case, depending on the database.
Does LIKE find empty cells?
No. If a cell holds nothing at all (NULL), neither LIKE '%' nor NOT LIKE is true. You find empty cells with IS NULL, which is the next lesson.