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
Write a query to get data of selected columns from a table
Write a query to select the student with the maximum marks
Write a query to select top 3 students by marks
Write a query to select the rows if a column has no value
Write a query to find the names of students that begin with ‘A’
Write a query to fetch the top 2 records
Interview Questions & Answers
How to select Top 10 records in MySQL?
Write a MySQL query to select students with less than 50% marks
Write a MySQL query to select students with marks between 70 and 80
Write a query to fetch top N records
Write a query to find the names of employees that begin with ‘S’
Write a query to select if column has no/empty value
What is NULL in MySQL? Is NULL the same as 0 or an empty string?