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

Like and Wildcards in SQL

Master SQL pattern matching with LIKE operators

SQL Pattern Matching Fundamentals

2
primary wildcard characters
3
common LIKE patterns
1
case-sensitive operator

LIKE vs EQUALS Comparison

FeatureEQUALSLIKE
Pattern MatchingExact match onlySupports wildcards
Case SensitivityCase sensitiveCase sensitive
PerformanceFasterSlower for patterns
FlexibilityLimitedHigh flexibility
Recommended: Use LIKE when you need pattern matching capabilities with wildcards

Wildcard Character Types

Percentage Sign (%)

Matches zero, one, or multiple characters. Use at beginning, end, or both sides of pattern for flexible matching.

Underscore (_)

Matches exactly one character. Useful when you know the exact position but not the specific character.

LIKE Pattern Implementation Steps

1

Connect to Database

Establish connection to your target database and select the appropriate table

2

Choose LIKE Operator

Replace EQUALS with LIKE in your WHERE clause to enable wildcard functionality

3

Add Wildcard Patterns

Insert % for multiple characters or _ for single character matching

4

Test and Refine

Execute query and adjust patterns based on results to match your requirements

Common LIKE Pattern Usage

Contains Pattern (%text%)
45
Starts With (text%)
30
Ends With (%text)
20
Single Character (_)
5
Case Sensitivity Best Practice

Use ILIKE in PostgreSQL instead of LIKE for case-insensitive pattern matching. ILIKE ignores uppercase and lowercase differences, making queries more flexible for user input.

LIKE vs ILIKE Operators

Pros
ILIKE provides case-insensitive matching
More user-friendly for search functionality
Reduces need for UPPER() or LOWER() functions
Better for handling mixed-case data
Cons
ILIKE is PostgreSQL-specific syntax
Slightly slower performance than LIKE
May not be available in all SQL databases
Can match unintended results if case matters

Wildcard Pattern Checklist

0/4

AND vs OR Logic in WHERE Clauses

FeatureAND LogicOR Logic
Condition RequirementsBoth must be trueEither can be true
Result Set SizeSmaller, more specificLarger, more inclusive
Use CaseNarrow down resultsExpand search criteria
Performance ImpactFaster filteringMore data to process
Recommended: Use AND to narrow results with multiple criteria, OR to broaden search scope
Be careful of that OR versus AND. We skip the SQL Server. We go to here. However, if you say AND, they both must be true.
Understanding the critical difference between AND and OR operators when combining multiple WHERE conditions
NOT Operator Best Practice

Always test the positive condition first before adding NOT. This helps identify typos and ensures you're excluding the correct data. If you start with NOT and make a typo, you won't see what you're actually excluding.

NOT Operator Variations

NOT LIKE

Excludes rows matching the pattern. Works with all wildcard combinations and is universally supported across SQL databases.

!= (Not Equal)

Programming-style inequality operator. More concise but only works with exact values, not with LIKE patterns or other operators.

NOT with Other Operators

NOT works universally with any operator including BETWEEN, IN, and comparison operators. Guaranteed compatibility.

Query Development Workflow

Step 1

Connect to Database

Establish connection and select target database

Step 2

Examine Data Structure

Review table contents and identify search columns

Step 3

Write Positive Query

Create initial query to find target data

Step 4

Add Pattern Matching

Replace EQUALS with LIKE and add wildcards

Step 5

Combine Conditions

Add AND/OR logic for multiple criteria

Step 6

Apply Negation

Add NOT operators after testing positive conditions

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.

Let's explore the powerful combination of LIKE operators and wildcards in SQL—tools that transform rigid exact-match queries into flexible pattern-matching powerhouses. When you first open your database management tool, establishing the right connection is crucial. You'll need to connect to your specific server and select the appropriate database—in this case, our company database—before diving into query construction.

Consider our users table as a practical example. When examining the data with a standard SELECT * FROM users query, you'll notice cities with varied naming patterns. Many contain "port" in different positions—some cities end with "port," others begin with it. This diversity presents the perfect scenario for demonstrating why exact matching falls short and pattern matching excels.

Traditional equality operators (=) demand precision. For instance, searching for city = 'Port Almeda' returns only users from that exact location. While this works perfectly for specific matches, it lacks the flexibility modern data analysis demands. The LIKE operator, however, opens up a world of pattern-matching possibilities through wildcards.

Initially, using LIKE without wildcards functions identically to the equals operator. The real power emerges when you introduce wildcard characters. The percentage sign (%) represents zero or more characters of any type, while the underscore (_) represents exactly one character. This distinction is crucial for precise pattern matching.

