You are on page 1of 7

Advanced Database System

MySQL Clauses and


4
Operators UNIT
.
Topic
The Like operator
3
Time Allotment: 5 hours

Learning Objectives

At the end of the session, you will be able to use and execute:

a. LIKE operator to search for a specified pattern in a column.

Activate Prior Learning 4.3

Give your thoughts on the following terms.

Terms Your thoughts

Character

String
.

Presentation of Content

The SQL LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.

There are two wildcards often used in conjunction with the LIKE operator:

• The percent sign (%) represents zero, one, or multiple characters


• The underscore sign (_) represents one, single character
1
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database System

• The percent sign and the underscore can also be used in combinations!

LIKE Syntax

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

• Tip: You can also combine any number of conditions using AND or OR operators.

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

LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in
length

WHERE CustomerName LIKE 'a_ _%' Finds any values that start with "a" and are at least 3 characters in
length

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"

Demo Database

select * from tblreceiver;

2
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database System

SQL LIKE Examples

The following SQL statement selects all customers with a Name starting with "a":

Example

SELECT * FROM tblReceiver


WHERE Name LIKE 'a%';

The following SQL statement selects all receivers with a Name ending with "s":

Example

SELECT * FROM tblReceiver


WHERE Name LIKE '%s';

The following SQL statement selects all receivers with a Name that have "er" in any
position:

SELECT * FROM tblReceiver


WHERE Name LIKE '%er%';

3
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database System

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

SELECT * FROM tblreceiver


WHERE Name LIKE '_o%';

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

Example

SELECT * FROM tblReceiver


WHERE Name LIKE 'J__%';

The following SQL statement selects all receivers with a Name that starts with "b" and
ends with "s":

Example

4
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database System

SELECT * FROM tblReceiver


WHERE Name LIKE 'b%s';

The following SQL statement selects all receivers with a Name that does NOT start with
"J":

Example

SELECT * FROM tblReceiver


WHERE Name NOT LIKE 'j%';

The following SQL statement selects all receivers with a Name that start with "c" and at
least 3 characters:

Example

SELECT * FROM tblReceiver


WHERE Name LIKE 'c__%';

5
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database System

Summary Unit

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator: the percent
sign (%) represents zero, one, or multiple characters and the underscore sign (_)
represents one, single character.

6
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database System

References

W3school.com (n.d.). SQL Tutorial. Retrieved from https://tinyurl.com/kvbazps

7
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021

You might also like