SQL Wildcards
SQL

SQL Wildcards — Complete Guide to Pattern Matching Characters

CHAPTER 16 of 30 SQL Tutorial — Free Course on Neotech Navigators
In the previous chapter, you learned the LIKE operator with % and _. In this chapter, we dive deeper into SQL wildcard characters — the special characters used with LIKE to build powerful search patterns. You will learn advanced combinations, escaping special characters, and database-specific wildcards.

SQL Wildcard Characters

Wildcards are special characters that represent one or more unknown characters in a pattern. They are always used with the LIKE operator inside a WHERE clause.

Wildcard Description Supported In
% Zero, one, or multiple characters All databases
_ Exactly one single character All databases
[] Any single character in the brackets SQL Server, MS Access
[^] or [!] Any character NOT in the brackets SQL Server, MS Access

We will use this Employees table:

TABLE: Employees
+----+-----------+------------+--------+-----------+
| ID | Name      | Department | Salary | City      |
+----+-----------+------------+--------+-----------+
| 1  | Priya K.  | Sales      | 55000  | Mumbai    |
| 2  | John D.   | Marketing  | 62000  | London    |
| 3  | Arun M.   | IT         | 78000  | Delhi     |
| 4  | Emma W.   | Sales      | 51000  | Sydney    |
| 5  | Ravi S.   | IT         | 72000  | Mumbai    |
| 6  | Sara L.   | HR         | 58000  | New York  |
+----+-----------+------------+--------+-----------+

The % Wildcard — Multiple Characters

The % wildcard matches any sequence of characters, including an empty string (zero characters).

Cities that contain “um”:

SQL
SELECT Name, City FROM Employees
WHERE City LIKE '%um%';
✓ OUTPUT
+-----------+--------+
| Name      | City   |
+-----------+--------+
| Priya K.  | Mumbai |
| Ravi S.   | Mumbai |
+-----------+--------+

Departments ending in “s”:

SQL
SELECT Name, Department FROM Employees
WHERE Department LIKE '%s';
✓ OUTPUT
+-----------+------------+
| Name      | Department |
+-----------+------------+
| Priya K.  | Sales      |
| Emma W.   | Sales      |
+-----------+------------+

The _ Wildcard — Single Character

The _ wildcard matches exactly one character. Use multiple underscores for multiple single characters.

Names where the second character is ‘a’:

SQL
SELECT Name FROM Employees
WHERE Name LIKE '_a%';
✓ OUTPUT
+-----------+
| Name      |
+-----------+
| Sara L.   |
| Ravi S.   |
+-----------+

Cities with exactly 5 characters:

SQL
SELECT Name, City FROM Employees
WHERE City LIKE '_____';
✓ OUTPUT
+-----------+--------+
| Name      | City   |
+-----------+--------+
| Arun M.   | Delhi  |
+-----------+--------+

Combining % and _ Wildcards

You can mix both wildcards in a single pattern for precise matching:

SQL — Names starting with any char, then ‘r’
SELECT Name FROM Employees
WHERE Name LIKE '_r%';
✓ OUTPUT
+-----------+
| Name      |
+-----------+
| Priya K.  |
| Arun M.   |
+-----------+

Common Wildcard Patterns

Pattern Meaning
'A%' Starts with A
'%a' Ends with a
'%or%' Contains “or” anywhere
'_a%' Second character is a
'A_%' Starts with A, at least 2 characters
'A__%' Starts with A, at least 3 characters
'_____' Exactly 5 characters
'%a_' Has “a” as the second-to-last character

⚠️ Performance Warning

Patterns starting with % (like '%son') cannot use indexes and may cause full table scans on large datasets. Patterns starting with a fixed prefix (like 'Jo%') are much faster because the database can use indexes.

Escaping Wildcards

If you need to search for a literal % or _ character in your data, use the ESCAPE clause:

SQL — Search for literal %
SELECT * FROM Products
WHERE Discount LIKE '%10\%%' ESCAPE '\';

The backslash tells the database to treat the next % as a literal character, not a wildcard.

For a complete reference of pattern matching syntax, see the official MySQL pattern matching documentation. In the next chapter, you will learn the SQL IN operator for filtering by a list of values.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a query that finds employees whose name has exactly 7 characters (including the space and initial like “Priya K.”).

ANSWER
SELECT Name FROM Employees
WHERE Name LIKE '_______';

📝 What You Learned in This Chapter

  • % matches zero, one, or many characters
  • _ matches exactly one character
  • [] matches any character in the set (SQL Server only)
  • Combine % and _ for advanced pattern matching
  • Patterns starting with % are slower on large tables
  • Use ESCAPE to search for literal wildcard characters

Frequently Asked Questions

What are SQL wildcards?
Wildcards are special characters used with the LIKE operator to match patterns in text data. The two standard wildcards are % (matches any number of characters) and _ (matches exactly one character). They let you search for partial matches instead of exact values.
What is the difference between % and _ in SQL?
% matches any number of characters (including zero). _ matches exactly one character. For example, 'A%' matches A, Ab, Abc, while 'A_' only matches exactly two-character strings starting with A like Ab or Ac.
Do wildcards work the same in all databases?
The % and _ wildcards work in all SQL databases. The [] bracket wildcard is specific to SQL Server and MS Access. MySQL and PostgreSQL do not support bracket wildcards but offer REGEXP for more advanced pattern matching.
How do I search for a literal % or _ character?
Use the ESCAPE clause to define an escape character. For example: WHERE col LIKE '%10\%%' ESCAPE '\' searches for the literal text “10%”. The backslash before % tells the database to treat it as a regular character.
Are wildcards case-sensitive?
It depends on the database. In MySQL and SQL Server, wildcard searches are case-insensitive by default. In PostgreSQL, they are case-sensitive. Use ILIKE in PostgreSQL or convert to lowercase with LOWER() for case-insensitive searches.

📖 Chapter 16 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com