How to use LIKE clause in SQL?

Learn to Code Today!

Learn 10x faster: coding, no-code and data skills. Join millions of users mastering new tech skills and accelerating their career with Enki.
Get started

The LIKE clause in SQL is incredibly useful for pattern matching within string data. It allows you to find records that match specified patterns, which can be particularly helpful for filtering and querying data effectively.

Discover more useful SQL tips and advanced techniques at enki.com to boost your data manipulation skills.

Overview of the SQL LIKE Clause

The SQL LIKE clause is a highly versatile tool used in combination with the WHERE statement to search for specific patterns within columns. By leveraging the LIKE clause, you can filter your data in a more refined manner, making it easier to find exactly what you're looking for.

The basic syntax for the SQL LIKE clause is straightforward:

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

Imagine you have a customer database and you need to find all customers whose names start with 'John'. By using the LIKE clause, you can efficiently filter out those records:

Using the % Wildcard in SQL LIKE

The % wildcard in SQL is a powerful way to match zero or more characters in a string. This flexibility makes it an excellent tool for locating patterns in your data.

To find all customers whose names start with 'A', you would use the following query:

SELECT * FROM Customers WHERE CustomerName LIKE 'A%';

The above query uses the % wildcard to match any sequence of characters following the initial 'A'. As a result, it returns all customer records where the CustomerName begins with 'A'. This illustrates how % can simplify searching for patterns within your data.

Using the _ Wildcard in SQL LIKE

The _ wildcard in SQL is a tool for matching exactly one character in a string. This enables precise and controlled pattern matching within your data.

To use the _ wildcard with the LIKE clause, your query would look like this:

SELECT * FROM Customers WHERE CustomerName LIKE 'a_n';

Let's explore a practical example using our Customers table. Assume we want to find customer names where there is a single character between 'Ernst' and 'Handle'. Here's how you would write that query:

In this query, the _ wildcard matches exactly one character that can be any value, whether a letter, number, or symbol. Therefore, Ernst_ Handle will match Ernst Handel, but not ErnstHandle or Ernstandle. This demonstrates the precision afforded by the _ wildcard in pattern matching.

The _ wildcard is particularly useful when you need to enforce stricter criteria in your SQL queries.

Examples of Combining Wildcards

Combining the % and _ wildcards can help you craft intricate patterns in your SQL queries, providing highly customized search capabilities.

Consider the following practical example using our Customers table. Suppose you want to find customer names that contain 'n', followed by any character, and then 'r':

In this query, %n_r% translates to any sequence of characters followed by 'n', a single character (matched by _), and an 'r', followed again by any sequence of characters. This pattern could match a name like 'Central Park' or 'Commercial Centre'.

Searching Email Domains

You might need to find all users with a specific email domain. Use the % wildcard to match the domain:

Finding Customers by Area Code

Need to filter phone numbers by area code? The LIKE clause makes this straightforward:

These examples demonstrate the practical power of the LIKE clause. When searching email domains, %@gmail.com ensures you match any username with a '@gmail.com' suffix. Similarly, (123)% helps you filter phone numbers starting with (123), a commonly required task for customer records.

Optimizing LIKE Queries

Using LIKE with indexed columns can greatly speed up your search, but there are caveats:

Avoid Starting Patterns with %: Patterns that start with % can be very slow, especially with large datasets, because they prevent the use of indexes.

-- Avoid, if possible
SELECT * FROM Customers WHERE CustomerName LIKE '%john';

Conclusion

The LIKE clause stands out as a powerful tool in SQL for pattern matching within text data, making your queries more flexible and efficient.

For more advanced SQL techniques and to further boost your proficiency, explore enki.com.

About Enki

  • Fully personalized online up-skilling
  • Unlimited AI coaching
  • Designed by Silicon Valley experts

More articles

Meet your AI-enabled coach

Professional athletes have a coach for every aspect of their performance. Why can’t you for your work? Enki’s AI-powered coaching on-demand - combined with state of the art, structured learning content - makes this a reality.
1
1:1 AI Coaching
How do I remove duplicate emails?
Convert the list to a set and back to a list. Sets automatically remove duplicates.
2
Personalized Exercises
3
Interactive practice

Unlock full access to all skills on Enki with a 7-day free trial

Get started