Lesson 25 of 60 – LIKE
42%

MySQL LIKE

The LIKE operator is used to search for a specified pattern in a column. It is commonly used when you do not know the exact value you want to search for.

Note: LIKE is mainly used with text data and works with wildcard characters such as % and _.

1. What is LIKE?

The LIKE operator searches for a pattern in a column.

SELECT *
FROM students
WHERE name LIKE 'A%';

This finds students whose names start with A.

2. Basic LIKE Syntax

The basic syntax is:

SELECT column_name
FROM table_name
WHERE column_name LIKE pattern;

The pattern tells MySQL what kind of text to search for.

3. The % Wildcard

The % wildcard represents zero or more characters.

SELECT *
FROM students
WHERE name LIKE 'A%';

This can match names such as Amit, Ankit, and Arun.

4. Names Starting With a Letter

To find names beginning with a particular letter, place % after the letter.

SELECT *
FROM students
WHERE name LIKE 'R%';

This finds names beginning with R.

5. Names Ending With a Letter

To find names ending with a particular letter, place % before the letter.

SELECT *
FROM students
WHERE name LIKE '%a';

This finds names ending with the letter a.

6. Text Containing a Word

To find text containing a particular word or character sequence, use % on both sides.

SELECT *
FROM students
WHERE name LIKE '%an%';

This finds names containing an.

7. The _ Wildcard

The underscore _ represents exactly one character.

SELECT *
FROM students
WHERE name LIKE 'A_it';

This pattern can match a four-character name such as Amit.

8. One Character with _

Each underscore represents one character.

SELECT *
FROM students
WHERE name LIKE '_mit';

The underscore represents the first character.

9. Multiple _ Wildcards

You can use multiple underscores to represent multiple individual characters.

SELECT *
FROM students
WHERE name LIKE '____';

This searches for values containing exactly four characters.

10. % vs _

Wildcard Meaning
% Zero or more characters
_ Exactly one character

11. LIKE with WHERE

LIKE is normally used together with WHERE.

SELECT name, course
FROM students
WHERE course LIKE 'P%';

This finds courses beginning with P.

12. LIKE with Multiple Conditions

LIKE can be combined with AND.

SELECT *
FROM students
WHERE name LIKE 'A%'
AND course = 'Python';

This finds Python students whose names start with A.

13. LIKE with OR

Multiple LIKE conditions can be combined with OR.

SELECT *
FROM students
WHERE name LIKE 'A%'
OR name LIKE 'R%';

This finds names beginning with A or R.

14. NOT LIKE

NOT LIKE finds values that do not match a specified pattern.

SELECT *
FROM students
WHERE name NOT LIKE 'A%';

This excludes names beginning with A.

15. LIKE with Course Names

LIKE is useful for searching course names.

SELECT *
FROM courses
WHERE course_name LIKE 'Web%';

This can find courses whose names begin with Web.

16. LIKE with Email

LIKE can be used to search email addresses.

SELECT *
FROM students
WHERE email LIKE '%gmail.com';

This searches for email addresses ending with gmail.com.

17. LIKE with Phone Numbers

LIKE can be used with phone-number strings when pattern searching is required.

SELECT *
FROM students
WHERE mobile LIKE '98%';

This searches for mobile values beginning with 98.

18. LIKE with ORDER BY

LIKE can be combined with ORDER BY.

SELECT name, course
FROM students
WHERE name LIKE 'A%'
ORDER BY name ASC;

The matching names are sorted alphabetically.

19. LIKE with LIMIT

LIMIT can be used to restrict the number of matching records.

SELECT *
FROM students
WHERE name LIKE 'A%'
LIMIT 5;

This returns up to five matching records.

20. LIKE with IN

LIKE and IN solve different problems, but they can be used together.

SELECT *
FROM students
WHERE course IN ('Python', 'Java')
AND name LIKE 'A%';

This finds Python or Java students whose names begin with A.

21. LIKE with BETWEEN

