SQL provides powerful tools for searching and filtering data, and one of the most versatile tools is the LIKE operator. The LIKE operator is used for pattern matching within text data, allowing you to find records that match a specific format or string pattern. Whether you're looking for names that begin with certain letters, email addresses that follow a specific domain, or phone numbers in a particular format, LIKE is a critical tool for database querying.
This chapter will explain how to use the LIKE
operator, from basic usage to advanced pattern matching techniques. You’ll learn how to use wildcards, combine LIKE
with other conditions, and optimize your queries for performance.
The LIKE
operator is used in a WHERE
clause to search for a specified pattern in a column. It is most commonly used with string data types (e.g., VARCHAR
, CHAR
, TEXT
).
SELECT column_name
FROM table_name
WHERE column_name LIKE pattern;
column_name
: The column to search for the pattern.table_name
: The table containing the data.pattern
: The pattern to search for, which can include wildcards.Wildcards are characters that represent one or more characters in a string. The two primary wildcards used with LIKE
are:
%
: Represents zero or more characters._
: Represents a single character.%
WildcardThe %
wildcard allows you to match any sequence of characters. For example, to find all customers whose name starts with “A”:
SELECT name
FROM customers
WHERE name LIKE 'A%';
Name |
---|
Alice Smith |
Aaron Brown |
Amanda White |
The %
after “A” means any sequence of characters can follow the letter “A.”
_
WildcardThe _
wildcard matches exactly one character. For example, to find all customers whose name is exactly five characters long and starts with “J”:
SELECT name
FROM customers
WHERE name LIKE 'J____';
Name |
---|
Jason |
Julie |
The four underscores (____
) represent four characters, so the query finds names that are exactly five characters long.
In most databases, the LIKE
operator is case-insensitive by default. This means that searching for LIKE 'A%'
will return records where the name starts with both “A” and “a.”
To find all names starting with “a” or “A”:
SELECT name
FROM customers
WHERE name LIKE 'a%';
Name |
---|
Alice Smith |
Aaron Brown |
Amanda White |
The query returns results regardless of whether the first letter is uppercase or lowercase.
You can combine LIKE
with other SQL clauses, such as AND
, OR
, and NOT
, to create more complex search queries.
To find customers whose name starts with “A” and who live in “New York”:
SELECT name
FROM customers
WHERE name LIKE 'A%' AND city = 'New York';
Name |
---|
Alice Smith |
This query filters results to include only customers whose name starts with “A” and who are from New York.
The NOT LIKE
operator is used to find records that do not match a specified pattern. For example, to find customers whose name does not start with “A”:
SELECT name
FROM customers
WHERE name NOT LIKE 'A%';
;
Name |
---|
Brian Jones |
Christine Lee |
This query returns all names that do not start with “A.”
Though LIKE
is typically used with text data, it can also be applied to numeric data stored as strings, such as phone numbers or zip codes.
Consider a customers
table with a phone_number
column. To find phone numbers that start with “555”:
SELECT name, phone_number
FROM customers
WHERE phone_number LIKE '555%';
Name | Phone Number |
---|---|
Alice Smith | 555-1234 |
Brian Jones | 555-5678 |
This query matches phone numbers that start with “555,” commonly used in fictional examples.
To find all customers whose last names end with “son”:
SELECT name
FROM customers
WHERE name LIKE '%son';
Name |
---|
Jason Anderson |
Mason Johnson |
The %
wildcard before “son” means any sequence of characters can come before “son.”
To find customers whose name contains the letters “an” anywhere in the name:
SELECT name
FROM customers
WHERE name LIKE '%an%';
Name |
---|
Amanda White |
Daniel Clark |
The %
on both sides of “an” ensures that “an” can appear anywhere in the name.
While the LIKE
operator is a powerful tool for pattern matching, it can become slow on large datasets, especially when using the %
wildcard at the beginning of the pattern. Here are a few tips to optimize LIKE
queries:
LIKE
is indexed to improve performance.%
at the beginning of the pattern, as it prevents the database from using an index efficiently.Sometimes, the search pattern may contain characters that are normally treated as wildcards (%
or _
). In such cases, you can use an escape character to treat them as literals.
Suppose you want to search for names that contain the literal “%” character:
SELECT name
FROM customers
WHERE name LIKE '%\%%' ESCAPE '\';
Name |
---|
50% Off Deals |
The backslash (\
) is used to escape the %
wildcard, so it is treated as a literal character.
You can combine LIKE
with JOIN
operations to search across multiple related tables.
Consider two tables: customers
and orders
.
Customer ID | Name |
---|---|
1 | Alice Smith |
2 | Brian Jones |
Order ID | Customer ID | Product Name |
---|---|---|
101 | 1 | Apple iPhone |
102 | 1 | Samsung TV |
103 | 2 | Sony Headphones |
To find all orders for products containing the word “Apple”:
SELECT customers.name, orders.product_name
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id
WHERE orders.product_name LIKE '%Apple%';
Name | Product Name |
---|---|
Alice Smith | Apple iPhone |
The query joins the customers
and orders
tables, then searches for products containing “Apple” in the name.
LIKE
, especially for large datasets, make sure to index the columns you’re searching. However, keep in mind that indexes may not be used when the pattern starts with a wildcard (%
).%pattern
, as they force a full table scan, slowing down the query.LIKE
with indexed columns, filters, or limiting results.The SQL LIKE operator is an essential tool for pattern matching and searching within text data. Whether you're filtering names, phone numbers, email addresses, or product descriptions, LIKE allows you to define flexible search patterns with wildcards like % and _. In this chapter, we've covered the basics of LIKE, explored advanced usage like combining with AND and NOT, handling special cases with escape characters, and optimizing LIKE queries for performance. Happy coding !❤️