SUBQUERY
A query written inside another query is known as a Subquery.
There are two queries in a subquery:
1. Inner Query
2. Outer Query
WORKING PRINCIPLE OF SUBQUERY
Let us consider two queries: Outer Query and Inner Query.
1. The Inner Query executes first and produces an output.
2. The output of the Inner Query is given as input to the Outer Query.
3. The Outer Query then generates the final result.
Therefore, we can say that the Outer Query is dependent on the Inner Query.
This is the execution principle of a Subquery.
BASIC SYNTAX
SELECT column_name
FROM table_name
WHERE column_name = (
SELECT column_name
FROM table_name
WHERE condition
);
USES OF SUBQUERY
CASE 1:
Whenever we have an unknown value present in the question, we use a subquery to find the unknown value.
CASE 2:
Whenever the data to be selected and the condition to be executed are present in different tables, we use subqueries.
TYPES OF SUBQUERY
There are mainly two types of subqueries:
1. Single-Row Subquery
2. Multi-Row Subquery
SINGLE-ROW SUBQUERY
If the subquery returns exactly one record or value, we call it a Single-Row Subquery.
If it returns only one value, then we can use the normal operators or the special operators to compare the value.
Example:
SELECT *
FROM Employee
WHERE Salary > (
SELECT Salary
FROM Employee
WHERE Employee_ID = 1
);
Here, the inner query returns one value, so it is a Single-Row Subquery.
Get Rishil Aman’s stories in your inbox
Join Medium for free to get updates from this writer.
MULTI-ROW SUBQUERY
If the subquery returns more than one record, we call it a Multi-Row Subquery.
If it returns more than one value, then we cannot use normal operators directly. We have to use special operators to compare the values.
The important special operators are:
1. ALL
2. ANY
SUBQUERY OPERATORS
ALL
ALL is a special operator used along with relational operators to compare the value with the values present on the right-hand side.
The ALL operator returns TRUE if the comparison condition is satisfied for all the values returned by the subquery.
Syntax:
SELECT column_name
FROM table_name
WHERE column_name > ALL (
SELECT column_name
FROM table_name
WHERE condition
);
ANY
ANY is a special operator used along with relational operators to compare the value with the values present on the right-hand side.
The ANY operator returns TRUE if the comparison condition is satisfied for at least one value returned by the subquery.
Syntax:
SELECT column_name
FROM table_name
WHERE column_name > ANY (
SELECT column_name
FROM table_name
WHERE condition
);
NESTED SUBQUERY
A subquery written inside another subquery is known as a Nested Subquery.
In Oracle, we can nest up to 255 levels of subqueries.
Example:
SELECT *
FROM table1
WHERE column1 = (
SELECT column2
FROM table2
WHERE column2 = (
SELECT column3
FROM table3
)
);
EMPLOYEE AND MANAGER CONNECTION
To find the manager and employee relationship:
- To find the manager, the manager’s information should be in the subquery.
- To find the employee, the employee number should be in the subquery.
This concept can be used to establish the relationship between an employee and their manager using a subquery.





