What is the SQL LIKE
Operator?
The LIKE
operator in SQL is used to search for a specified pattern in a column. It is commonly used in WHERE
clauses to filter rows based on matching patterns in text data.
Syntax of SQL LIKE
Key Features of the LIKE
Operator
- Pattern Matching: Allows flexible filtering based on partial matches.
- Case Sensitivity: Behavior depends on the database. For example:
- MySQL: Case-insensitive by default.
- PostgreSQL: Case-sensitive by default.
- Works with Wildcards: Supports
%
and_
as wildcards for flexible matching.
Wildcards in SQL LIKE
%
: Represents zero, one, or multiple characters.- Example:
'%abc'
matches any text ending with "abc".
- Example:
_
: Represents exactly one character.- Example:
'a_c'
matches "abc", "acc", but not "abcc".
- Example:
Examples of SQL LIKE
1. Basic Pattern Matching
Retrieve customers whose names start with "Jo".
2. Matching Patterns Anywhere
Retrieve products containing "phone" in their names.
3. Matching a Specific Character
Retrieve employees whose names have "a" as the second character.
4. Matching Text Ending with a Pattern
Retrieve files with the ".txt" extension.
Using NOT LIKE
The NOT LIKE
operator filters rows that do not match the specified pattern.
Example
Retrieve employees whose names do not start with "A".
Case Sensitivity in LIKE
1. MySQL
By default, MySQL is case-insensitive for LIKE
.
To enforce case sensitivity, use the BINARY
keyword:
2. PostgreSQL
PostgreSQL is case-sensitive for LIKE
. Use ILIKE
for case-insensitive searches:
Advanced Examples
1. Search for Patterns Containing Special Characters
To match %
or _
as literal characters, escape them using the ESCAPE
clause.
Retrieve rows where the name includes "100%":
2. Combining LIKE
with OR
Retrieve customers whose names start with "A" or "B".
3. Using LIKE
with Numbers (Implicit Conversion)
Retrieve orders where the ID starts with "10".
Performance Considerations
Indexes:
- Leading wildcards (e.g.,
'%text'
) disable index usage, slowing performance. - Avoid starting patterns with
%
unless necessary.
- Leading wildcards (e.g.,
Use Full-Text Search: For complex text searches in large datasets, consider using database-specific full-text search features.
Optimize Queries: Use
LIKE
selective patterns to minimize scanned rows.
Common Use Cases for LIKE
1. Searching for Substrings
Retrieve emails containing "gmail".
2. Filtering by File Extensions
Retrieve all .jpg
files.
3. Filtering by Date Formats
Retrieve records for dates starting with "2025".
Best Practices for Using LIKE
- Use Wildcards Judiciously: Avoid leading wildcards (
'%text'
) to maintain query performance. - Escape Special Characters: Handle
%
and_
carefully when used as literals. - Consider Alternatives: For precise searches, use equality (
=
) or range queries instead ofLIKE
.
Conclusion
The SQL LIKE
operator is a powerful tool for filtering data based on patterns. Its flexibility with wildcards allows for partial matches, making it ideal for text searches, filtering based on substrings, or pattern-based queries. However, for optimal performance, use LIKE
thoughtfully and consider alternatives for large datasets or complex search requirements.