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_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290

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%';
nameprice
Blue ballpoint pens, 10-pack3.49
Black ink cartridge24.9
Black toner XL89.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:

PatternMeaningMatches in products
'B%'starts with Bthe three rows above
'%XL'ends with XLBlack toner XL
'%paper%'contains paperCopy 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_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL

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_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-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:

DatabaseCase sensitivity of LIKEExtras
MySQLdepends on the collation; the default utf8mb4_0900_ai_ci is case-insensitivedefault escape character is the backslash
PostgreSQLLIKE is case-sensitive; ILIKE ignores case according to the active localeILIKE is a PostgreSQL extension; default escape character is the backslash
SQL Serverdepends on the collationalso [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-sensitiveescape 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.

Learn this next: NULL

Missing values in SQL are neither 0 nor an empty string. Learn to find them with IS NULL, replace them with COALESCE, and see why = NULL never matches anything.

Practice NULL

Write to us

Choose language

Select your preferred language