Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
What is Nested Select Statement in PostgreSQL?
Next article icon

What is Nested Select Statement in PL/SQL?

Last Updated : 08 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Understanding Nested Select Statement in PL/SQL

The nested select statement in PL/SQL is a feature that allows for the retrieval of data from multiple tables in a single query. It involves using a SELECT statement within another SELECT statement to fetch data based on specified conditions.

The main concept of a nested select statement in PL/SQL involves using a SELECT statement within another SELECT statement. This allows for the retrieval of data from multiple tables or views based on specified conditions. The syntax for a nested select statement is as follows:

Syntax:

-- Outer SELECT Statement: Retrieves data from table1 based on conditions

SELECT column1, column2, ...
FROM table1
WHERE condition IN (

-- Nested SELECT Statement: Retrieves data from table2 based on separate conditions

SELECT column3, column4, ...
FROM table2
WHERE condition
);

Explanation of Syntax:

Outer SELECT Statement:

  • The outer SELECT statement start the query execution by specifying the columns (column1, column2, etc.) which we want to retrieve from table1 and generally defines the foundation of the query and indicates the primary data source.

Nested SELECT Statement:

  • Nested within the IN clause of the outer SELECT statement.This inner query selects specific columns (column3, column4, etc.) from table2 and operates independently and retrieving data from table2 based on its own set of conditions.

Examples of Nested Select Statement in PL/SQL

Examples 1

Let’s Say we have two tables, "employees" and "departments." with the Following Data.

We will create the Employees and Departments table which defines the table structure with columns such as emp_id, emp_name, dept_Id and Salary of Employees TABLE and dept_Id, dept_name, and location of departments TABLE which specify the appropriate data types for each column to store information about employees and departments.

Query:

-- Create departments table

CREATE TABLE departments
(
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50),
location VARCHAR(50)
);

INSERT INTO departments (dept_id, dept_name, location) VALUES
(1, 'IT', 'New York'),
(2, 'HR', 'London'),
(3, 'Finance', 'New York');

-- Create employees table

CREATE TABLE employees
(
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT,
salary DECIMAL(10, 2),
FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);

INSERT INTO employees (emp_id, emp_name, dept_id, salary) VALUES
(101, 'John Doe', 1, 60000.00),
(102, 'Jane Smith', 1, 65000.00),
(103, 'Michael Johnson', 2, 55000.00),
(104, 'Emily Brown', 3, 70000.00),
(105, 'David Lee', 1, 62000.00);

After inserting data into the departments and employees table, The table looks:

example1
Departments and Employees tables

In this example we want to retrieve the names of employees along with their corresponding department names. We can achieve this using a nested select statement as shown below:

Query:

SELECT emp_name, 
(SELECT dept_name
FROM departments
WHERE departments.dept_id = employees.dept_id) AS dept_name
FROM
employees
WHERE
dept_id IN (
SELECT dept_id
FROM departments
WHERE location = 'New York'
);

Output:

eg1ans
Output

Explanation: We can observe that we get the names of employees along with their corresponding department names.

Examples 2

Let’s Say we have two tables Students and Grades with the Following Data.

We can create the Students and Grades table using the following query which defines the table structure with columns such as student_id, and name of Students TABLE and student_id, course, and grade of Grades TABLE which specify the appropriate data types for each column to store information about Students and Grades.

Query:

-- Create students table

CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(100)
);


INSERT INTO students (student_id, name) VALUES
(1, 'John Doe'),
(2, 'Jane Smith'),
(3, 'Michael Johnson'),
(4, 'Emily Brown');

-- Create grades table

CREATE TABLE grades (
student_id INT,
course VARCHAR(100),
grade CHAR(1)
);


INSERT INTO grades (student_id, course, grade) VALUES
(1, 'Math', 'A'),
(1, 'Science', 'B'),
(2, 'Math', 'B'),
(2, 'Science', 'A'),
(3, 'Math', 'A'),
(3, 'Science', 'A'),
(4, 'Math', 'C'),
(4, 'Science', 'B');

After inserting data into the students table, The table looks:

students-
Students Table

After inserting data into the grades table, The table looks:

grade-
Grades Table

In this example We want to retrieve the names of students who have achieved an 'A' grade in any course. We can achieve this using a nested select statement.

Query:

SELECT name
FROM students
WHERE student_id IN (
SELECT student_id
FROM grades
WHERE grade = 'A'
);

Output:

output
Output

Explanation: We can observe that the names of students who have achieved an 'A' grade in any course.

Conclusion

Overall, the nested select statement in PL/SQL provides a method for retrieving data from multiple tables based on specified conditions. By using single SELECT query within another which allow the user to efficiently perform query and manipulate data across related tables in a single operation. With a good understanding of nested select statements you can easily write more complex and efficient SQL queries.


Next Article
What is Nested Select Statement in PostgreSQL?

K

kartikey0501
Improve
Article Tags :
  • Databases
  • PL/SQL
  • PL/SQL Query

Similar Reads

  • What is Nested Select Statement in SQL Server
    SQL Server is a powerful relational database management system. Sql Server is very good with its robustness and scalability. SQL Server operates as a client-server structure, imparting centralized management and scalability for huge-scale applications and enterprise-stage solutions. It offers advanc
    3 min read
  • What is Nested Select Statement in PostgreSQL?
    Nested select statements, also known as subqueries which is a fundamental concept in PostgreSQL and play an important role in data retrieval and manipulation. In PostgreSQL, a powerful relational database management system, the nested select statements allow for complex queries by nesting one query
    5 min read
  • What is Nested Select Statement in MariaDB
    Nested select statements, normally named subqueries, represent an advanced feature for MariaDB which enables more efficient and thorough querying of the data. Therefore, the nesting of a SELECT statement within another allows us to perform operations and filtering that could be hardly possible with
    4 min read
  • Nested Select Statement in MySQL
    The relational databases, the ability to retrieve and manipulate data with precision is a cornerstone of effective database management. MySQL, a popular relational database management system, provides a powerful tool called the Nested SELECT statement that empowers developers to perform complex and
    5 min read
  • SQL INSERT INTO SELECT Statement
    In SQL, the INSERT INTO statement is used to add or insert records into the specified table. We use this statement to insert data directly into a table by specifying column names in a specific order. The SELECT statement is used to retrieve data from the table, and it can be used in conjunction with
    6 min read
  • SQL SELECT INTO Statement
    The SELECT INTO statement in SQL is a powerful and efficient command that allow users to create a new table and populate it with data from an existing table or query result in a single step. This feature is especially useful for creating backups, extracting specific subsets of data, or preparing new
    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 b
    4 min read
  • Select Statement in MS SQL Server
    The SELECT statement in SQL Server is a foundational SQL command used for querying and retrieving data from one or more tables within a database. This command allows users to specify which columns and rows to retrieve and apply filters to focus on specific data and perform various operations to mani
    4 min read
  • PL/SQL INSERT Statement
    The PL/SQL INSERT statement is vital for adding new records to a database table. By specifying the table's name and providing values for its columns, users can populate their database with essential information. This functionality enables efficient data entry and ensures the completeness of datasets
    3 min read
  • MySQL INSERT INTO SELECT Statement
    MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manipulate databases. It stores data in a table format. It provides various statements to perform Create, Read, Update, and Delete operations on a database table. INSERT INTO SELECT statement i
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences