In SQL, the LIKE operator is used to search for a specified pattern in a column. It allows you to perform pattern matching using wildcard characters. However, there may be situations where you need to use multiple LIKE conditions to refine your search. In this article, we will explore how to use multiple LIKE conditions in SQL with examples.
Basic Syntax of the LIKE Operator
Before diving into multiple LIKE conditions, let’s first understand the basic syntax of the LIKE operator:
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
The column_name
represents the name of the column you want to search in, the table_name
is the name of the table, and the pattern
is the string you want to search for. The LIKE
operator supports wildcard characters to match different patterns.
Using Multiple LIKE Conditions
To use multiple LIKE conditions, you can combine them using logical operators such as AND
or OR
. Here’s an example:
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern1 AND column_name LIKE pattern2;
In the above example, we are searching for rows where the column_name
matches both pattern1
and pattern2
.
Example 1:
Let’s say we have a table called employees
with columns first_name
and last_name
. We want to find all employees whose first name starts with “J” and last name ends with “son”. We can use multiple LIKE conditions to achieve this:
SELECT first_name, last_name FROM employees WHERE first_name LIKE 'J%' AND last_name LIKE '%son';
This query will return all employees whose first name starts with “J” and last name ends with “son”.
Example 2:
Let’s consider another example where we have a table called products
with a column called product_name
. We want to find all products that contain either “apple” or “orange” in their names. We can use multiple LIKE conditions with the OR
operator:
SELECT product_name FROM products WHERE product_name LIKE '%apple%' OR product_name LIKE '%orange%';
This query will return all products whose names contain either “apple” or “orange”.
Using NOT LIKE with Multiple Conditions
In addition to using multiple LIKE conditions, you can also use the NOT LIKE
operator to exclude certain patterns from your search. Here’s an example:
SELECT column_name(s) FROM table_name WHERE column_name NOT LIKE pattern1 AND column_name NOT LIKE pattern2;
In the above example, we are searching for rows where the column_name
does not match both pattern1
and pattern2
.
Example 3:
Let’s say we have a table called customers
with a column called email
. We want to find all customers whose email does not contain “gmail” or “yahoo” in the domain part. We can use multiple NOT LIKE
conditions:
SELECT email FROM customers WHERE email NOT LIKE '%gmail%' AND email NOT LIKE '%yahoo%';
This query will return all customers whose email does not contain “gmail” or “yahoo” in the domain part.
Conclusion
Using multiple LIKE conditions in SQL allows you to refine your search and find specific patterns in your data. By combining logical operators such as AND
or OR
, you can create complex search queries. Additionally, the NOT LIKE
operator can be used to exclude certain patterns from your search. Understanding how to use multiple LIKE conditions will enhance your SQL querying capabilities and help you retrieve the desired results.