SQL

SQL Wildcard: Syntax, Usage, and Examples

An SQL wildcard is a special character used to perform pattern-based searches in a database query. Wildcards allow you to match partial values in string fields using characters like % or _.

These characters are typically used with the LIKE operator to build flexible search patterns that retrieve results even when only part of the value is known. This makes wildcard SQL queries valuable when filtering for substrings, prefixes, suffixes, or unknown characters in a column.


What Is an SQL Wildcard?

An SQL wildcard is a character that allows flexible pattern matching when querying text values in a database. These wildcards are used within string patterns passed to the LIKE or NOT LIKE operators.

For example, if you want to find all entries in a name column that start with “A”, you can use:

SELECT * FROM users WHERE name LIKE 'A%';

The % symbol matches any number of characters. The result includes “Alice”, “Aaron”, “Ava”, etc.

Wildcards in SQL enhance the querying capabilities of relational databases by supporting partial or approximate value matching.


Why Use Wildcards in SQL?

Using SQL wildcards lets you search when you only know part of the value you're looking for. This is useful for:

  • Searching for all products with a common prefix or suffix
  • Finding log entries with specific keywords
  • Locating records with variations in spelling or formatting
  • Allowing flexible search options in user-facing applications

An SQL wildcard provides more dynamic query control than exact-match filters. You can also combine wildcards with other filtering conditions to create complex queries.


Wildcard Characters in SQL

SQL supports two primary wildcard characters:

  • % matches any number of characters (including zero)
  • _ matches exactly one character

Each has specific use cases depending on what part of the value you're trying to match.

Examples:

  • LIKE 'J%' matches “John”, “Jane”, “Jake”
  • LIKE '%son' matches “Jackson”, “Emerson”
  • LIKE '_at' matches “Cat”, “Hat”, “Mat” but not “Flat”

These patterns work with most SQL database systems, including MySQL, PostgreSQL, SQL Server, and SQLite.


Syntax of SQL Wildcard Queries

To perform a wildcard SQL search, use the LIKE operator in a WHERE clause with a string pattern.

SELECT column_name FROM table_name WHERE column_name LIKE 'pattern';

You can place wildcards at the beginning, middle, or end of the string to control how values are matched.

Pattern Positions:

  • Start match: 'A%' (starts with A)
  • End match: '%ing' (ends with “ing”)
  • Contains match: '%cat%' (contains “cat”)
  • Specific character match: '__e' (three-letter words ending in “e”)

Using different patterns lets you match a wide variety of text values.


Case Sensitivity in Wildcard Queries

The behavior of wildcard searches in terms of case sensitivity depends on the SQL database engine:

  • MySQL is case-insensitive by default on non-binary strings.
  • PostgreSQL is case-sensitive unless you use ILIKE.
  • SQL Server is case-insensitive by default but can vary by collation.

Example of case-insensitive search using LOWER():

SELECT * FROM users WHERE LOWER(name) LIKE 'john%';

This ensures consistent behavior across different systems.


Escaping Special Characters

In some cases, the pattern you're searching for may contain characters like % or _, which you want to treat as literals rather than wildcards.

Use the ESCAPE clause to define a custom escape character:

SELECT * FROM logs WHERE message LIKE '%!_%' ESCAPE '!';

Here, the _ is preceded by !, which tells the engine to interpret it as a normal character.

This is useful when querying data that contains special characters or when dealing with filenames, URLs, or log strings.


Using NOT LIKE with Wildcards

You can invert wildcard searches using NOT LIKE to filter out patterns.

Examples:

  • NOT LIKE 'A%' excludes names starting with A
  • NOT LIKE '%test%' filters out entries containing “test”

Combining NOT LIKE with wildcards helps refine results by excluding unwanted matches.


SQL Wildcard in Different Columns

You can use wildcards in multiple conditions or columns within a single query.

Example:

SELECT * FROM users
WHERE name LIKE '%smith%' OR email LIKE '%@yahoo.com';

This returns users with “smith” in their name or a Yahoo email address.

You can also combine wildcards with other conditions using AND, OR, and IN.


SQL Wildcard Search Performance

While wildcard SQL queries are flexible, they may cause performance issues if not used carefully:

  • Patterns starting with % prevent index usage, causing full table scans
  • Indexes are more effective with patterns like 'term%' than '%term'

Performance Tips:

  • Avoid leading wildcards if possible
  • Use full-text search for large datasets
  • Normalize data before querying (e.g., lowercase)

These best practices ensure your wildcard queries don’t slow down your database.


Real-World Examples

Find all names starting with “Sam”

SELECT * FROM employees WHERE name LIKE 'Sam%';

Search for product descriptions containing “wireless”

SELECT * FROM products WHERE description LIKE '%wireless%';

Match filenames with a specific prefix and extension

SELECT * FROM files WHERE filename LIKE 'report_%.pdf';

Get users with emails that include numbers

SELECT * FROM users WHERE email LIKE '%[0-9]%';  -- Note: regex-like behavior may vary by DB engine

These examples demonstrate practical uses of wildcard SQL logic in business applications.


Alternatives to Wildcards

For more advanced text matching, consider alternatives like:

  • Regular expressions (REGEXP, SIMILAR TO)
  • Full-text search (e.g., PostgreSQL tsvector)
  • Soundex or fuzzy matching extensions

These approaches support broader string-matching requirements but can be more complex to set up.


Summary

The SQL wildcard is a key tool for flexible and pattern-based searches. Using characters like % and _, you can retrieve records that match partial text values, support user-driven search functionality, or audit inconsistent data.

Understanding how wildcards work, where to place them, how to escape them, and how they affect performance gives you control over your SQL queries. Use these tools wisely to build efficient, precise, and powerful search logic in your database workflows.

Mastering wildcard SQL queries is essential for anyone working with dynamic datasets, search functionality, or imperfect data inputs.

Learn SQL for Free
Start learning now
button icon
To advance beyond this tutorial and learn SQL by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH

Reach your coding goals faster