When working with SQL queries, it’s often essential to employ multiple LIKE conditions within the WHERE clause to pinpoint specific data. This enables you to retrieve records that match various patterns or criteria, enhancing the precision of your results. Let’s explore how to effectively utilize multiple LIKE conditions to streamline your SQL queries and optimize data retrieval.

Table of contents

  1. SQL Query Like with multiple values
    1. Single Like
    2. Multiple Like 
    3. Example for Multiple Like
    4. Example for Multiple NOT LIKE 

SQL Query Like with multiple values

SQL where clause fetches records quickly when you give conditions correctly. The conditions should be indexed in table columns. And, many a time, you need to filter records using like conditions.

Sometimes you need multiple conditions of matching or not matching. This post covers how to use the Multiple Like in the Where clause of SQL.

Not only LIKE, but you can also use multiple NOT LIKE conditions in SQL. You will get records of matching/non-matching characters with the LIKE – this you can achieve by percentile (%) wildcard character.

Below use cases, to help you know how to use single and multiple-like conditions.

  1. How to use Single LIKE condition
  2. How to use multiple LIKE Conditions
  3. Real-life example with multiple LIKE conditions
  4. How to use multiple, NOT LIKE conditions
Advertisements

Single Like

Below is the simple SQL to give the Like condition in the Where clause. It is with single Like.

select column1, column2, column3
where
column1 like '05%'
from my_table;

Multiple Like 

The following code says, the general syntax of usage of multiple like conditions in the Where clause.

select column1, column2, column3
where
(column1 like '05%' 
or column1 like '06%' 
or column1 like '07%')
from my_table;

Example for Multiple Like

The following example is to get records from the TRAVEL-DET table (travel details table).

select country_cde, date, name, age
where
(country_cde like 'BR%' 
or country_cde like 'US%' 
or country_cde like 'CA%')
from travel_det;

In the above example, I did filter records matching with either Brazil (BRA), the United States of America (USA), and Canada(CAN).

Example for Multiple NOT LIKE 

select country_cde, date, name, age
where
(country_cde not like 'BR%'
or country_cde not like 'US%'
or country_cde not like 'CA%')
from travel_det;

Here, will get records not matching with Brazil (BRA), the United States of America (USA), and Canada(CAN).

References