Pattern placement dramatically affects your results. A query like city LIKE 'Port%' finds anything beginning with "Port," regardless of what follows. The percentage wildcard tells the database: "match 'Port' followed by any number of characters—or none at all." Case sensitivity matters here; PostgreSQL's LIKE operator distinguishes between 'Port' and 'port'. For case-insensitive matching, PostgreSQL offers ILIKE, which proves invaluable in real-world applications where case consistency isn't guaranteed.

Reversing the wildcard placement with city LIKE '%port' captures anything ending in "port." This approach proves particularly useful when searching for patterns at word endings. Surrounding your search term with wildcards—city LIKE '%port%'—casts the widest net, finding "port" anywhere within the string.

Advanced pattern matching allows for sophisticated Boolean logic. Consider searching for cities that either begin with "Port" OR end with "port" using: city LIKE 'Port%' OR city LIKE '%port'. This excludes middle occurrences like "New Portsmouth" while capturing both prefix and suffix matches. Such precision becomes invaluable when dealing with complex datasets requiring nuanced filtering criteria.


Moving beyond theoretical examples, let's examine practical applications. When analyzing a products table for items containing "Paper," the wildcard placement determines your results. Without wildcards, LIKE 'Paper' finds only exact matches—products literally named "Paper." Adding surrounding wildcards with LIKE '%Paper%' discovers products with "Paper" anywhere in the title, such as "High-Quality Paper Towels" or "Recycled Paper Products."

Combining pattern matching with additional conditions creates powerful, targeted queries. Searching for paper products over $30 requires both pattern matching AND numerical comparison: title LIKE '%Paper%' AND price > 30. This demonstrates how modern SQL Server implementations excel at combining multiple criteria for precise data retrieval.

The distinction between AND and OR operators cannot be overstated. AND requires all conditions to be true simultaneously, while OR includes rows meeting any single condition. When searching for either paper products OR items over $30, the OR operator returns a broader result set that includes expensive non-paper items alongside any paper products regardless of price.

Cross-column pattern matching adds another layer of sophistication. You're not limited to searching within a single column—combine conditions across different fields for comprehensive filtering. For example, finding customers from Florida OR those with a specific ZIP code: state = 'Florida' OR zip_code = '37201'. This flexibility proves essential when dealing with real-world data where multiple pathways might lead to your desired subset.

Negation requires a strategic approach. The NOT operator excludes specified patterns, but testing your positive pattern first prevents costly mistakes. When filtering out products tagged as "Emeralds," first verify your pattern with tags = 'Emeralds' to confirm you're identifying the correct records. Then apply the negation: tags != 'Emeralds' or NOT tags = 'Emeralds'.

This preliminary positive search proves crucial because typos in negated queries can be invisible. If you mistype your exclusion pattern, you might exclude nothing while believing you've filtered your data. Always validate your pattern logic with a positive search before implementing negation.


SQL offers multiple negation syntaxes, each with specific use cases. While != works well with equality operators, NOT provides universal compatibility with any comparison operator—LIKE, BETWEEN, IN, or mathematical comparisons. For maximum flexibility and code readability, NOT ensures your queries work regardless of the underlying comparison logic.

Contemporary database management in 2026 emphasizes pattern matching more than ever, given the explosion of unstructured and semi-structured data sources. Understanding these fundamentals positions you to handle everything from customer name variations to product categorization challenges in modern enterprise environments.

Professional database developers increasingly rely on these techniques for data cleaning, customer segmentation, and business intelligence applications. The ability to construct flexible, efficient queries separates competent analysts from exceptional ones in today's data-driven landscape.

Now let's put this knowledge into practice. Open file 2.0 LIKE and Wildcards from your SQL Level One class materials. Work through the warmup exercises and challenge questions—these hands-on problems will solidify your understanding of pattern matching concepts and prepare you for real-world database scenarios. We'll reconvene shortly to review the challenges and discuss advanced techniques.

Key Takeaways

1LIKE operator enables pattern matching with wildcards, while EQUALS requires exact matches
2The percent sign (%) matches zero or more characters, underscore (_) matches exactly one character
3ILIKE provides case-insensitive matching in PostgreSQL, making searches more flexible than standard LIKE
4AND requires all conditions to be true, OR requires only one condition to be true
5Always test positive conditions before applying NOT to avoid excluding unintended data due to typos
6NOT operator works universally with any SQL operator, while symbols like != only work with specific operations
7Wildcard positioning matters: use % at the beginning for 'ends with', at the end for 'starts with', or both for 'contains'
8Multiple columns can be combined in WHERE clauses using different operators and logic combinations

RELATED ARTICLES