Using LIKE and NOT LIKE operators in MySQL 8: A Practical Guide

Updated: January 25, 2024 By: Guest Contributor Post a comment

Introduction

The LIKE and NOT LIKE operators in MySQL are powerful tools for pattern matching in SQL queries. They are often used in the WHERE clause to search for a specified pattern in a column. This practical guide will demonstrate through a series of examples the use of these operators to filter data in MySQL 8, from basic to advanced level.

Understanding the LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. In MySQL, there are two wildcards used in conjunction with the LIKE operator:

  • % – Represents zero, one, or multiple characters
  • _ – Represents a single character

Basic Syntax for LIKE

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

Let’s start with a simple example. Assuming we have a table named users with a column for user names (username), we want to find all users whose names start with ‘A’:

SELECT username
FROM users
WHERE username LIKE 'A%';

This will retrieve all users with usernames beginning with ‘A’. The ‘%’ wildcard allows for any characters to follow.

LIKE with Multiple Conditions

You can also use the LIKE operator with multiple conditions using the AND or OR operators. Here’s an example of retrieving users whose names start with ‘A’ or ‘B’:

SELECT username
FROM users
WHERE username LIKE 'A%' OR username LIKE 'B%';

Using the NOT LIKE Operator

The NOT LIKE operator is used to exclude records that match a specified pattern. Here’s an example of how to use it:

SELECT username
FROM users
WHERE username NOT LIKE 'A%';

This query will return all users except those whose usernames start with the letter ‘A’.

Pattern Matching with Underscore

You can use the underscore ‘_’ to specify a pattern where you want a single character to vary. For example, to find users whose username is five characters long and ends with ‘y’:

SELECT username
FROM users
WHERE username LIKE '____y';

Escaping Special Characters

In cases where you want to search for a string that includes a ‘%’ or ‘_’, you need to escape it using the backslash ‘\’. Here’s how:

SELECT username
FROM users
WHERE username LIKE '25\%';

This will find usernames that contain the literal string ‘25%’.

Advanced Use of LIKE and NOT LIKE Operators

Combining Wildcards

It’s possible to combine multiple wildcards in a single pattern for more complex searches. Suppose you want to find all usernames that start with ‘A’ and are at least three characters long:

SELECT username
FROM users
WHERE username LIKE 'A_%_%';

This will match usernames such as ‘Amy’, ‘Alan’, or ‘Alex’, but not ‘Al’ since it only has two characters.

Using LIKE with CONCAT function

The CONCAT function can be used to construct patterns dynamically. For example, if we want to find users whose usernames start with the first letter of the email address:

SELECT username
FROM users
WHERE username LIKE CONCAT(SUBSTRING(email, 1, 1), '%');

This demonstrates how to use LIKE with other SQL functions to create powerful queries.

Best Practices for Performance

While the LIKE operator is very useful, it’s important to use it judiciously as it can lead to performance overhead, especially with leading wildcards. Here are some tips:

  • Try to avoid leading wildcards if possible, as they prevent the use of indexes.
  • Use full-text searching capabilities if you’re dealing with a large amount of text data.
  • When possible, use more restrictive patterns to reduce the dataset early in the processing.

Conclusion

Through various examples, we’ve seen how to utilize the LIKE and NOT LIKE operators in MySQL 8 for pattern matching in SQL queries. These operators enhance the flexibility and power of search conditions in your database. Always consider performance implications when using pattern-based filtering in your queries.