🔍 MySQL Wildcards – Flexible Searching with LIKE
When working with databases, we often need to search for patterns rather than exact values. This is where wildcards in MySQL come into play. Wildcards are used in conjunction with the LIKE
operator to perform pattern matching in SELECT
queries.
In this guide, we’ll explore how to use MySQL wildcards with practical examples using employees
and departments
tables.
🧠 What Are MySQL Wildcards?
Wildcards are special characters used with the LIKE
operator to search for patterns in text columns. The most commonly used wildcards are:
-
%
: Represents zero, one, or multiple characters. -
_
: Represents a single character.
These are very useful when you’re searching for names, emails, department titles, etc., that follow a certain pattern.
🔤 Basic Syntax
Common Patterns
Pattern | Description |
---|---|
'A%' |
Starts with ‘A’ |
'%n' |
Ends with ‘n’ |
'%an%' |
Contains ‘an’ anywhere |
'A_n' |
Starts with ‘A’, any 1 char, then ‘n’ |
'A__%' |
Starts with ‘A’, followed by 2 chars |
📋 Wildcards Examples Using Employees and Departments Tables
1️⃣ Find Employees Whose Names Start with “A”
🟢 Result: Returns all employees whose names start with the letter A.
2️⃣ Find Employees Whose Names End with “n”
🟢 Result: Returns employees with names ending in n, such as “John” or “Arman”.
3️⃣ Find Employees Whose Names Contain “li”
🟢 Result: Returns names like “Alice”, “Malik”, or “Julian”.
4️⃣ Find Employees with a 3-Letter Name Starting with “J”
🟢 Result: Returns names like “Jim”, “Jon”, or “Jay”.
5️⃣ Find Departments Containing the Word “Sales”
🟢 Result: Returns “Sales”, “International Sales”, “Sales & Marketing”, etc.
6️⃣ Find Employees in Departments That Start with “Tech”
🟢 Result: Returns employees from departments like “Technical”, “Tech Support”, or “Technology”.
7️⃣ Exclude a Pattern – Names Not Starting with “A”
🟢 Result: Returns all employee names that do not start with the letter A.
✅ Real-World Use Cases
🔹 Use Case 1: Filter Employees by Email Domain
🟢 Result: Useful when emailing all Gmail users or cleaning up specific domains.
🔹 Use Case 2: Find Departments That Start with a Capital Letter and Have Exactly 6 Characters
🟢 Result: Returns department names with exactly 6 characters, like “Design”, “HRTeam”.
🧠 Summary of MySQL Wildcards
Wildcard | Use |
---|---|
% |
Any number of characters (including none) |
_ |
Exactly one character |
They are used with LIKE
or NOT LIKE
to filter text-based values using patterns.
💡 Tip from Acesoftech Academy:
Use wildcards wisely — %
is powerful but can slow down large queries if not optimized. Always try to limit results with additional conditions (WHERE
, LIMIT
, etc.).
🏁 Final Thoughts
Wildcards are incredibly useful for flexible pattern searching in MySQL. Whether you’re looking for names, emails, or departments, using %
and _
allows you to write smarter, more dynamic queries.
Keep practicing with your own employees
and departments
tables — the more patterns you try, the more confident you’ll get!