MYSQL

9. MySQL Wildcards – Flexible Searching with LIKE

🔍 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

SELECT column_name
FROM table_name
WHERE column_name LIKE 'pattern';

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”

SELECT employee_id, name
FROM employees
WHERE name LIKE 'A%';

🟢 Result: Returns all employees whose names start with the letter A.


2️⃣ Find Employees Whose Names End with “n”

SELECT employee_id, name
FROM employees
WHERE name LIKE '%n';

🟢 Result: Returns employees with names ending in n, such as “John” or “Arman”.


3️⃣ Find Employees Whose Names Contain “li”

SELECT employee_id, name
FROM employees
WHERE name LIKE '%li%';

🟢 Result: Returns names like “Alice”, “Malik”, or “Julian”.


4️⃣ Find Employees with a 3-Letter Name Starting with “J”

SELECT employee_id, name
FROM employees
WHERE name LIKE 'J__';

🟢 Result: Returns names like “Jim”, “Jon”, or “Jay”.


5️⃣ Find Departments Containing the Word “Sales”

SELECT department_id, department_name
FROM departments
WHERE department_name LIKE '%Sales%';

🟢 Result: Returns “Sales”, “International Sales”, “Sales & Marketing”, etc.


6️⃣ Find Employees in Departments That Start with “Tech”

SELECT e.employee_id, e.name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id
WHERE d.department_name LIKE 'Tech%';

🟢 Result: Returns employees from departments like “Technical”, “Tech Support”, or “Technology”.


7️⃣ Exclude a Pattern – Names Not Starting with “A”

SELECT employee_id, name
FROM employees
WHERE name NOT LIKE '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

SELECT employee_id, name, email
FROM employees
WHERE email LIKE '%@gmail.com';

🟢 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

SELECT department_name
FROM departments
WHERE department_name LIKE '______';

🟢 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!

Leave a Reply

Your email address will not be published. Required fields are marked *