How to Use the IN Operator With a SubQuery?
Last Updated :
12 Dec, 2021
The IN Operator in SQL allows to specifies multiple values in WHERE clause, it can help you to easily test if an expression matches any value in the list of values. The use of IN Operator reduces the need for multiple OR conditions in statements like SELECT, INSERT, UPDATE, and DELETE.
Sub Queries:
The SQL queries where one or more SELECT statements are nested with the WHERE clause of another SELECT statement are called subquery. The first statement of such type of query is called outer query where as the inside one is called an inner query. In the execution of such queries, the inner query will be evaluated first, and the outer query receives the value of the inner query.
Now, we will create a schema for our database and named it geeks for geeks. After that, we will create a table inside it with the name geeks_data and use IN operator with a sub-query.
Step 1: Create a database
In order to create a database, we need to use the CREATE operator.
Query :
CREATE DATABASE geeksforgeeks;
Output:

Figure: Create database
Step 2: Create a table inside the database
In this step, we will create two tables geeks_data and geek_dept inside the geeks for geeks database.
- Creating geeks_data table :
Query :
CREATE TABLE geeks_data(id INT,
first_name VARCHAR(255),
last_name VARCHAR(255),
dept VARCHAR(255),
PRIMARY KEY(id));

Figure: Create a table geeks_data
- Creating geeks_dept table :
Query:
CREATE TABLE geeks_dept(id INT,
dept_name VARCHAR(255),
PRIMARY KEY(id));

Figure: Create a table geeks_dept
Step 3: Insert data into the table
In order to insert the data inside the database, we need to use the INSERT operator. First, we will insert it in the geeks_data table.
Query:
INSERT INTO geeks_data VALUES
(1, 'Chandan', 'Mishra', 'Mechanical'),
(2, 'Abhinav', 'Singh', 'Electronics'),
(3, 'Utkarsh', 'Raghuvanshi', 'Computer Science');

Figure: Insert values in geeks_data table
Output:

Figure: geeks_data
Now we will insert data for the table geeks_dept.
INSERT INTO geeks_dept VALUES (1, 'Computer Science'),
(2, 'Electronics'),
(3, 'Computer Science'),
(4, 'Mechanical');

Figure: Insert data into geeks_dept
Output:

Figure: geeks_dept
Step 4: Executing IN Operator within a Sub Query
In this step we will try to find out the geek’s data from table geeks_data, those who are from the computer science department with the help of geeks_dept table using sub-query.
Query:
SELECT first_name, last_name FROM geeks_data WHERE dept IN
(SELECT dept_name FROM geeks_dept WHERE id = 1);

We will get the first_name and last_name of the geeks who are from the computer science department.
Output:

Figure: Result of IN operator within Sub Query
Similar Reads
How to Use the IN Operator with a SubQuery in SQL?
In this article, we will see the use of IN Operator with a SubQuery in SQL. IN operator is used to compare the column values with the list of values. The list of values is defined after IN query in SQL. If we don't know the exact list of values to be compared, we can generate the list of values usin
2 min read
How to use the IN operator in SQLAlchemy in Python?
In this article, we will see how to use the IN operator using SQLAlchemy in Python. We will cover 2 examples, one each for SQLAchemy Core and ORM layers. In both examples, we will count the number of records present in the category table within the sakila database. The sample data from the table loo
4 min read
How to Use a Subquery in a SELECT Statement
A subquery also known as a nested query is a query embedded within another query to perform advanced filtering or computation. In the context of a SELECT statement, subqueries allow retrieving values or conditions dynamically by enabling complex and flexible data extraction. In this article, We will
4 min read
How to Use âNOT INâ Operator in R?
In this article, we will discuss NOT IN Operator in R Programming Language. NOT IN Operator is used to check whether the element in present or not. The symbol used for IN operator is "%in%". For NOT IN operator we have to add " ! " operator before that , so the symbol for NOT IN operator is "! %in%"
2 min read
How to Update Table Rows Using Subquery in MySQL
Updating table rows using subqueries in MySQL enables precise modifications based on specific conditions or values from other tables. This technique leverages subqueries within the SET or WHERE clauses of the UPDATE statement, allowing dynamic and context-specific updates. This guide covers the synt
5 min read
How to use Relational Operators in MySQL
In the database management area, relational operators are the building blocks for data filtering and querying. MySQL, one of the most popular relational database management systems that are widely used across the world, features a wide range of relational operators that are highly efficient in handl
6 min read
How to Select Row With Max Value in in SQLite
In SQLite, retrieving rows with the maximum value for a specific column, grouped by another column's distinct values can be a challenging task. Whether you're analyzing data trends or identifying top performers, this operation can provide valuable output. In this beginner-friendly guide, we will exp
4 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 in SQL?
SQL(Structured Query Language) is a powerful tool that is used to manage and query data in relational databases. A common requirement in data analysis is finding the maximum value in a column for each distinct value of another column, such as determining the highest salary in each department. This c
5 min read
How to Use Count With Condition in PostgreSQL?
In PostgreSQL, the COUNT() function serves as a tool for tallying the number of records within a table. This article aims to address this query, delving into the nuances and implications of integrating conditions into the COUNT() function in PostgreSQL. The COUNT() function in PostgreSQL is traditio
4 min read