×

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples.

Introduction of MySQL LIKE() function

The LIKE function is a logical function that verifies whether a string contains a mentioned pattern or not.

Syntax of the MySQL LIKE() function

The syntax of the MySQL LIKE() function is given as follows:

expression LIKE pattern ESCAPE escape_character

If the expression matches the pattern in this syntax, the LIKE function returns 1. Otherwise, it returns 0.

MySQL provides two wildcard characters for constructing patterns: percentage % and underscores _.

  • The percentage ( % ) wildcard matches any string of zero or more characters.
  • The underscore ( _ ) wildcard matches any single character.

For example, s% matches any string that starts with the character s, such as sun and six. The se_ matches any string starts with se and is followed by any character such as see and sea.

When the pattern contains the wildcard character, and you want to treat it as a regular character, you can use the ESCAPE clause.

Typically, you'll use the LIKE function in the WHERE clause of the SELECT, DELETE, and UPDATE statement.

Returns:

It will return whether a string contains a specified pattern or not.

Application used for LIKE() function:

The LIKE() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL LIKE() function:

Now we will look into some MySQL LIKE() function examples and will explore how we can use the LIKE function in MySQL.

We will use the following worker table from the sample database for the demonstration:

Worker Table

Worker
Workernumber
Firstname
Lastname
Email
Extension
Email
reportsTo
Officecode
jobname

A) Using MySQL LIKE function with the percentage (%) wildcard examples

This example uses the LIKE function to find workers whose first names start with the letter s:

SELECT
employeeNumber,
Lastname,
firstName
FROM
worker
WHERE
firstName LIKE 'a%';

OUTPUT:

WorkernumberFirstnameLastname
1656SasmitaSahoo
1687Sumansahoo

In this example, MySQL scans the whole worker table to find workers whose first names start with the letter ‘s’ and are followed by any number of characters.

The following query uses the LIKE function to find workers whose last names end with the literal string on, e.g., Patterson, Thompson:

SELECT
employeeNumber,
Lastname,
firstName
FROM
worker
WHERE
Lastname LIKE '%on';

Here, we used the percentage (%) wildcard at the beginning and the end of the substring to check if a string contains a substring or not.

For example, the following query uses the LIKE function to find all workers whose last names contain the substring on:

SELECT
employeeNumber,
Lastname,
firstName
FROM
worker
WHERE
lastname LIKE '%on%';

B) Using MySQL LIKE function with an underscore( _ ) wildcard examples

To find workers whose first names start with the letter T, end with the letter m, and contain any single character between, e.g., Tom, Tim, you use the underscore (_) wildcard to construct the pattern as follows:

SELECT
employeeNumber,
Lastname,
firstName
FROM
worker
WHERE
firstname LIKE 'T_m';

C) Using MySQL NOT LIKE function example

The MySQL allows you to combine the NOT function with the LIKE function to find a string that does not match a specific pattern.

Suppose you want to search for workers whose last names don't start with the letter B; you can use the NOT LIKE function as follows:

SELECT
employeeNumber,
Lastname,
firstName
FROM
worker
WHERE
lastName NOT LIKE 'B%';

It should be noted that the pattern is not case-sensitive. Therefore, the b% and B% patterns return the same result.

MySQL LIKE function with the ESCAPE clause

Sometimes the pattern may contain wildcard characters, e.g., 10%, _20, etc.

In this case, you can use the ESCAPE clause to specify the escape character so that the LIKE function interprets the wildcard character as a literal character.

If you don't specify the escape character explicitly, the backslash character (\) is the default escape character.

For example, if you want to find products whose product codes contain the string _20, you can use the pattern %\_20% with the default escape character:

SELECT
productCode,
productName
FROM
products
WHERE
productCode LIKE '%\_20%';

Alternatively, you can specify a different escape character, e.g., $ using the ESCAPE clause:

SELECT
productCode,
productName
FROM
products
WHERE
productCode LIKE '%$_20%' ESCAPE '$';

The pattern %$_20% matches any string that contains the _20 string.

Application of MySQL LIKE() function:

This function is used to test whether a string contains a specified pattern or not.

Summary

In the above context, we have learned how we can use the LIKE() function in MySQL

to Use the LIKE function to test if a value matches a pattern.

The % wildcard matches zero or more characters.

The _ wildcard matches a single character.

Use ESCAPE clause specifies an escape character other than the default escape character (\).

Use the NOT function to negate the LIKE function.


Related Topics

MySQL IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL PI() function

In this context, we will learn how we can use the MySQL PI() function with proper syntax and good examples. Introduction of MySQL PI() function PI() function in MySQL is used to...

2 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL DATE_ADD() function

In this context, we will learn how we can use the MySQL DATE_ADD() function with proper syntax and good examples. Introduction of MySQL DATE_ADD() function In MySQL, the DATE_ADD() function is used...

3 minutes read.

MySQL MOD() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function Basically, in MySQL, the MOD() function is...

3 minutes read.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL Character Length Function

In this context, we will learn how we can use the MySQL CHARACTER_LENGTH() function with proper syntax and good examples. Introduction of MySQL CHARACTER_LENGTH() function CHARACTER_LENGTH() function in MySQL is used to...

2 minutes read.

MySQL database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.