SQL Wildcard Characters – LIKE Patterns, ESCAPE Rules, and Examples | DbSchema
Table of Contents
- What SQL wildcards are
- Wildcard characters reference table
- Common pattern cookbook
- The % wildcard – zero or more characters
- The _ wildcard – exactly one character
- The [] wildcard – character set (SQL Server and Access)
- Combining wildcards
- NOT LIKE – exclude matching patterns
- Escaping wildcard characters
- Case sensitivity: LIKE vs ILIKE
- Wildcards by database
- LIKE vs REGEXP vs full-text search
- Performance tips for wildcard queries
- Use wildcards in DbSchema
- FAQ
- Conclusion
SQL wildcard characters are special symbols used with the LIKE operator to match patterns in text. They help you find rows based on partial values, such as names that start with A, product codes that end with -XL, or strings that contain a literal %.
Wildcard searches appear in search boxes, audit filters, data-cleanup jobs, and reporting queries. They also become much easier to validate when you can preview real column values in DbSchema before deciding on the final pattern.
What SQL wildcards are
A wildcard acts as a placeholder for one or more unknown characters inside a string. Wildcards are normally used with LIKE or NOT LIKE.
SELECT *
FROM products
WHERE name LIKE 'Shirt%';
This returns every product whose name starts with Shirt.
Sample table used throughout this guide:
CREATE TABLE Products (
product_id INT PRIMARY KEY,
name VARCHAR(100),
category VARCHAR(50),
price DECIMAL(10, 2)
);
INSERT INTO Products VALUES
(1, 'Shirt-Blue', 'Clothing', 19.99),
(2, 'Shirt-Red', 'Clothing', 21.99),
(3, 'Shorts-XL', 'Clothing', 34.99),
(4, 'Shoes', 'Footwear', 89.99),
(5, 'Hat', 'Accessories', 12.99),
(6, 'Belt-M', 'Accessories', 24.99),
(7, '10% Discount', 'Promotions', 0.00);
For a broader introduction to pattern matching syntax, see SQL LIKE Operator.
Wildcard characters reference table
| Wildcard | Meaning | Supported in |
|---|---|---|
% | zero or more characters | all major databases |
_ | exactly one character | all major databases |
[] | any single character in the set | SQL Server, Access |
[^] | any single character not in the set | SQL Server, Access |
- | range inside [] | SQL Server, Access |
Common pattern cookbook
This is the quickest way to map a search intent to a LIKE pattern:
| Goal | Pattern | Example |
|---|---|---|
starts with Shirt | 'Shirt%' | WHERE name LIKE 'Shirt%' |
ends with -XL | '%-XL' | WHERE name LIKE '%-XL' |
contains oes | '%oes%' | WHERE name LIKE '%oes%' |
| one unknown final character | 'Belt-_' | WHERE name LIKE 'Belt-_' |
| literal percent sign | '%\%%' ESCAPE '\' | WHERE name LIKE '%\%%' ESCAPE '\' |
| literal underscore | '%\_%' ESCAPE '\' | WHERE code LIKE '%\_%' ESCAPE '\' |
If you are building a search UI, parameterize the pattern in your application rather than concatenating raw user input.
The % wildcard – zero or more characters
% matches zero, one, or many characters. It is the most common wildcard.
Starts with
SELECT * FROM Products WHERE name LIKE 'Shirt%';
Ends with
SELECT * FROM Products WHERE name LIKE '%-XL';
Contains
SELECT * FROM Products WHERE name LIKE '%oes%';
Important behavior
% can also match an empty string. So LIKE 'Hat%' matches Hat as well as Hat-Blue.
The _ wildcard – exactly one character
_ matches exactly one character.
SELECT * FROM Products WHERE name LIKE 'Belt-_';
That matches Belt-M, Belt-L, or Belt-X, but not Belt-XL.
Combine % and _
SELECT * FROM Products WHERE name LIKE 'S____%';
This matches names that start with S and have at least five characters.
The [] wildcard – character set (SQL Server and Access)
[] is supported in SQL Server and Access. It matches one character from the listed set or range.
SELECT * FROM Products WHERE name LIKE '[BH]%';
Character range
SELECT * FROM Products WHERE name LIKE '[A-S]%';
Negation with [^]
SELECT * FROM Products WHERE name LIKE '[^S]%';
In MySQL and PostgreSQL, [] is treated as literal text inside LIKE. For character classes in those databases, use regular-expression features instead.
Combining wildcards
Wildcards can be combined for more specific patterns:
SELECT * FROM Products WHERE name LIKE 'S_i%';
This matches values that start with S, have any one character in the second position, then i.
SELECT * FROM Products WHERE name LIKE '%-%';
This finds any product name that contains a hyphen.
NOT LIKE – exclude matching patterns
Use NOT LIKE when you want rows that do not match the pattern:
SELECT * FROM Products WHERE name NOT LIKE 'Shirt%';
This is useful for cleanup reports, exception lists, and validation queries.
Escaping wildcard characters
If the data contains literal % or _ characters, escape them so they are treated as normal text.
Standard SQL ESCAPE clause
SELECT *
FROM Products
WHERE name LIKE '%\%%' ESCAPE '\';
That finds rows containing a real percent sign, such as 10% Discount.
Literal underscore example
SELECT *
FROM product_codes
WHERE code LIKE '%\_%' ESCAPE '\';
Database-specific notes
| Database | Default escape behavior | Practical tip |
|---|---|---|
| MySQL | backslash often works | still prefer ESCAPE for clarity |
| PostgreSQL | supports explicit ESCAPE | be explicit in portable SQL |
| SQL Server | no implicit wildcard escape | use ESCAPE clause |
| Oracle | no implicit wildcard escape | use ESCAPE clause |
Case sensitivity: LIKE vs ILIKE
Case sensitivity varies by database and collation.
| Database | Is LIKE case-sensitive? | Case-insensitive option |
|---|---|---|
| PostgreSQL | yes | use ILIKE |
| MySQL | often no, depends on collation | often already case-insensitive |
| SQL Server | depends on collation | use a case-insensitive collation |
| Oracle | yes | use UPPER() or LOWER() |
PostgreSQL ILIKE example
SELECT * FROM Products WHERE name ILIKE 'shirt%';
If you need row filtering and sorting around these patterns, review SQL WHERE Clause and SQL ORDER BY.
Wildcards by database
| Feature | MySQL | PostgreSQL | SQL Server | Oracle |
|---|---|---|---|---|
% wildcard | ✓ | ✓ | ✓ | ✓ |
_ wildcard | ✓ | ✓ | ✓ | ✓ |
[] character set | ✗ | ✗ | ✓ | ✗ |
ILIKE | ✗ | ✓ | ✗ | ✗ |
| regex alternative available | ✓ | ✓ | partial | ✓ |
Understanding these differences matters when you move SQL between systems or test patterns locally before shipping them to production.
LIKE vs REGEXP vs full-text search
LIKE is not the answer to every text-search problem.
| Tool | Best for | Example use |
|---|---|---|
LIKE | simple prefixes, suffixes, contains checks | name LIKE 'Shirt%' |
REGEXP or SIMILAR TO | advanced pattern rules | email format checks, complex token patterns |
| full-text search | large text bodies and relevance ranking | searching articles, comments, documentation |
If you only need simple matching, LIKE is easier and more portable. If you need complex pattern logic, use regex support. If you need ranked search over large text, use database full-text search or a dedicated search engine.
Performance tips for wildcard queries
- Avoid leading wildcards —
LIKE '%term'usually prevents normal index use - Prefer trailing wildcards —
LIKE 'term%'can often use a B-tree index - Normalize case intentionally — case-insensitive searches may benefit from expression indexes such as
LOWER(column) - Escape user-supplied
%and_characters when they should be treated literally - Use the right tool — switch to regex or full-text search when
LIKEbecomes awkward
For debugging, it helps to preview the actual values in the table before deciding on the pattern. DbSchema makes that step much faster than guessing from schema names alone.
Use wildcards in DbSchema
DbSchema makes it easy to test wildcard queries interactively before they reach production code.
- Connect to your database using the PostgreSQL JDBC driver, MySQL JDBC driver, or SQL Server JDBC driver
- Open the SQL Editor and test
LIKE,NOT LIKE, orILIKEqueries - Preview real column values in the table browser so you can design the right pattern
- Switch to the Query Builder to add filters visually
- Save the query or publish it through shared schema documentation
This is especially helpful for data-quality audits, search features, and cleanup jobs where one wildcard change can alter the result set dramatically.
For related reading, see SQL LIKE Operator, SQL SELECT, and SQL WHERE Clause.
FAQ
What is the difference between % and _ in SQL?
% matches zero or more characters. _ matches exactly one character.
Does % match an empty string?
Yes. That is why LIKE 'Hat%' matches Hat as well as longer strings that begin with Hat.
How do I search for a literal % or _ character?
Use the ESCAPE clause and prefix the wildcard with the chosen escape character, such as LIKE '%\%%' ESCAPE '\'.
Is LIKE case-sensitive in SQL?
It depends on the database and collation. PostgreSQL LIKE is case-sensitive; ILIKE is the case-insensitive alternative there.
Can I use [] wildcards in PostgreSQL or MySQL?
Not with LIKE. The [] syntax is mainly for SQL Server and Access.
Does a leading % prevent index use?
Usually yes. LIKE '%value%' often forces a scan because the database cannot jump to a specific index prefix.
Conclusion
SQL wildcard characters are essential for pattern-based filtering. % and _ work across major databases, ILIKE helps in PostgreSQL, and ESCAPE is how you search for literal wildcard characters safely.
Use DbSchema to inspect real values, test LIKE patterns interactively, and move from ad-hoc filtering to documented, repeatable queries with confidence.