MySQL LIKE and Wildcards

LIKE searches for a pattern in text. Two wildcards make the pattern.

WildcardMeaning
%Any number of characters (even none)
_Exactly one character
PatternMatches
'A%'Starts with A
'%a'Ends with a
'%an%'Contains “an” anywhere
'_o%'Second letter is o
SELECT name FROM students WHERE name LIKE 'A%';
Result
name
Asha

SELECT name FROM students WHERE name LIKE '%a';
Result
name
Maria
Asha
Neha

SELECT name FROM students WHERE name LIKE '_o%';
Result
name
Robin
John

SELECT name FROM students WHERE name LIKE '%i%';
Result
name
Robin
Maria
Vikram

Read the interview question names that begin with S.


NOT LIKE

SELECT name FROM students WHERE name NOT LIKE '%a%';
Result
name
Robin
John

Note: LIKE is not case-sensitive with the default settings. A search like %word with a starting % cannot use an index, so it can be slow on big tables. For big text search use a FULLTEXT index.

Summary


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.