MySQL LIKE

MySQL LIKE Operator

The LIKE operator is used for pattern matching in WHERE clauses, helping you search for text-based data using wildcards.


Wildcards in LIKE

Wildcard

Description

%

Matches zero or more characters.

_

Matches exactly one character.

Basic Syntax


SELECT column_names FROM table_name
WHERE column_name LIKE 'pattern';


Here are some examples showing different LIKE operators with '%' and '_' wildcards:

Pattern

Description

Matches Examples

'A%'

Starts with "A"

Alice, Andrew, Adam

'%n'

Ends with "n"

John, Ethan, Megan

'%an%'

Contains "an" anywhere

Daniel, Jonathan, Mandy

'____y'

Exactly 5 characters, ending in "y"

Emily, Jenny, Daisy

'%son'

Ends with "son"

Jackson, Anderson

'_o%'

"o" as the second character

Tom, Tony, Bob

CAST(name AS BINARY) LIKE 'David%'

Case-sensitive, starts with "David"

David (not david or DAVID)

'%\_admin%'

Search for "_admin" (escape _)

super_admin, user_admin

Demo Database

Below is a selection from the "Students" table in the school_db database:


student_id

name 

gender

city

age

score

course_id

email

phone_number

1

Alice

Female

Delhi

20

85

101

NULL

1234567890

2

Bob

Male

Mumbai

22

75

NULL

bob@email.com

NULL

3

Charlie

Male

Delhi

21

95

102

NULL

NULL

4

David

Male

Bangalore

23

65

101

david@email.com

NULL

5

Eve

Female

Mumbai

20

80

103

NULL

NULL

6

Frank

Male

Delhi

22

90

103

NULL

NULL

7

Alice

Female

Mumbai

19

60

102

NULL

NULL

SQL LIKE Examples

The following SQL statement selects all students with a name starting with "A":

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

Try it yourself

The following SQL statement selects all students with a name ending with "e":

Example
SELECT * FROM students
WHERE name LIKE '%e';

Try it yourself

The following SQL statement selects all students with a name that have "an" in any position:

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

Try it yourself

The following SQL statement selects all studenst with a name that have "o" in the second position:

Example
SELECT * FROM students
WHERE name LIKE '_o%';

Try it yourself

The following SQL statement selects all studenst with a name that starts with "a" and are at least 3 characters in length:

Example
SELECT * FROM students
WHERE name LIKE 'a__%';

Try it yourself

The following SQL statement selects all studenst with a name that starts with "a" and ends with "e":

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

Try it yourself

The following SQL statement selects all students with a name that does NOT start with "J":

Example
SELECT * FROM students
WHERE name NOT LIKE 'J%';

Try it yourself

The following SQL statement selects all students with a name that starts with case senstive "David":

Example
SELECT * FROM students
WHERE name LIKE BINARY 'David%';

Try it yourself

Note

By default, LIKE is case-insensitive in MySQL.


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.