Skip to main content
April 2, 2026Dan Rodney/3 min read

Pattern Matching in SQL

Master SQL Wildcards for Flexible Data Queries

SQL Pattern Matching Fundamentals

Underscore Wildcard

Represents exactly one character or number in a specific position. Each underscore is a literal fill-in-the-blank for a single character.

Percentage Wildcard

Represents zero, one, or multiple characters. Provides maximum flexibility when you don't know the exact length or content.

LIKE vs ILIKE

LIKE is case-sensitive while ILIKE is case-insensitive. Most queries benefit from the flexibility of case-insensitive matching.

Underscore vs Percentage Wildcard Comparison

FeatureUnderscore (_)Percentage (%)
Character CountExactly oneZero to unlimited
FlexibilityFixed positionVariable length
Use CaseKnown pattern lengthUnknown pattern length
Example Match'B_b' matches 'Bob''B%' matches 'Bob', 'Barbara'
Recommended: Use underscore for fixed-length patterns, percentage for variable-length patterns

Implementing Pattern Matching in SQL

1

Choose Your Wildcard

Determine if you need fixed-length matching (underscore) or variable-length matching (percentage) based on your data pattern requirements.

2

Use LIKE or ILIKE

Replace the equals operator with LIKE for case-sensitive matching or ILIKE for case-insensitive matching in your WHERE clause.

3

Construct Your Pattern

Combine known characters with wildcards to create flexible search patterns that match your specific data requirements.

PostgreSQL Case Sensitivity Best Practice

Most SQL queries benefit from case-insensitive matching using ILIKE instead of LIKE, as it provides more flexible results without the strict uppercase/lowercase requirements.

LIKE is a wildcard and says you can use these things which don't represent a literal underscore. They represent a fill-in-the-blank.
Understanding the distinction between literal characters and wildcard patterns is crucial for effective SQL pattern matching.

Case-Sensitive vs Case-Insensitive Matching

Pros
ILIKE provides broader search results
Eliminates need to handle multiple case variations
More user-friendly for general search functionality
Reduces query complexity for most use cases
Cons
LIKE offers precise control when case matters
Some data requires exact case matching
Case-sensitive searches can be more performant
Maintains data integrity for case-specific fields

Pattern Matching Implementation Checklist

0/5

This lesson is a preview from our SQL Course Online (includes software) and SQL Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.

Database filtering extends beyond exact matches to embrace pattern-based searches that unlock sophisticated query capabilities. When you need flexibility in your search criteria, SQL wildcards become indispensable tools for professional database management.

SQL provides two primary wildcard characters, each serving distinct purposes in pattern matching. The underscore (_) functions as a precise placeholder, representing exactly one character position. The percentage sign (%) operates as a flexible catch-all, matching zero or more characters of any length.

The underscore wildcard offers surgical precision in your queries. Each underscore represents exactly one character or digit that must exist in that position. When you specify three underscores (___), you're defining a pattern that requires exactly three characters in those positions, regardless of their values. This approach provides controlled flexibility while maintaining structural requirements in your data matching.

Consider scenarios where you know the exact length of a field but not its content—product codes, employee IDs, or standardized reference numbers often follow this pattern. The underscore wildcard excels in these structured data environments.

The percentage sign wildcard delivers maximum flexibility for open-ended pattern matching. Unlike the underscore's one-to-one character replacement, the percentage sign can represent zero characters, one character, or unlimited characters. This makes it invaluable for searches where you know partial information but cannot predict the full string length or content.


Real-world applications demonstrate the percentage sign's versatility effectively. Email filtering provides a perfect example: searching for patterns like "%@gmail.com" captures all Gmail addresses regardless of username length. Similarly, name searches using "B%" return everyone whose first name begins with "B"—from "Bob" to "Barbara" to "Benjamin"—without requiring advance knowledge of name lengths or complete spellings.

Implementation requires understanding the distinction between equality operators and pattern matching syntax. When incorporating wildcards into WHERE clauses, you must use the LIKE operator rather than the equals sign (=). The equals operator treats wildcards as literal characters, while LIKE activates their pattern-matching functionality.

For example, the query "WHERE name LIKE '___TY'" finds names where the first three characters can be anything, followed by "TY". This matches "Marty" (M-A-R-TY) and "Betty" (B-E-T-TY) but excludes "Dougherty" due to its five-character prefix before "TY".

PostgreSQL enhances wildcard functionality with case sensitivity options through LIKE and ILIKE operators. Standard LIKE performs case-sensitive matching, treating uppercase and lowercase letters as distinct characters. ILIKE (case-insensitive LIKE) ignores case differences, providing more flexible matching in most business scenarios.


Case sensitivity significantly impacts query results. A case-sensitive search for "LIKE 'B%'" matches only names beginning with uppercase "B" (like "Brian"), excluding those starting with lowercase "b" (like "bob"). Switching to "ILIKE 'B%'" captures both variations, typically providing more comprehensive results for business applications.

In professional database management, case-insensitive matching often proves more practical for user-facing applications, customer searches, and data analysis. Most business scenarios benefit from ILIKE's flexibility, reserving case-sensitive LIKE for specific requirements like password fields or exact code matching.

The percentage wildcard's versatility shines in prefix matching scenarios. Whether searching for customers by partial company names, filtering products by category prefixes, or analyzing log files by timestamp patterns, the percentage sign adapts to variable-length data structures. Even single-character names like "B" match the pattern "B%", demonstrating the wildcard's inclusive zero-or-more character logic.

Key Takeaways

1Underscore wildcards represent exactly one character, while percentage wildcards represent zero or more characters
2Use LIKE for case-sensitive pattern matching and ILIKE for case-insensitive matching in PostgreSQL
3Pattern matching with LIKE replaces exact equality matching when you need flexible search criteria
4Percentage wildcards are ideal for email patterns or names where character count is unknown
5Case-insensitive matching with ILIKE is generally preferred for user-friendly search functionality
6Wildcard patterns enable flexible data retrieval without requiring exact string matches
7Three underscores would match exactly three characters, making it useful for fixed-format data
8Pattern matching is essential for building robust search features in database applications

RELATED ARTICLES