SQL Basics
- What is SQL?
- Data Definition Language
- Data Manipulation Language
- Data Query Language
- Data Control Language
- Transaction Control Language
- Tables and Schemas
- Data Types
- Constraints
Querying and Filtering
🧠 SQL LIKE and Wildcards: Flexible Pattern Matching Made Easy
When working with databases, there are times you want to search for a partial match instead of an exact value. That’s where SQL’s LIKE
operator and wildcard characters come in handy.
This article will walk you through how LIKE
works in SQL, how to use the wildcard characters %
and _
, and provide real-world examples for better understanding. Whether you’re querying names, emails, or anything else, these tools are essential for flexible searching.
📘 What is the LIKE
Operator in SQL?
The LIKE
operator in SQL is used in the WHERE
clause to search for a specific pattern in a column. It is not case-sensitive in most databases (like MySQL and PostgreSQL by default), but in some systems like Oracle, case-sensitivity matters unless configured otherwise.
SELECT column_nameFROM table_nameWHERE column_name LIKE 'pattern';
Instead of matching the whole string exactly, LIKE
allows you to use wildcard characters to define patterns.
🔣 Wildcards in SQL
1. %
(Percent Sign)
- Represents zero or more characters.
- Can be used anywhere in the pattern.
Example:
LIKE 'A%'
finds any value that starts with “A”.
2. _
(Underscore)
- Represents a single character.
- Used when you want to match a character at a specific position.
Example:
LIKE '_at'
matches “Cat”, “Hat”, “Bat”, but not “Flat”.
🎓 Example Table: employees
id | name | |
---|---|---|
1 | Alice Green | alice@gmail.com |
2 | Bob Carter | bob.carter@yahoo.com |
3 | Charlie Tan | ctan@outlook.com |
4 | Amina Khan | amina.khan@gmail.com |
5 | Alan Walker | alan123@company.org |
🔍 Examples of Using LIKE
✅ 1. Names Starting with “A”
SELECT nameFROM employeesWHERE name LIKE 'A%';
Result:
name |
---|
Alice Green |
Amina Khan |
Alan Walker |
✅ 2. Emails Ending with “gmail.com”
SELECT emailFROM employeesWHERE email LIKE '%gmail.com';
Result:
alice@gmail.com |
amina.khan@gmail.com |
✅ 3. Names Containing “ar”
SELECT nameFROM employeesWHERE name LIKE '%ar%';
Result:
name |
---|
Bob Carter |
✅ 4. Emails with Single Character Before ”@“
SELECT emailFROM employeesWHERE email LIKE '_%@%';
This checks for emails that start with exactly one character before the @
.
📐 Pattern Matching Examples
Pattern | Description | Example Match |
---|---|---|
'A%' | Starts with A | Alice, Alan |
'%com' | Ends with “com” | gmail.com |
'____' | Exactly 4 characters long | John, Mary |
'%@gmail.com' | Ends with “@gmail.com” | alice@gmail.com |
'%@%.com' | Has domain ending in .com | bob@yahoo.com |
'%@_%.%' | General email format check (basic) | alan123@company.org |
🧯 Common Mistakes to Avoid
❌ 1. Forgetting Wildcards
-- Wrong: matches only exact string "gmail.com"WHERE email LIKE 'gmail.com';
Fix:
WHERE email LIKE '%gmail.com';
❌ 2. Using =
Instead of LIKE
If you’re searching for a pattern or partial match, LIKE
is required.
-- Wrong:WHERE name = '%ar%';-- Right:WHERE name LIKE '%ar%';
❌ 3. Expecting Case Sensitivity (varies by DBMS)
- MySQL:
LIKE
is case-insensitive by default. - PostgreSQL: Use
ILIKE
for case-insensitive search. - SQL Server: Depends on collation settings.
🛠 Real-World Use Cases
🧾 1. Filter Emails from Specific Domain
SELECT name, emailFROM employeesWHERE email LIKE '%@gmail.com';
🔍 2. Find Customers with Short Names
SELECT nameFROM customersWHERE name LIKE '___'; -- Exactly 3 characters
🗂 3. Search Product Names with “Pro”
SELECT product_nameFROM productsWHERE product_name LIKE '%Pro%';
💬 Combining LIKE
with NOT
🛑 Exclude Emails from Gmail
SELECT emailFROM employeesWHERE email NOT LIKE '%@gmail.com';
This returns all email addresses not ending with “@gmail.com”.
🔄 Combine LIKE
with AND
/OR
SELECT nameFROM employeesWHERE name LIKE 'A%' OR name LIKE 'B%';
Returns names starting with A or B.
🧠 Summary Table
Operator | Description | Example |
---|---|---|
LIKE | Pattern-based matching | LIKE 'A%' |
% | Zero or more characters wildcard | LIKE '%mail' |
_ | Matches a single character | LIKE '_at' |
NOT LIKE | Excludes pattern-matching records | NOT LIKE '%gmail' |
✅ Final Thoughts
The LIKE
operator and wildcards (%
and _
) are essential tools for anyone working with SQL. They allow for flexible, powerful searches that go beyond simple exact matches. Whether you’re filtering names, emails, products, or any other text-based column, understanding how to use LIKE
effectively will save you time and make your queries much more powerful.
Take time to experiment with different combinations and patterns in your database — the more you use them, the more natural they’ll become!