LIKE can be combined with BETWEEN when multiple filtering conditions are needed.

SELECT *
FROM students
WHERE name LIKE 'A%'
AND age BETWEEN 18 AND 25;

This finds students whose names begin with A and whose age is between 18 and 25.

22. LIKE with UPDATE

LIKE can be used to update records matching a pattern.

UPDATE students
SET course = 'Python'
WHERE name LIKE 'A%';
Warning: Always run a SELECT query first to check which records will be updated.

23. LIKE with DELETE

LIKE can also be used with DELETE.

DELETE FROM students
WHERE name LIKE 'Test%';

This deletes records whose names start with Test.

Warning: DELETE permanently removes matching records. Verify the condition with SELECT before executing it.

24. Searching for NULL Values

LIKE should not be used to check for NULL values. Use IS NULL instead.

SELECT *
FROM students
WHERE email IS NULL;

Use LIKE for pattern matching and IS NULL for NULL checking.

25. LIKE with a Subquery

LIKE can be used together with a subquery when the filtering logic requires another query.

SELECT *
FROM students
WHERE name LIKE CONCAT(
    (SELECT 'A'),
    '%'
);

This creates a pattern beginning with A.

26. Common LIKE Mistakes

  • Forgetting quotation marks around the pattern
  • Using = when a pattern search is required
  • Confusing % with _
  • Using LIKE to check NULL values
  • Forgetting the wildcard when partial matching is needed
  • Using UPDATE or DELETE without first checking the matching records

27. LIKE Search Workflow

A simple LIKE workflow is:

  1. Choose the column to search.
  2. Decide what pattern you need.
  3. Choose % or _ as required.
  4. Write the LIKE condition.
  5. Test it using SELECT.
  6. Add other conditions if necessary.
SELECT id, name, course
FROM students
WHERE name LIKE 'A%'
ORDER BY name;

28. % and _ Practical Difference

Consider the following examples:

-- Starts with A
name LIKE 'A%'

-- Ends with a
name LIKE '%a'

-- Contains an
name LIKE '%an%'

-- Exactly four characters
name LIKE '____'

-- Four-character value starting with A
name LIKE 'A___'

29. Practical Student Search

Suppose we want to find students whose names start with A and who are enrolled in Python or Java.

SELECT id, name, course, age, fee
FROM students
WHERE name LIKE 'A%'
AND course IN ('Python', 'Java')
ORDER BY name ASC;

This combines LIKE, IN, AND, and ORDER BY in one query.

30. Complete LIKE Example

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    course VARCHAR(100),
    age INT,
    email VARCHAR(150),
    fee DECIMAL(10,2)
);

INSERT INTO students
(name, course, age, email, fee)
VALUES
('Amit Kumar', 'Python', 22, 'amit@gmail.com', 15000.00),
('Anita Singh', 'Java', 21, 'anita@gmail.com', 18000.00),
('Rahul Kumar', 'PHP', 24, 'rahul@yahoo.com', 12000.00),
('Ravi Sharma', 'JavaScript', 26, 'ravi@gmail.com', 22000.00),
('Priya Das', 'Python', 20, 'priya@gmail.com', 15000.00);

SELECT id, name, course, email, fee
FROM students
WHERE name LIKE 'A%'
AND course IN ('Python', 'Java')
ORDER BY name ASC;

This query finds students whose names start with A and whose course is Python or Java.

📌 Key Points

  • LIKE is used for pattern matching.
  • % represents zero or more characters.
  • _ represents exactly one character.
  • NOT LIKE finds values that do not match a pattern.
  • LIKE is commonly used with the WHERE clause.
  • LIKE can be combined with AND, OR, IN, BETWEEN, ORDER BY, and LIMIT.
  • LIKE can be used with SELECT, UPDATE, and DELETE.
  • Use IS NULL instead of LIKE to check for NULL values.
  • Always test UPDATE and DELETE conditions with SELECT first.

🧠 Quick Quiz

Question: Which wildcard represents zero or more characters in MySQL LIKE?