The % operator in SQL is not a standalone operator—it is the wildcard character used with the LIKE operator to match any sequence of zero or more characters in a string. For example, WHERE name LIKE 'A%' finds all names starting with ‘A’. This article explains how % works, how to use it correctly, and common pitfalls.
What Exactly Is the % Operator?
In SQL, % is a wildcard that represents zero, one, or many characters. It is only meaningful when paired with LIKE (or ILIKE in PostgreSQL for case-insensitive matching). Without LIKE, % is just a modulo operator in arithmetic (e.g., SELECT 10 % 3 returns 1). In string contexts, % is used to search for patterns. For instance:
WHERE title LIKE '%SQL%'matches any title containing ‘SQL’ anywhere.WHERE title LIKE 'SQL%'matches titles starting with ‘SQL’.WHERE title LIKE '%SQL'matches titles ending with ‘SQL’.
The underscore _ is the other wildcard, matching exactly one character. So LIKE 'A_C' matches ‘ABC’ but not ‘AC’ or ‘ABBC’.
How the % Wildcard Works with LIKE
When you use LIKE, the database scans the string column and compares each value against the pattern. The % matches any sequence of characters, including an empty sequence. This makes it powerful for fuzzy searches. For example, in a customer table, you can find all emails from a domain:
SELECT * FROM customers WHERE email LIKE '%@gmail.com';
This returns rows where the email ends with ‘@gmail.com’. The leading % means ‘any characters before’. You can also combine wildcards: LIKE '%a%b%' matches any string containing an ‘a’ followed later by a ‘b’. Note that LIKE is case-sensitive in many databases (e.g., PostgreSQL with default collation, SQL Server with default collation), but case-insensitive in MySQL by default. To force case-insensitivity in PostgreSQL, use ILIKE or LOWER(column) LIKE LOWER(pattern).
Using % in Different SQL Databases
The % wildcard works across all major SQL databases—MySQL, PostgreSQL, SQL Server, SQLite, Oracle—with slight syntax differences. In SQL Server, you can also use % with LIKE in the same way. Oracle requires LIKE and treats % identically. SQLite also supports LIKE with %. In all cases, the pattern is a string literal, and you can escape % if you need to match a literal percent sign. For example, to find strings containing ‘50%’, use LIKE '%50\%%' ESCAPE '\' (the backslash is the escape character). In MySQL, the default escape is backslash, so you can write LIKE '%50\%%' without specifying ESCAPE. In SQL Server, use LIKE '%50[%]%' with brackets to escape.
Common Problems and Misconceptions
One common mistake is using % without LIKE, like WHERE name = '%John%'—this will not match anything because it looks for the literal string ‘%John%’. Another issue is performance: leading wildcards (e.g., '%John') prevent the database from using an index, causing full table scans. If you have a large table, consider using full-text search or a reverse index. Also, remember that LIKE with % does not match NULL values—you need IS NULL separately. A frequent misconception is that % matches any number of characters including newlines, which it does, but in some databases, newline handling depends on collation. Finally, be careful with user input: always parameterize queries to avoid SQL injection; never concatenate user input directly into a LIKE pattern without escaping.
How to Choose Between LIKE, =, and Full-Text Search
Use = for exact matches, LIKE for simple pattern matching, and full-text search for complex text queries. For example, if you need to find a product by exact SKU, use WHERE sku = 'ABC123'. If you need to find products with ‘blue’ in the name, use LIKE '%blue%'. But if you need to search for multiple words with ranking, stemming, or fuzzy matching, use full-text search (e.g., MySQL’s MATCH ... AGAINST, PostgreSQL’s tsvector). Full-text is faster for large texts and supports language-aware search. For simple prefix searches (e.g., ‘John%’), a regular index can help if you use LIKE 'John%' (without leading wildcard).
Performance and Indexing with %
When you use LIKE 'abc%' (no leading wildcard), the database can use a B-tree index on that column. However, LIKE '%abc%' cannot use a standard index because the wildcard is at the start. To speed up such searches, you can create a reverse index (store the reversed string) or use a trigram index in PostgreSQL (with the pg_trgm extension). In MySQL, you can use FULLTEXT indexes for better performance on large text columns. For small tables, the performance difference is negligible, but for millions of rows, a leading wildcard can be very slow. Always test with EXPLAIN to see if the query uses an index.
Alternatives to LIKE with %
If you need more advanced pattern matching, consider:
- Regular expressions: Use
REGEXP_LIKEin Oracle,REGEXPin MySQL,~in PostgreSQL, orLIKEwith regex in SQLite (viaREGEXP). These offer more flexibility (e.g.,WHERE name ~ '^J.*n$'). - Full-text search: For natural language queries, use built-in full-text features.
- SOUNDEX: For phonetic matching, use
SOUNDEXin SQL Server or MySQL. - String functions: Use
CHARINDEX(SQL Server),INSTR(Oracle/MySQL), orPOSITION(PostgreSQL) to find a substring and then filter manually—though this is often less efficient than LIKE.
Each has trade-offs in syntax and performance. For most simple substring searches, LIKE '%...%' is sufficient and readable.
Frequently Asked Questions
Can I use % with the equals sign?
No, = performs an exact match. To use wildcards, you must use LIKE. For example, WHERE name = 'A%' looks for the literal ‘A%’, not names starting with A.
How do I search for a literal percent sign?
Use an escape character. In MySQL, you can write LIKE '%50\%%' (backslash escapes the first %). In SQL Server, use LIKE '%50[%]%'. In PostgreSQL, use LIKE '%50\%%' ESCAPE '\'.
Does % match newline characters?
Yes, % matches any character, including newlines, in most databases. However, in some collations, newline handling might differ, but generally it matches.
Is LIKE case-sensitive?
It depends on the database and collation. MySQL is case-insensitive by default, while PostgreSQL and SQL Server are case-sensitive by default. Use ILIKE in PostgreSQL or LOWER() to make it case-insensitive.
Can I use % in a JOIN condition?
Yes, you can use LIKE in a JOIN, e.g., ON a.name LIKE b.pattern. But be aware of performance implications—it may cause a full table scan.
Why does my query with % return unexpected results?
Check for NULL values (they are not matched by LIKE), check case sensitivity, and ensure you are not using = instead of LIKE. Also, verify that the column data type is a string (CHAR, VARCHAR, TEXT).
Related
- COUNTIF in Google Sheets: Formula & Examples
- PS30 Drill Driver: Specs, Use, and Buying Guide
- Cell Counting Techniques: Hemocytometer, Automated, and More
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Google Sheets Timestamp: How to Create OneTo create a fixed timestamp in Google Sheets, select a cell and press Ctrl+; to insert the date,…
- Excel Custom Number Format for MillionsTo display Excel values in millions without changing the stored numbers, apply the custom number format #,##0.0,,"M". The…
- Create a Number List in Excel: Easy Steps and MethodsTo create a numbered list in Excel, enter 1 in the first cell and 2 in the cell…
- Excel Goal Seek Shortcut Key: Open and Use the ToolIn Excel for Microsoft 365 on Windows, press Alt+A, W, G in sequence to open Goal Seek; do…
- Wedding Budget Spreadsheet for 200 Guests: Costs & SetupA useful wedding budget spreadsheet for 200 guests lists each cost, a planned amount, vendor commitments, and the…
- Advanced Filter in Google Sheets: Methods and ExamplesGoogle Sheets does not have a single command named “Advanced Filter” like Microsoft Excel. To filter a table…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.