Like and Wildcards in SQL
Master SQL pattern matching with LIKE operators
SQL Pattern Matching Fundamentals
LIKE vs EQUALS Comparison
| Feature | EQUALS | LIKE |
|---|---|---|
| Pattern Matching | Exact match only | Supports wildcards |
| Case Sensitivity | Case sensitive | Case sensitive |
| Performance | Faster | Slower for patterns |
| Flexibility | Limited | High flexibility |
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
Connect to Database
Establish connection to your target database and select the appropriate table
Choose LIKE Operator
Replace EQUALS with LIKE in your WHERE clause to enable wildcard functionality
Add Wildcard Patterns
Insert % for multiple characters or _ for single character matching
Test and Refine
Execute query and adjust patterns based on results to match your requirements
Common LIKE Pattern Usage
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
Wildcard Pattern Checklist
Verify your target data exists before using wildcards
Choose LIKE or ILIKE based on your matching requirements
Place % at beginning, end, or both sides based on search needs
Use AND/OR operators to refine your result set
AND vs OR Logic in WHERE Clauses
| Feature | AND Logic | OR Logic |
|---|---|---|
| Condition Requirements | Both must be true | Either can be true |
| Result Set Size | Smaller, more specific | Larger, more inclusive |
| Use Case | Narrow down results | Expand search criteria |
| Performance Impact | Faster filtering | More data to process |
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.
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
Connect to Database
Establish connection and select target database
Examine Data Structure
Review table contents and identify search columns
Write Positive Query
Create initial query to find target data
Add Pattern Matching
Replace EQUALS with LIKE and add wildcards
Combine Conditions
Add AND/OR logic for multiple criteria
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.
Key Takeaways