MYSQL Tutorial



MySQL LIKE


MySQL LIKE Operator – Pattern Matching Made Easy

The LIKE operator in MySQL is used to search for a specified pattern in a column. It is commonly used in the WHERE clause to filter rows based on partial matches.

🔹 Wildcards in LIKE

  • % – Matches zero or more characters
  • _ – Matches exactly one character

🔹 Examples Using LIKE

1. Find employees whose first name starts with 'J':

SELECT * FROM employees
WHERE first_name LIKE 'J%';
  

Explanation: Finds names like 'John', 'Jane', 'Jack', etc.

2. Find employees whose last name ends with 'son':

SELECT * FROM employees
WHERE last_name LIKE '%son';
  

Explanation: Matches 'Johnson', 'Wilson', 'Robson', etc.

3. Find employees whose email contains 'tech':

SELECT * FROM employees
WHERE email LIKE '%tech%';
  

Explanation: Finds emails like 'info@techcorp.com', 'user@mytechnology.net', etc.

4. Find employees whose first name has 'a' as the second letter:

SELECT * FROM employees
WHERE first_name LIKE '_a%';
  

Explanation: Matches names like 'Mark', 'David', 'Sally', etc. ('_' matches exactly one character before 'a')

🔹 Practice Example

Write a query to find employees whose department name contains 'Sale'.

SELECT * FROM employees
WHERE department LIKE '%Sale%';
  

🔹 Summary

  • LIKE 'abc%' matches values starting with 'abc'.
  • LIKE '%abc' matches values ending with 'abc'.
  • LIKE '%abc%' matches values containing 'abc' anywhere.
  • LIKE '_a%' matches values where the second character is 'a'.
  • Use _ for single-character wildcards and % for multiple characters.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review