Professional Documents
Culture Documents
Sub Queries
Sub Queries
Sub Queries
Sub queries
Introduction
A subquery is a query within another query. The outer query is called as main query and inner
query is called as subquery.
- A FROM clause
- A WHERE clause
• The comparison operator canalso be a multiple-row operator, such as IN, ANY, or ALL.
• A subquery can be treated as an inner query, which is a SQL query placed as a part of
• The subquery (inner query) executes once before the main query (outer query) executes.
• The main query (outer query) use the subquery result.
Now we want to write a query to identify all students who get better marks than that of the
student whoseStudentID is 'V002', but we do not know the marks of 'V002'. \
- To solve the problem, we require two queries. One query returns the marks (stored in
Total_marks field) of 'V002' and a second query identifies the students who get better marksthan
the result of the first query.
First query:
SELECT *
FROM ‘marks’
WHERE studentid = ‘V002’;
Query result:
Second query:
SELECT a.studentid, a.name, b.total_marks
FROM student a, marks b
WHERE a.studentid = b.studentid
AND b.total_marks >80;
Page | 2 By - Dr. Hemangini S. Patel
DBMS ~: UNIT 1:~ SUB QUERIES
Query result:
Above two queries identified students who get better number than the student who's
StudentID is 'V002' (Abhay).
You can combine the above two queries by placing one query inside the other. The subquery
(also called the 'inner query') is the query inside the parentheses. See the following code and
query result
Type of Subqueries
• Single row subquery: Returns zero or one row.
If we want to insert those orders from 'orders' table which have the advance_amount 2000 or
5000 into 'neworder' table the following SQL can be used:
In an UPDATE statement, you can set new column value equal to the result returned by asingle
row subquery. Here is the syntax and an example of subqueries using UPDATE statement.
UPDATE table
SET column_name = new_value
[ WHERE PREDICATE] C O MP A R I S I O N O P ER A T O R
(SELECT COLUMN_NAME
FROM TABLE_NAME);
If we want to update those ord_date in 'neworder' table with '15-JAN-10' which have the
difference of ord_amount and advance_amount is less than the minimum ord_amount or
'orders' table the following SQL can be used:
If we want to delete those orders from 'neworder' table which advance_amount are less thanthe
maximum advance_amount of 'orders' table, the following SQL can be used:
Sample table :neworder
Difference Between
Subquery and Correlated
Subquery
Subquery
In a normal subquery, the outer query is dependent on the inner query for
execution
The inner query is only executed once to return the values required by the
outer query to execute
Syntax:
comparison operator
Example:
Output:
Explanation:
The inner query i.e “Select Emp_id from Department where Dept_id=’1'”
it gives Emp_id from the Department table whose Emp_id is ‘1’. Then the
outer query takes it as input to the where clause and executes to give the
Name, Address, Phone, Salary of the Employees who work in Department
1.
Correlated Subquery
In this query, the correlated subquery is evaluated once for each row of the
outer query.
Unlike the normal subquery,the inner query is dependent on the outer query
for values
Each time the inner query gets executed it goes to the outer query for values
Syntax:
SELECT column_name(s)
Output:
Explanation: