Here is an example on how to use multiple LIKE conditions in WHERE clause of SQL.
SQL where clause fetches records quickly when you give conditions correctly. The conditions should be indexed 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 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.
How to Use Like in SQL
In four steps, I have shared my ideas, how to use multiple like conditions.
- How to use Single LIKE condition
- How to use multiple LIKE Conditions
- Real-life example with multiple LIKE conditions
- How to use multiple NOT LIKE conditions
How to use Single Like condition
Below is the simple SQL to give Like condition in Where clause. It is with single Like.
select column1, column2, column3 where column1 like '05%' from my_table;
Related: The List of top 60 SQL Blogs
How to give Multiple Like Conditions
Below example says, general syntax of usage of multiple like conditions in Where clause.
select column1, column2, column3 where (column1 like '05%' or column1 like '06%' or column1 like '07%') from my_table;
Real-life example with multiple Like conditions
Below example is to get records from 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 of matching with either Brazil (BRA), the United States of America (USA), and Canada(CAN).
How to use multiple NOT LIKE conditions
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;
In the above example, you will get records of not matching with Brazil (BRA), the United States of America (USA), and Canada(CAN).