How to Select Group of Rows that Match All Items on a List in SQL Server?
Last Updated :
28 Oct, 2021
In this article, we will see, how to select a group of rows that match all the items on a list in SQL Server.
We can perform the above function by two methods. Those are:
- By using IN operator
- BY performing JOIN using STRING_SPLIT method
IN operator: It allows you to specify a list of values in a WHERE clause.
STRING_SPLIT(): this function can be used to split a character expression using a specified separator like comma(','), etc.
For demonstration follow the below steps:
Step 1: Create a database
The database can be created using CREATE command.
Query:
CREATE DATABASE geeks;
Step 2: Using a database
Use the below SQL statement to switch the database context to geeks:
Query:
USE geeks;
Step 3: Table definition
We have the following demo_table in our geek's database.
Query:
CREATE TABLE demo_table(
NAME VARCHAR(20),
AGE INT,
CITY VARCHAR(20) );
Step 4: Insert data into a table
Query:
INSERT INTO demo_table VALUES
('ROMY KUMARI', 22, 'NEW DELHI'),
('PUSHKAR JHA',23, 'NEW DELHI'),
('RINKLE ARORA',23, 'PUNJAB'),
('AKASH GUPTA', 23, 'UTTAR PRADESH'),
('AKANKSHA GUPTA',22, 'PUNJAB'),
('SUJATA JHA', 30,'PATNA')
('PREETI GIRI', 26,'BANGLORE'),
('PREM GUPTA',31,'PUNE');Step 5: View data of the table
Query:
SELECT * FROM demo_table;
Output:

Step 6: Select a group of rows matching the items in the list.
For the purpose of demonstration let's suppose we have a list having the following items: (21, 45, 23, 31). We will select the group of rows whose values in the AGE column match the items in the list.
Method 1: Using IN operator
Syntax:
SELECT *FROM table_name WHERE column_name IN (list);
Query:
SELECT * FROM demo_table WHERE AGE IN (21, 45, 23, 31);
Output:

Method 2: Performing JOIN using STRING_SPLIT() function
Syntax:
SELECT * FROM table_name
JOIN STRING_SPLIT('list', 'separator')
ON value = column_name;Query:
SELECT * FROM demo_table
JOIN STRING_SPLIT('21, 45, 23, 31', ',')
ON value = AGE;Output:
Similar Reads
How to SELECT Top N Rows For Each Group in SQL Server
SQL Serverâs ROW_Number() window function is often used to get the top number of rows in a group. In this article, weâll look at how to get the top N Row Counts in SQL Server using this function. Weâll provide step-by-step instructions, syntax examples, and examples to help you get the results you n
4 min read
How to Select the nth Row in a SQL Server Database Table?
In SQL Server databases, it's common to encounter scenarios where we need to retrieve a specific row, such as the nth row, from a table efficiently. Whether you're building a pagination feature for a web application or analyzing data, having the ability to select a particular row based on its positi
3 min read
How to Retrieving the Last Record in Each Group in SQL Server
In SQL Server data analysis, the need to retrieve the last record in each group is a common and essential task. Consider a scenario where you are managing a database of customer orders. Each order is associated with a unique order ID and a customer ID, and you want to identify the most recent order
4 min read
How to Select the Last Records in a One to Many Relationship using SQL Server
In database management, handling one-to-many relationships is common, where a record in one table relates to multiple records in another. For instance, a parent table may have one record linked to multiple child records in a related table. Retrieving the latest record from the "many" side for each r
6 min read
How to Select the First Row of Each GROUP BY in SQL?
Selecting the first row of each group in SQL is a common requirement in database queries, especially when working with grouped data. SQL GROUP BY queries are highly effective for organizing data into logical groups, and additional techniques like window functions help to isolate the first row from e
5 min read
How to Limit Rows in a SQL Server?
To limit rows in SQL Server, use the TOP clause in the SELECT statement. Using the TOP clause in SQL Server, users can limit the number of rows in the results set. Here, we will understand how to limit rows in SQL Server with the help of different examples. Steps to Limit Rows in SQL ServerLet's che
3 min read
How to Select Row With Max Value in SQL Server
In SQL Server, retrieving rows that contain the maximum value for a specific column for each distinct value in another column can be a common and challenging task. This process is done by identifying the maximum value for each group and then selecting the corresponding rows. In this article, we'll e
6 min read
How to Select Row With Max Value on a Column in SQL?
SQL is a powerful language for managing and handling relational databases. A common requirement in database management is to retrieve rows where a specific column has the maximum value. Here, we will look at different ways to do this, depending on different situations and database environments. This
5 min read
How to Select Rows with no Matching Entry in Another Table in SQLite?
In database management, selecting rows from one table that does not have matching entries in another table means returning the rows that are present in one table but do not have the same entry in any other table. This scenario often arises in various data validation and analysis processes. In this a
5 min read
How to Concatenate Text From Multiple Rows in SQL Server
When we fetch data from a table, there may be requirements to concatenate the text value of a table column in multiple rows into a single row. There are many ways we can concatenate multiple rows into single row SQL Server. We can use different ways based on need and convenience. In this article, we
6 min read