How to Combine LIKE and IN in SQL Statement
Last Updated :
30 Dec, 2024
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 TableExample 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 BExample 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_id | product_name | category_name | price |
---|
1 | Apple iPhone | Electronics | 999.99 |
5 | Bluetooth Speaker | Electronics | 49.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.
Similar Reads
How to Combine LIKE and IN an SQLite
SQLite a lightweight and versatile relational database management system offers powerful querying capabilities through its support for various SQL operators. Two commonly used operators, 'LIKE' and 'IN', enable developers to write flexible and efficient queries for data retrieval. In this article, w
3 min read
How to Combine MySQL IN and LIKE Operators?
In database management, MySQL stands as one of the most popular relational database management systems. As developers strive to extract specific data from their databases, the judicious use of SQL operators becomes imperative. Among these operators, the IN and LIKE operators play crucial roles in fa
4 min read
How to Use Column Alias in SELECT Statement?
When working with SQL queries, readability and clarity are crucial for efficient data analysis. Using column aliases in the SELECT statement can significantly improve the clarity of our output by providing more meaningful and user-friendly names to the columns. Aliases are especially useful when wor
3 min read
Is there a combination of "LIKE" and "IN" in SQL Server?
In SQL Server, the LIKE operator is commonly used for pattern matching within string columns, while the IN operator is utilized to filter data based on a set of specified values. By combining these two operators, we can create more efficient queries. This combination allows for filtering results bas
3 min read
How to Specify Condition in Count() in SQL?
The COUNT() function in SQL is a powerful and widely used aggregate function for analyzing datasets. Whether weâre working with large-scale data in business applications or small datasets in personal projects, understanding how to count records with specific conditions can provide deeper insights an
4 min read
What is Nested Select Statement in PL/SQL?
PL/SQL is a Procedural Language/Structured Query Language and it enables users to write procedural logic directly within the database, including procedures, functions, triggers, and packages. In this article, we will understand Nested select statements in PL/SQL along with the examples and so on. Un
4 min read
How to do Case Sensitive and Case Insensitive Search in a Column in MySQL
LIKE Clause is used to perform case-insensitive searches in a column in MySQL and the COLLATE clause is used to perform case-sensitive searches in a column in MySQL. Learning both these techniques is important to understand search operations in MySQL. Case sensitivity can affect search queries in My
4 min read
How to Insert a Value that Contains an Apostrophe in SQL?
SQL is a standard database language used to access and manipulate data in databases. SQL stands for Structured Query Language. SQL was developed by IBM Computer Scientists in the 1970s. By executing queries SQL can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL
3 min read
How to Retrieve Data from Multiple Tables in SQL?
In SQL, retrieving data from multiple tables is a common requirement in database operations. Efficiently combining data from different tables allows developers to create complex queries and extract valuable insights from interconnected datasets. In this article, we will explore multiple approaches t
5 min read
SQL SELECT IN Statement
The IN operator in SQL is used to compare a column's value against a set of values. It returns TRUE if the column's value matches any of the values in the specified list, and FALSE if there is no match.In this article, we will learn how IN operator works and provide practical examples to help you be
3 min read