×

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 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 SOUNDEX() function

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

3 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 Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 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 Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.

MySQL SEC_TO_TIME() function

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

2 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 RTRIM() function

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

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 Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL RADIANS() function

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

2 minutes read.

MySQL RPAD() function

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

2 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL INSTR() Function

In this context, we will learn how we can use the MySQL INSTR() function with proper syntax and good examples. Introduction of MySQL INSTR() function This function in MySQL returns the location...

4 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 QUARTER() function

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

3 minutes read.