When working with SQL queries, there are times when you need to search for data that matches a certain pattern, but without knowing the exact values. The LIKE
operator allows you to perform pattern matching in SQL, enabling you to search for records that contain specific sequences of characters.
On the flip side, the NOT LIKE
operator is used to exclude records that match a certain pattern. These operators are essential when you're dealing with textual data (like names, addresses, or descriptions) and want to find or exclude certain patterns.
The LIKE
operator is used in a WHERE
clause to search for a specified pattern in a column.
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;
column_name
: The name of the column where you want to search for the pattern.pattern
: The pattern you want to match. You can use wildcards to define the pattern.SQL uses two primary wildcards with the LIKE
operator:
%
): Represents zero or more characters._
): Represents a single character.
SELECT first_name, last_name
FROM employees
WHERE first_name LIKE 'A%';
Explanation:
A
. The %
wildcard allows any characters to follow A
.Result:
first_name | last_name |
---|---|
Alice | Johnson |
Andrew | Smith |
Adam | Brown |
%
) WildcardThe percent sign (%
) is used to represent zero, one, or more characters. This allows for broad pattern matching.
To find all employees whose last name ends with "son":
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE '%son';
Explanation:
%son
pattern matches any last name that ends with "son", such as "Johnson" or "Wilson".Result:
first_name | last_name |
---|---|
Alice | Johnson |
James | Wilson |
_
) WildcardThe underscore (_
) represents exactly one character. It’s used when you need to match a specific number of characters.
If you want to find all employees whose first name is exactly 5 characters long and starts with the letter A
:
SELECT first_name, last_name
FROM employees
WHERE first_name LIKE 'A____';
Explanation:
A____
pattern will match any first name that starts with A
and is followed by exactly four other characters.Result:
first_name | last_name |
---|---|
Alice | Johnson |
Angie | Thomas |
%
and _
WildcardsYou can combine the %
and _
wildcards to create more complex patterns.
To find employees whose first name starts with A
, followed by exactly two characters, and ends with e
:
SELECT first_name, last_name
FROM employees
WHERE first_name LIKE 'A__e';
Explanation:
A__e
pattern will match names like "Abe", "Ace", and "Aye" (but not "Alice").Result:
first_name | last_name |
---|---|
Abe | Williams |
Ace | Daniels |
The NOT LIKE
operator is used to exclude rows that match a specified pattern. It’s useful when you want to retrieve all records except those that match a given pattern.
Suppose you want to find employees whose first name does not start with the letter A
:
SELECT first_name, last_name
FROM employees
WHERE first_name NOT LIKE 'A%';
Explanation:
NOT LIKE 'A%'
pattern will exclude any employee whose first name starts with the letter A
.Result:
first_name | last_name |
---|---|
Bob | Smith |
Charles | Davis |
Emma | Roberts |
In some SQL databases, the LIKE
and NOT LIKE
operators are case-insensitive by default (for example, MySQL), while in others (such as PostgreSQL), they are case-sensitive. To make the pattern matching case-insensitive, you can use the LOWER()
or UPPER()
functions in combination with LIKE
or NOT LIKE
.
To find employees whose first name starts with a
or A
(case-insensitive):
SELECT first_name, last_name
FROM employees
WHERE LOWER(first_name) LIKE 'a%';
Explanation:
LOWER(first_name)
function ensures that the comparison is case-insensitive, so it will match names starting with both a
and A
.Result:
first_name | last_name |
---|---|
Alice | Johnson |
Andrew | Smith |
The LIKE
operator is not limited to text columns—it can also be used to match patterns in columns containing numbers or special characters. You can use the same wildcards (%
and _
) to match numeric or alphanumeric patterns.
If you have a products
table and want to find product codes that start with "A" and are followed by any characters:
SELECT product_code, product_name
FROM products
WHERE product_code LIKE 'A%';
Explanation:
A
, such as "A001", "A2345", etc.Result:
product_code | product_name |
---|---|
A001 | Widget |
A2345 | Gadget |
You can also use the NOT LIKE
operator to exclude patterns with numbers or special characters.
If you want to exclude product codes that start with A
:
SELECT product_code, product_name
FROM products
WHERE product_code NOT LIKE 'A%';
Explanation:
A
.Result:
product_code | product_name |
---|---|
B123 | Tool |
C5678 | Device |