LIKE Operator
Introduction
When the text you want fits a shape rather than one exact value, LIKE matches it against a pattern: the cities that start with sao, the names that end with polis, the categories with eletr anywhere in them.
Here you will:
- see how
LIKElays a pattern over a value - use its two wildcards,
%and_ - try common patterns on the course's tables
- leave values out with
NOT LIKE - see how SQLite treats capitals
- match a real
%or_withESCAPE - read what a pattern costs
What is the LIKE Operator?
LIKE matches text against a pattern. Instead of asking whether a value equals another, it asks whether the whole value fits the pattern.
Syntax
SELECT columns
FROM table_name
WHERE column LIKE 'pattern';
How It Works
- For each row, the database takes the column's text.
- It lays the pattern over the whole text, from its first character to its last.
- The row is kept when the pattern fits, and left out when it does not.
A pattern always covers the whole value. In PostgreSQL's manual: "LIKE pattern matching always covers the entire string." SQLite matches the same way.
Basic Example
-- Customers in cities that start with 'sao'
SELECT customer_id, customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_city LIKE 'sao%';20,988 customers live in a city whose name starts with sao: 224 cities, sao paulo, sao carlos and sao jose among them. The page shows the first 30.

The Percent Wildcard (%)
The % wildcard stands for a run of characters. SQLite's manual says a percent symbol "matches any sequence of zero or more characters in the string." Zero characters is a run too, so % can stand for nothing at all.
Starts With Pattern
Find text that starts with given characters:
-- Cities that start with 'sao'
SELECT DISTINCT customer_city
FROM olist_customers_dataset
WHERE customer_city LIKE 'sao%';224 cities. rio de janeiro and campinas are not among them.
Ends With Pattern
Find text that ends with given characters:
-- Cities that end with 'paulo'
SELECT DISTINCT customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_city LIKE '%paulo';3 cities: sao paulo (SP), morro de sao paulo (BA) and monsenhor paulo (MG). paulinia starts with pauli but does not end with paulo, so it is left out.
Contains Pattern
Find text that holds given characters anywhere:
-- Cities with 'de' anywhere in the name
SELECT DISTINCT customer_city
FROM olist_customers_dataset
WHERE customer_city LIKE '%de%';422 cities. The letters can stand inside a word: resende and trindade fit as well as rio de janeiro, and so does conde, where the last % stands for nothing.
Multiple Wildcards
A pattern can hold several %. This one asks for an a, and somewhere after it an o:
-- An 'a', and after it an 'o'
SELECT 'sao paulo' LIKE '%a%o%' AS sao_paulo,
'campinas' LIKE '%a%o%' AS campinas,
'brasilia' LIKE '%a%o%' AS brasilia;sao paulo fits; campinas and brasilia have no o after an a, so both answer 0. SQLite answers a test like this with 1 for TRUE and 0 for FALSE. Among the customers' cities, 1,649 fit '%a%o%'.
Pattern Examples Table
| Pattern | Matches | Doesn't Match |
|---|---|---|
'sao%' | sao paulo, sao carlos | rio de janeiro, campinas |
'%paulo' | sao paulo, monsenhor paulo | paulinia |
'%de%' | rio de janeiro, conde | sao paulo |
'%' | any text, even empty text | NULL |
The last row matters: % matches any text, but never a NULL.
-- '%' matches any text, but never NULL
SELECT COUNT(*) AS products
FROM olist_products_dataset
WHERE product_category_name LIKE '%';32,341 of the 32,951 products. The 610 with no category are left out: for them the test product_category_name LIKE '%' is NULL, not TRUE.
The Underscore Wildcard (_)
The _ wildcard stands for exactly one character. SQLite's manual says an underscore "matches any single character in the string."
Single Character Match
-- State codes: an 'S', then one more character
SELECT DISTINCT customer_state
FROM olist_customers_dataset
WHERE customer_state LIKE 'S_';3 states: SC, SE and SP. A value of one character, or of three, would not fit 'S_'.
Fixed Position Pattern
-- Cities of exactly three characters
SELECT DISTINCT customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_city LIKE '___';11 cities have names of exactly three characters: uba, jau, una, poa, ico, itu, acu, ipu, ipe, ita and luz. No city here is named rio or sao: those are the first words of longer names.
Combining _ with %
-- Any one character, then 'ao', then anything
SELECT DISTINCT customer_city
FROM olist_customers_dataset
WHERE customer_city LIKE '_ao%';225 cities: the 224 that start with sao, and nao-me-toque, whose first character is an n. ao alone would not fit: the _ needs a character before the ao.
Pattern Examples Table
| Pattern | Matches | Doesn't Match |
|---|---|---|
'S_' | SP, SC, SE | S, SAO |
'___' | ita, luz, uba | rio de janeiro, campinas |
'_a%' | sao paulo, campinas | brasilia |
'%__%' | any value of 2 or more characters | a value of 1 character |
Common Pattern Matching Scenarios
Scenario 1: Search Functionality
A search box often looks for the typed letters anywhere in a name:
-- A search box: cities with 'camp' anywhere
SELECT DISTINCT customer_city
FROM olist_customers_dataset
WHERE customer_city LIKE '%camp%';71 cities: campinas, campina grande, campo grande and sao bernardo do campo among them.
Scenario 2: Category Filtering
Find the categories whose names hold eletr:
-- Categories with 'eletr' in the name
SELECT DISTINCT product_category_name
FROM olist_products_dataset
WHERE product_category_name LIKE '%eletr%';4 categories: eletrodomesticos, eletrodomesticos_2, eletronicos and eletroportateis. One pattern finds them all: '%eletro%' finds the same 4, and every name that holds eletro holds eletr too, so an OR of the two adds nothing.
Scenario 3: State Code Pattern
Find the state codes of two characters:
-- State codes of two characters
SELECT DISTINCT customer_state
FROM olist_customers_dataset
WHERE customer_state LIKE '__';All 27 states. '__' checks the length, not that a code names a real state: any two characters would fit.
Scenario 4: Prefix-Based Grouping
Find the cities whose name starts with rio:
-- Cities whose name starts with 'rio'
SELECT DISTINCT customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_city LIKE 'rio%';52 rows of city and state for 48 names: 4 names, rio branco among them, are cities in two states. A prefix of a name is no region: rio branco alone stands in AC and MT, and riolandia fits too, its rio the start of one word.
Scenario 5: Suffix-Based Filtering
Find the cities whose name ends with polis:
-- Cities whose name ends with 'polis'
SELECT DISTINCT customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_city LIKE '%polis';111 rows of city and state for 109 names, florianopolis and petropolis among them.
NOT LIKE
NOT LIKE keeps the rows whose value does not fit the pattern.
Syntax
WHERE column NOT LIKE 'pattern'
Example: Exclude Specific Prefix
-- Cities that do not start with 'sao'
SELECT DISTINCT customer_city
FROM olist_customers_dataset
WHERE customer_city NOT LIKE 'sao%';3,895 of the 4,119 cities: all but the 224 that start with sao.
Example: Exclude a Category Prefix
The furniture categories all start with moveis. This finds every other category:
-- Categories that do not start with 'moveis' (furniture)
SELECT DISTINCT product_category_name
FROM olist_products_dataset
WHERE product_category_name NOT LIKE 'moveis%';67 categories: all but the 6 furniture ones, moveis_decoracao and moveis_sala among them. No category ends with moveis, so NOT LIKE '%moveis' would leave out none of them. There is no NULL row either, though 610 products have no category: NULL fits neither LIKE nor NOT LIKE, so no IS NOT NULL is needed to keep it out. Every product falls in one of three groups:
-- Every product: furniture, other, or no category at all
SELECT SUM(product_category_name LIKE 'moveis%') AS furniture,
SUM(product_category_name NOT LIKE 'moveis%') AS other,
SUM(product_category_name IS NULL) AS no_category,
COUNT(*) AS products
FROM olist_products_dataset;3,271 furniture products, 29,070 others and 610 with no category: 32,951 in all. A test that is NULL counts in neither sum.
Example: Exclude Containing Pattern
-- Cities without 'de' in the name
SELECT DISTINCT customer_city
FROM olist_customers_dataset
WHERE customer_city NOT LIKE '%de%';3,697 cities.
Combining NOT LIKE with AND
-- Cities that start with none of three prefixes
SELECT DISTINCT customer_city
FROM olist_customers_dataset
WHERE customer_city NOT LIKE 'sao%'
AND customer_city NOT LIKE 'rio%'
AND customer_city NOT LIKE 'santa%'
LIMIT 20;20 of the 3,734 cities that start with none of the three.
Case Sensitivity
How LIKE treats capitals depends on the database.
SQLite Behavior
In SQLite, LIKE ignores the case of ASCII letters:
-- The pattern in capitals finds the same customers
SELECT COUNT(*) AS customers
FROM olist_customers_dataset
WHERE customer_city LIKE 'SAO%';20,988 customers, the same as with 'sao%', though every city here is stored in small letters. The rule stops at ASCII: "The LIKE operator is case sensitive by default for unicode characters that are beyond the ASCII range."
-- ASCII letters match in either case; other letters do not
SELECT 'a' LIKE 'A' AS ascii_letter,
'ã' LIKE 'Ã' AS beyond_ascii;1 for the ASCII letter and 0 for the one beyond ASCII. The cities in this dataset are written without accents, so here the rule never bites.
For Case-Sensitive Matching
SQLite's GLOB minds the case: "Also, GLOB is case sensitive, unlike LIKE." Its * stands for any run of characters and its ? for one, as % and _ do in LIKE: customer_state GLOB 'S?' finds the same 3 states as LIKE 'S_'.
-- GLOB minds the case
SELECT COUNT(*) AS customers
FROM olist_customers_dataset
WHERE customer_city GLOB 'SAO*';0 customers: no city is stored in capitals.
-- GLOB's * is LIKE's %
SELECT COUNT(*) AS customers
FROM olist_customers_dataset
WHERE customer_city GLOB 'sao*';20,988 customers, as with LIKE 'sao%'.
Other Databases
PostgreSQL's LIKE minds the case, and its ILIKE ignores it: "The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale." On PostgreSQL 17.11, 'Sao Paulo' LIKE 'sao%' is false, while 'Sao Paulo' ILIKE 'sao%' and LOWER('Sao Paulo') LIKE 'sao%' are true.
LOWER() Changes Nothing Here
-- LOWER() first: the same customers here
SELECT COUNT(*) AS customers
FROM olist_customers_dataset
WHERE LOWER(customer_city) LIKE 'sao%';20,988 customers again. SQLite's LIKE already ignores the case of ASCII letters, and its lower() changes only those: "The default built-in lower() function works for ASCII characters only." So here LOWER() adds work to every row and finds the same rows. On PostgreSQL, where LIKE minds the case, ILIKE or LOWER() on the column is how a search ignores it.
Best Practice
Write the pattern in the case the data is stored in, here small letters. On a database whose LIKE minds the case, use its own way to ignore it.
Escaping Special Characters
% and _ are wildcards, so a pattern needs a way to ask for a real one.
The ESCAPE Clause
An ESCAPE clause names one character, and that character turns the % or _ after it into a plain one. In SQLite's words: "This character may be used in the LIKE pattern to include literal percent or underscore characters." Without the clause, SQLite has no escape character at all.
An Underscore in the Data
Underscores are common here: 56 of the 73 category names hold one, and so do the payment types credit_card, debit_card and not_defined. A _ in a pattern still means any one character:
-- The '_' is a wildcard, so 'pcs' matches too
SELECT DISTINCT product_category_name
FROM olist_products_dataset
WHERE product_category_name LIKE 'pc_%';Two categories: pc_gamer, and pcs, whose s the _ takes while the % takes nothing.
-- After the escape character, '_' is a real underscore
SELECT DISTINCT product_category_name
FROM olist_products_dataset
WHERE product_category_name LIKE 'pc!_%' ESCAPE '!';Only pc_gamer: !_ asks for a real underscore after pc.
A Percent Sign in the Data
135 review comments hold a %. In this pattern the first % and the last are wildcards, and !% is the sign itself:
-- Review comments that hold a percent sign
SELECT review_score, review_comment_message
FROM olist_order_reviews_dataset
WHERE review_comment_message LIKE '%!%%' ESCAPE '!'
LIMIT 5;The first five of the 135.
-- Comments that mention 50%
SELECT COUNT(*) AS reviews
FROM olist_order_reviews_dataset
WHERE review_comment_message LIKE '%50!%%' ESCAPE '!';5 comments mention 50%.
Other Escape Characters
Any one character can be the escape character:
-- Any one character can be the escape character
SELECT 'pc_gamer' LIKE 'pc\_%' ESCAPE '\' AS backslash,
'pcs' LIKE 'pc~_%' ESCAPE '~' AS tilde;1 and 0: with a backslash as the escape, pc_gamer fits; with a tilde, pcs does not. Without an ESCAPE clause, SQLite reads a backslash as a plain character:
-- With no ESCAPE clause, a backslash is a plain character
SELECT 'pc_gamer' LIKE 'pc\_%' AS no_escape_clause;0: the pattern asks for a backslash after pc. PostgreSQL reads it the other way: "The default escape character is the backslash but a different one can be selected by using the ESCAPE clause." On PostgreSQL 17.11, 'pc_gamer' LIKE 'pc\_%' is true and 'pcs' LIKE 'pc\_%' is false, with no ESCAPE written.
Performance Considerations
How fast a pattern runs depends on whether an index can narrow the search, and on the course's tables none can.
A Leading Wildcard Reads Every Row
A pattern that starts with % can match anywhere, so no index can help. SQLite's rule: "The string literal must not begin with a wildcard; if the right-hand side begins with a wildcard character then this optimization is not attempted." Its plan for customer_city LIKE '%paulo' reads SCAN olist_customers_dataset, every row.
A Fixed Start Needs the Right Index
A pattern with a fixed start, such as 'sao%', can use an index, but only one that ignores case the way LIKE does: "Or the optimization might occur if the column is indexed using the built-in NOCASE collating sequence and the case_sensitive_like mode is off." On the course's tables none does. customer_city has no index, so customer_city LIKE 'sao%' reads SCAN olist_customers_dataset too, every row, the same as '%paulo'. customer_state has an index, but a plain one, so customer_state LIKE 'S%' reads SCAN olist_customers_dataset as well. GLOB minds the case, and the plain index serves it: "For the GLOB operator, the column must be indexed using the built-in BINARY collating sequence." customer_state GLOB 'S*' reads SEARCH olist_customers_dataset USING INDEX idx_customers_state (customer_state>? AND customer_state<?), only the states from S up to the next letter.
On a copy of the cities with an index that ignores case (COLLATE NOCASE), SQLite's plan for LIKE 'sao%' is SEARCH cities USING COVERING INDEX cities_nocase (customer_city>? AND customer_city<?), while '%paulo' still reads SCAN cities. The course's tables are read-only, so such an index can only go on a copy you make, and the site's Run does not show a plan.
Performance Tips
- Avoid a leading wildcard when the question allows it:
'sao%'can use the right index,'%sao%'never can. - Full-text search looks words up in an index of its own, such as SQLite's FTS5. The site's SQLite is built without it:
-- Full-text search: FTS5 is not built into the site's SQLite
CREATE VIRTUAL TABLE notes USING fts5(body);The site answers no such module: fts5. FTS5 comes with SQLite's source and is turned on "by arranging for the SQLITE_ENABLE_FTS5 pre-processor symbol to be defined."
- Narrow the rows with an indexed filter first:
-- The state through its index first, then the pattern
SELECT customer_id, customer_city
FROM olist_customers_dataset
WHERE customer_state = 'SP'
AND customer_city LIKE '%camp%';3,156 customers. SQLite reads the state's rows through its index, SEARCH olist_customers_dataset USING INDEX idx_customers_state (customer_state=?): 41,746 rows instead of 99,441, and tests the pattern on those alone.
Practice
Practice the LIKE operator on the course's tables.
Exercise 1: Starts With
Find all cities that start with rio:
-- Find cities starting with 'rio'
SELECT DISTINCT customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_city LIKE 'rio%'
LIMIT 10;Ten of the 52 rows of city and state.
Exercise 2: Ends With
Find all cities that end with polis:
-- Find cities ending with 'polis'
SELECT DISTINCT customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_city LIKE '%polis'
LIMIT 10;Ten of the 111.
Exercise 3: Contains
Find all cities that contain jose anywhere in the name:
-- Find cities containing 'jose'
SELECT DISTINCT customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_city LIKE '%jose%'
LIMIT 10;Ten of the 45.
Exercise 4: Underscore Wildcard
Find all state codes that start with S:
-- Find state codes starting with 'S'
SELECT DISTINCT customer_state
FROM olist_customers_dataset
WHERE customer_state LIKE 'S_';SC, SE and SP.
Exercise 5: NOT LIKE
Find the cities that start with neither sao nor rio:
-- Find cities not starting with 'sao' or 'rio'
SELECT DISTINCT customer_city, customer_state
FROM olist_customers_dataset
WHERE customer_city NOT LIKE 'sao%'
AND customer_city NOT LIKE 'rio%'
LIMIT 10;Ten of the 4,012.
Exercise 6: Product Categories
Find the categories that contain casa (home) or moveis (furniture):
-- Find home and furniture categories
SELECT DISTINCT product_category_name
FROM olist_products_dataset
WHERE product_category_name LIKE '%casa%'
OR product_category_name LIKE '%moveis%';All 10. portateis_casa_forno_e_cafe is among them: by its English name in the translation table, small appliances for the home, oven and coffee. A pattern matches letters, not meaning. No IS NOT NULL is needed, since LIKE never keeps a NULL.
Summary
Key Takeaways
- LIKE matches text against a pattern that covers the whole value
%stands for any run of characters, even none_stands for exactly one character- NOT LIKE keeps the values that do not fit; NULL fits neither
- SQLite's LIKE ignores the case of ASCII letters; GLOB minds it
- ESCAPE turns a wildcard into a plain character
- A leading
%reads every row; a fixed start can use an index that ignores case
Wildcard Reference
| Wildcard | Matches | Example |
|---|---|---|
% | any run of characters, even none | 'sao%' matches sao paulo |
_ | exactly one character | 'S_' matches SP and SC |
Pattern Reference
-- Starts with
WHERE column LIKE 'prefix%'
-- Ends with
WHERE column LIKE '%suffix'
-- Contains
WHERE column LIKE '%middle%'
-- Exactly 3 characters
WHERE column LIKE '___'
-- Does not start with
WHERE column NOT LIKE 'prefix%'
-- A real underscore
WHERE column LIKE '%!_%' ESCAPE '!'
Performance Tips
'prefix%'can use an index that ignores case (COLLATE NOCASE); on the course's tables none does'%suffix'and'%middle%'read every row- An indexed filter beside the pattern narrows the rows first
What's Next?
This completes Filtering Data: the comparison operators (=, !=, >, <, >=, <=), the logical operators (AND, OR, NOT), ranges (BETWEEN), lists (IN) and patterns (LIKE). Intro - What is Data Combining begins SQL Joins: rows from several tables in one result.