Lecture12 - 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Sub Query

By Maria Rahim
What Is A Subquery

 A subquery is a query within another query. The outer query is called as main query and
inner query is called as subquery.
IN operator in Sub Queries

 IN operator plays a very important role in subqueries as generally subqueries generate a


list of values and main query is to compare a single value against the list of values supplied
by subquery.
The following query displays the details of the
faculty members who have not taken any batch in the
last three months
Multiple Subqueries

 It is possible for a main query to receive values from more than one subquery. The
following example displays the details of batches that are taken by faculty with
qualification MS or the course fee is more than 5000.
Nesting Subqueries
 It is also possible to nest subqueries. So far we have seen examples where a single
subquery is executed and sends values to main query. It is also possible for a subquery to
depend on another subquery and that subquery on another and so on.
 The following example displays the details of the students who belong to batches that are
taken by faculty with qualification MS.
The following is another example where we will take details
of payments made by students of the batch that started on
12-jul-2001
Comparing more than one value

 A subquery can return multiple columns. These multiple columns must be compared with
multiple values. The following query displays the details of the batches that have taken
maximum duration among the batches of the same course.
Get the details of course that has highest course
fee.
Subqueries in DML and DDL commands

Subqueries can also be used with DML commands. WHERE clause of UPDATE and DELETE
can always contain a subquery. The following UPDATE command increases the FEE of the
course if more than 5 batches have started for that course.
The following subquery creates a new table
from an existing table.
Renaming a column using subquery

 The following procedure will illustrate how to use subquery with DDL to rename a column
in a table. Renaming a column is not permitted in Oracle. So to rename a column, follow
the given procedure. However, it is to be noted that this procedure is lengthy and not very
refined. But you can consider in case of desperate need.
Subquery in VALUES clause Since Oracle8i

 it is possible to use a subquery in VALUES clause of INSERT command. Prior to Oracle8i


it was possible to use subquery with INSERT command but not in VALUES clause of
INSERT command.
 The following INSERT command gets the next ROLLNO for a new student by using a
subquery.

 insert into students values ( (select max(rollno) + 1 from students), 'b7', 'Robert Lafore',
'm', sysdate, null, null);
ORDER BY is permitted in subquery – TOPn
analysis
 Oracle8i has allowed the ORDER BY clause to be used with subquery. The
following query will use ORDER BY clause to get courses in descending order.
Then main query will take the data sent by the subquery and selects only first two
rows. As the result, the query will display the details of course with first two
highest course fee.
 ROWNUM pseudo column contains the row number for the retrieved rows. The
query uses ROWNUM and takes only those rows that have row number less than
3.
Correlated Subquery

 If there is any correlation between main query and subquery then subquery is called as
correlated subquery.
 A correlated subquery is a subquery that receives some input from main query and sends
result back to main query. Unlike normal subquery, a correlated subquery receives value
from main query. It uses the value (generally in condition) and sends the results of the
query back to main query.
Differences between normal and correlated
subquery
EXISTS and NOT EXISTS operators

 These two operators are exclusively used in correlated subquery. EXISTS checks whether any row is
returned by subquery and condition will be true if subquery returns any rows. Whereas, NOT EXISTS
returns true if subquery doesn’t retrieve any row.
 EXISTS is different from other operators like IN,ANY etc., because it doesn’t compare values of
columns, instead, it checks whether any row is retrieved from subquery or not. If any row is
retrieved from subquery the EXISTS returns true otherwise it returns false.
 The following query displays details of courses for which at least one batch started in this month.
Examples
ANY and ALL Operators

 Both are used for comparing one value against a set of values. ALL specifies that all the
values given in the list should be taken into account, whereas ANY specifies that the
condition is satisfied when any of the values satisfies the condition.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy