Open In App

How to Combine LIKE and IN in SQL Statement

Last Updated : 30 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The LIKE and IN operators in SQL are essential for building efficient, complex queries that filter data with precision. Combining these two operators in a single query enables users to target specific patterns in data while also filtering based on predefined values

In this article, we will explain how the LIKE and IN operators work together and help users navigate complex data collection scenarios. Learn how to precisely filter data, creating focused queries that boost our SQL skills.

SQL Combining 'LIKE' and 'IN' Operator

The 'LIKE' operator searches for a specified pattern in a column within a string, and the 'IN' operator allows users to filter data based on a set of specified values. By combining LIKE and IN, users can filter results based on patterns and specific values simultaneously, allowing users to create more complex and precise search conditions in SQL statements.

Syntax:

SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern
AND column_name IN (value1, value2, ...);

Examples of Combining 'LIKE' and 'IN' Operators

To illustrate how to combine LIKE and IN operators, let's first create a table called products with product details, showcasing how these operators can be effectively used together to filter data based on both patterns and predefined values. This combination allows for more precise and flexible searches in SQL queries.

Query:

CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
category_name VARCHAR(50),
price DECIMAL(10, 2)
);

INSERT INTO products (product_id, product_name, category_name, price)
VALUES
(1, 'Apple iPhone', 'Electronics', 999.99),
(2, 'Book: SQL Mastery', 'Books', 29.99),
(3, 'Blue T-Shirt', 'Clothing', 19.99),
(4, 'Banana', 'Groceries', 0.99),
(5, 'Bluetooth Speaker', 'Electronics', 49.99),
(6, 'Black Jeans', 'Clothing', 39.99);

Select * FROM products;

Output:

Products-Table
Products Table

Example 1: Filtering Products Starting with 'A' or 'B' in Specific Categories

This query retrieves products whose names start with 'A' or 'B' and belong to the 'Electronics' or 'Clothing' categories. It uses the LIKE operator for pattern matching and the IN operator to filter by specific categories.

Query:

SELECT *
FROM products
WHERE product_name LIKE 'A%' OR product_name LIKE 'B%'
AND category_name IN ('Electronics', 'Clothing');

Output:

Filtering-Products-starting-with-A-or-B
Filtering Products starting with A or B

Example 2: Filtering Products Including 'Phone' or 'Speaker' in Electronics

This query retrieves products that have "Phone" or "Speaker" in their names and belong to either the 'Electronics' or 'Accessories' categories. It combines the LIKE operator for pattern matching with the IN operator for filtering by categories.

Query:

SELECT *
FROM products
WHERE (product_name LIKE '%Phone%' OR product_name LIKE '%Speaker%' )
AND category_name IN ('Electronics', 'Accessories');

Output:

product_idproduct_namecategory_nameprice
1Apple iPhoneElectronics999.99
5Bluetooth SpeakerElectronics49.99

Conclusion

By combining the LIKE and IN operators in SQL, users can create highly tailored queries that filter data based on specific patterns and predefined values. This approach enhances the power and flexibility of SQL, making it easier to navigate complex datasets and retrieve precise information based on multiple criteria. Mastering this combination can lead to more efficient and focused queries, significantly improving our SQL querying capabilities.


Next Article
Article Tags :

Similar Reads