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_deptStep 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_dataNow 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_deptOutput:
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 Update Table Rows in PL/SQL Using Subquery?
Updating table rows in PL/SQL via subqueries allows precise data modifications based on specific conditions. By integrating subqueries within the UPDATE statement, rows can be selectively targeted for updates, enhancing data management efficiency. This article explores the concept of updating rows i
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 in SQL Server using Subquery ?
Updating table rows in SQL Server using a subquery is a common operation that allows us to modify records based on values derived from another table or query. In this article, we will explain how to update table rows in SQL Server using subquery with the help of examples. Updating Table Rows Using a
3 min read
How to Update Table Rows in PostgreSQL Using Subquery?
PostgreSQL is a general-purpose object-relational database management system and is open-source. It ensures data integrity features like constraints, transactions and foreign key support. In this article, We will understand How to update table rows in PostgreSQL using subquery using various methods,
5 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 Update Table Rows in SQLite Using Subquery
SQLite is a lightweight, serverless RDBMS that is used to store and manipulate the data in the database in relational or tabular form. It can run on various operating systems like Windows, macOS, Linux and mobile operating systems like Android and iOS. SQLite is designed for single-user access. Mult
4 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 PostgreSQL
In PostgreSQL, efficiently selecting rows with maximum values from a table is a common task faced by developers and database administrators. Whether you're working on analytics, reporting, or data manipulation, knowing how to retrieve the maximum value per group can significantly enhance your SQL sk
4 min read