LIKE: Mastering Pattern Matching with LIKE Operator in SQL

Learn SQL with Udemy

For an instructor lead, in-depth look at learning SQL click below.


In SQL (Structured Query Language), the LIKE operator is a very powerful tool that enables us to match string pattern in the data being queried. This operator, incorporated in the WHERE clause, helps in the retrieval of matching patterns in column data from a table.

Understanding the LIKE Operator

Let’s understand how the LIKE operator functions. This operator is used in the WHERE clause to search for a specific pattern in a column. Here’s a simple structure of the SQL query using LIKE:

You can use two wildcards alongside the LIKE operator:

  • The percent sign (%): Represents zero, one or multiple characters.
  • The underscore sign (_): Represents a single character.

Examples

Using Percentage (%)

For instance, let’s say we want to find all customers whose names start with “Ma” in the “Customers” table. This can be achieved using a percentage (%) wildcard with the LIKE operator.

This query will return all customers whose names start with “Ma”.

Using Underscore (_)

In contrast, let’s say we want to match any customers whose name starts with any character, followed by “ohn”, and ending with any number of characters. We can use the underscore (_) and the percentage (%) wildcard in this scenario as follows:

This query will return all names that have “ohn” starting from the second character, regardless of what the first character or the characters after “ohn” are.

Conclusion

Mastering the use of the SQL LIKE operator greatly enhances your ability to retrieve data matching complex patterns. It’s your powerful weapon in querying databases. Remember practice is the key, Happy querying!

Leave a Comment