Is there a combination of "LIKE" and "IN" in SQL Server?
Last Updated :
30 Jul, 2024
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 based on both patterns and specific values, making it a powerful tool for data retrieval in SQL.
In this article, We will learn about the How to Combine SQL LIKE
and IN
Operator with the help of various examples and so on.
SQL Combine LIKE
and IN
Operator
- In SQL Server, the
LIKE
operator is used to search for a specified pattern within a string column, while the IN
operator allows us to filter data based on a set of specified values.
- By combining
LIKE
and IN
, we can create more nuanced queries that filter results based on both patterns and specific values.
Syntax to Combine SQL LIKE
and IN
Operators
The basic syntax for combining LIKE
and IN
in SQL Server is:
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern
AND column_name IN (value1, value2, ...);
Examples
We will create on table employees on which we will perform various examples based on the SQL Combine LIKE
and IN
Operator:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees (employee_id, employee_name, department, salary)
VALUES
(1, 'Alice Johnson', 'Marketing', 75000.00),
(2, 'Bob Smith', 'Sales', 68000.00),
(3, 'Charlie Brown', 'IT', 95000.00),
(4, 'Diana Prince', 'Marketing', 72000.00),
(5, 'Evan Roberts', 'HR', 60000.00),
(6, 'Fiona Clark', 'IT', 88000.00);
The employees
table will look like this:
employee_id | employee_name | department | salary |
---|
1 | Alice Johnson | Marketing | 75000.00 |
2 | Bob Smith | Sales | 68000.00 |
3 | Charlie Brown | IT | 95000.00 |
4 | Diana Prince | Marketing | 72000.00 |
5 | Evan Roberts | HR | 60000.00 |
6 | Fiona Clark | IT | 88000.00 |
Example 1: Filtering Employees with Names Starting with âAâ or âFâ in Specific Departments
Query:
SELECT *
FROM employees
WHERE (employee_name LIKE 'A%' OR employee_name LIKE 'F%')
AND department IN ('Marketing', 'IT');
Output:
employee_id | employee_name | department | salary |
---|
1 | Alice Johnson | Marketing | 75000.00 |
6 | Fiona Clark | IT | 88000.00 |
Explanation: This query retrieves all employees whose names start with 'A' or 'F' and belong to the departments 'Marketing' or 'IT'. The parentheses around the LIKE
conditions ensure that the OR
condition is evaluated before the AND
condition with IN
.
Example 2: Filtering Employees with Names Containing âCharlieâ or âBobâ in Specific Departments
Query:
SELECT *
FROM employees
WHERE (employee_name LIKE '%Charlie%' OR employee_name LIKE '%Bob%')
AND department IN ('IT', 'Sales');
Output:
employee_id | employee_name | department | salary |
---|
2 | Bob Smith | Sales | 68000.00 |
3 | Charlie Brown | IT | 95000.00 |
Explanation: This query retrieves all employees whose names contain the word 'Charlie' or 'Bob' and belong to the departments 'IT' or 'Sales'. The use of parentheses ensures that the OR
conditions for the LIKE
pattern matching are properly grouped before applying the AND
condition with IN
.
Conclusion
Combining the LIKE
and IN
operators in SQL Server provides a powerful way to filter data based on both specific patterns and predefined categories. This versatile approach allows for complex and precise queries, enhancing the capability to retrieve relevant information efficiently.
Similar Reads
How to Combine LIKE and IN in SQL Statement
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 valuesIn this article, we will explain ho
3 min read
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
INNER JOIN ON vs WHERE clause in SQL Server
In SQL Server, joining tables and filtering data are essential for retrieving meaningful information. The INNER JOIN operation is used to combine rows from multiple tables based on a matching condition, while the WHERE clause allows for further filtering of results. In this article, we will PL/SQL S
7 min read
What is the Difference Between NOT EXISTS and NOT IN SQL Server?
When applying filters on a result set that is stored in SQL Server, there are occasions where one wants to apply conditions where specific records exist or do not exist. Not exists and not in are two of the most basic, but deciding when to use them can be a real challenge. In this article, we will l
7 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
Select into and temporary tables in MS SQL Server
In SQL Server, the SELECT INTO TEMP TABLE statement is used to select data from one or more source tables and insert it into a temporary table. Temporary tables are extremely useful when dealing with intermediate results, or when working with subsets of data within a session without modifying or aff
4 min read
How to Index Foreign Key Columns in SQL Server
Indexing foreign key columns in SQL Server is a vital optimization strategy for enhancing query performance, particularly when dealing with JOIN operations between related tables. Foreign keys enforce relationships between tables by linking a column (or columns) in one table to the primary key of an
4 min read
How to Use Reserved Words as Column Names in SQL?
In SQL, certain words are reserved. These are called Keywords or Reserved Words. These words cannot be used as identifiers i.e. as column names in SQL. But, there is an exception to this rule too. In this article, we will discuss how to use Reserved Words as column names in SQL. For this article, we
2 min read
Difference Between EXISTS and IN in SQL Server?
The SQL Server database developer is quite familiar with the filtering and retrieving operators which enable the developer to execute the query rapidly. When it comes to these operators namely IN and EXISTS, they share almost similar purposes but work differently at the same level. Understanding the
4 min read
SQL Server Universal Comparison Quantified Predicates (ANY, ALL, SOME)
Quantified Comparison Predicates are used to compare the value with every value in the set returned by the subquery. There are mainly two predicates available in SQL Server that are ALL and ANY/SOME. This clause is very useful when you have to compare two sets of values. These clauses are used for b
5 min read