0% found this document useful (0 votes)
5 views27 pages

Subquery ORACLE

The document explains the concept of subqueries in SQL, detailing their types: non-correlated and correlated subqueries. It provides examples of how to use subqueries to retrieve employee and department data based on various conditions. Additionally, it outlines the syntax and types of subqueries, including predicate, scalar, and table subqueries, along with practical SQL queries demonstrating their usage.

Uploaded by

majnubhai730
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Subquery ORACLE

The document explains the concept of subqueries in SQL, detailing their types: non-correlated and correlated subqueries. It provides examples of how to use subqueries to retrieve employee and department data based on various conditions. Additionally, it outlines the syntax and types of subqueries, including predicate, scalar, and table subqueries, along with practical SQL queries demonstrating their usage.

Uploaded by

majnubhai730
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

SUBQUERY IN SQL

To illustrate the SUBQUERY, we consider the instances of the following tables to illustrate this
section.

EMP table:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E01 KOUSHIK GHOSH 5000 D01 SYSTEM ANALYST 10-MAR-93
E02 JAYANTA DUTTA 3500 D01 PROGRAMMER 15-JAN-94
E03 HARI NANDAN TUNGA 4000 D02 PROGRAMMER 01-JUL-95
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96
E07 RANI BOSE 3000 D01 PROJECT ASSISTANT 17-JUN-97
E08 GOUTAM DEY 5000 D01 PROGRAMMER 23-OCT-97
E09 PINAKI BOSE 5500 D02 PROGRAMMER 26-AUG-94

DEPT table:

DNO DNAME CITY


----- ------------------------------ -----------
D01 PROJECT KOLKATA
D02 RSEARCH CHENNAI
D03 PERSONNEL KOLKATA

PROJECT table:

PID PNAME LOCATION


----- ------------------------------ --------------
P01 HOSPITAL MANAGEMENT SYSTEM KOLKATA
P02 ACCUNTING SYSTEM KOLKATA
P03 BANKING INFORMATION SYSTEM CHENNAI

ASSIGN table:

ECODE PID
----- -----
E01 P01
E01 P02
E01 P03
E02 P01
E08 P01
E02 P02
E08 P02
E07 P02
E03 P03
E09 P03
E06 P03

Compiled By Manas Ghosh


1
So far we have worked with queries that have either a comparison statement or a compound
comparison statement in the WHERE clause. It is possible to have a query in the WHERE clause
of a SELECT statement. The query in the WHERE clause is called a subquery. The subquery is
often referred to as the inner query and the surrounding query is called the outer query.
These Subqueries may be written into two forms-
• NON-CORRELATED
• CORRELATED
In non-correlated subquery, first inner query is executed first. The outer query takes an action
based on the results if the inner query. In correlated subquery inner query needs values from
outer query and passes results to outer query. It is important to know for a simple (or correlated
subquery) the subquery is executed only once.
Let us illustrate the above forms with an example:

Query: Find the employees who are working in department located at KOLKATA.
Using non-correlated subquery the SQL statement will be:

OUTER QUERY

SELECT ecode, ename, dno FROM emp


WHERE dno IN (SELECT dno FROM dept WHERE city=’KOLKATA’);

INNER QUERY

Inner query is independent gets executed first, passes results to the outer query. Using correlated
subquery the above one can be rewritten as-
SELECT ecode, ename, dno
FROM emp outer
WHERE dno IN (SELECT dno FROM dept WHERE city = ‘KOLKATA’
AND dno =outer.dno);

Output:

ECODE ENAME DNO


----- ------------------------------ -----
E01 KOUSHIK GHOSH D01
E02 JAYANTA DUTTA D01
E04 JAYANTA GANGULY D03
E05 RAJIB HALDER D03
E07 RANI BOSE D01
E08 GOUTAM DEY D01

6 rows selected.

Correlated sub queries operate in a repetitive manner. Those familiar with looping control
structures of a programming language will see the similarity. In a correlated sub query, the outer

Compiled By Manas Ghosh


2
query runs producing one test record at a time. For each record that the outer query produces,
the inner query is supplied one or more columns from the outer query. The inner query runs when
it receives column(s) from the outer query and produces a result set. After the inner query
produces a result set, the outer query evaluates the comparison statements containing the inner
query. If the comparison statements evaluate favorably, the record produced by the outer query is
returned and displayed. This process continues until the outer query tests each test record. Note
that a test record is a record that is produced by the outer query by relaxing the conditions
containing the inner query.
The full set of test records for this correlated sub query produced are listed below by running the
relaxed query. The relaxed query is the original outer query with the condition imposed by the
inner query having been removed. (remove the inner query and its condition to get the relaxed
query)

Relaxed Query
SELECT ecode, ename, dno FROM emp;

Test records
ECODE ENAME DNO
----- ------------------------------ -----
E01 KOUSHIK GHOSH D01
E02 JAYANTA DUTTA D01
E03 HARI NANDAN TUNGA D02
E04 JAYANTA GANGULY D03
E05 RAJIB HALDER D03
E06 JISHNU BANERJEE D02
E07 RANI BOSE D01
E08 GOUTAM DEY D01
E09 PINAKI BOSE D02
9 rows selected.

HOW IT WORKS: Steps involved.

Step 1 First the outer query runs and supplies a column from the first test record to the inner
query

The first record is

ECODE NAME DNO


---------------------------------------------- --------------- Column outer.dno = ‘D01’ is
E01 KOUSHIK GHOSH D01 supplied to inner query.

Step 2 Then the outer query shares the outer.dno column value of ‘D01’ with the inner query and
the inner query runs. Let’s see the result of the inner query.

SELECT dno
DNO
FROM dept Column outer.dno is supplied to the -----------
WHERE city = ‘KOLKATA’ inner query. The inner query runs and D01
produces the result set on the right.
AND dno = ‘D01’;

Step 3 The inner query can now provide a value to the outer query’s WHERE clause. Now the
outer query can evaluate the comparison condition in the WHERE clause. Notice that we have to

Compiled By Manas Ghosh


3
simulate the current test record by adding first and last name conditions to freeze outer query.
Let’s run it and see what we get.

SELECT ecode, ename, dno


FROM emp outer
Inner query return value replaces the subquery in the outer
WHERE dno = ‘D01’; query evaluation.

This test record satisfies WHERE clause condition and is returned

ECODE ENAME DNO


----- ------------------------------ -----
E01 KOUSHIK GHOSH D01

Step 4 The outer query has found a match so E01’s information is returned.

First returned record

ECODE ENAME DNO


----- ------------------------------ -----
E01 KOUSHIK GHOSH D01

Continue Step 1 through Step 4 for each record in the test record set.

As we have seen, a simple subquery is executed once prior to the execution of the outer query.
With a correlated subquery, the inner query is executed once for each record that is returned by
the outer query. The inner query is driven by a correlation between the outer query and the inner
query. The correlation provided by an exchange of column(s) from the outer query to the inner
query. A table alias is used to share the outer query column(s) with the inner query.
The general form of subquery is as follows-

SELECT [DISTINCT] <column_list>


FROM <table_list>
WHERE <column_name> | <expression> {[NOT] IN | <comparison_operator >
[ANY | ALL] | [NOT] EXISTS}
(SELECT [DISTINCT] <column_list>
FROM <table_list>
WHERE <condition>)
GROUP BY <group_by_list>
HAVING <condition>
ORDER BY <order_by_list>;

Types of Sub queries


There are 3 basic types of subqueries in SQL:
• Predicate Subqueries -- extended logical constructs in the WHERE (and HAVING)
clause.

Compiled By Manas Ghosh


4
• Scalar Subqueries -- standalone queries that return a single value; they can be used
anywhere a scalar value is used.
• Table Subqueries -- queries nested in the FROM clause.
All subqueries must be enclosed in parentheses.

PREDICATE SUBQUERIES

Predicate subqueries are used in the WHERE (and HAVING) clause. Each is a special logical
construct. Predicate subqueries may be classified into following categories-

 Single value subquery(introduced with relational operator)


 Multiple value subquery(introduced with IN)
 Quantified Subqueries( introduced with comparison operator accompanied by ANY/ALL)
 Subqueries that are an existence test (introduced with EXISTS)

 Single value subquery (introduced with relational operator)

Subqueries returning single value from single column

The following generic SELECT statement depicts the syntax for a simple subquery returning a
single value.
SELECT <column_list>
FROM <table_list>
WHERE COLUMN <relational_operator> (SELECT statement);
Illustration:

A) NON-CORRELATED SUBQUERY

Query: List the employees working in the personnel department

SELECT * FROM emp WHERE dno=(SELECT dno FROM dept WHERE


dname=’PERSONNEL’);
Output:
ECODE ENAME SALARY DNO DESG DT_JN
---------- -------------------- ------------ ------- ------- ---------
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95

The above query may also be written as below-


SELECT * FROM emp WHERE 'PERSONNEL'=(SELECT dname FROM dept WHERE
dept.dno=emp.dno);

Query: Display the information of the employee(s) getting highest salary.

SELECT * FROM emp WHERE salary = (SELECT MAX(salary) FROM emp);

Compiled By Manas Ghosh


5
Output:
ECODE ENAME SALARY DNO DESG DT_JN
---------- -------------------- ------------ ------- ------- ---------
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96

Query: Retrieve the names and salaries for all employees who earn more than the average
salary.

SELECT ENAME, SALARY FROM EMP


WHERE SALARY > (SELECT AVG(SALARY) FROM EMP);
Output:
ENAME SALARY
------------------------------ ----------
KOUSHIK GHOSH 5000
JAYANTA GANGULY 6000
JISHNU BANERJEE 6500
GOUTAM DEY 5000
PINAKI BOSE 5500

• Non-Correlated subquery in HAVING - Selecting ONLY the group with the maximum Sum in
a group query

Query: Find a designation with lowest average salary.

SELECT desg, AVG (salary)


FROM emp
GROUP BY desg
HAVING AVG (salary) = (SELECT MIN (AVG (salary))
FROM emp
GROUP BY desg);
Output:
DESG AVG(SALARY)
------------------------------ -----------
PROJECT ASSISTANT 3000

Query: Display the name of the department and sum of salaries of all employees of that
department which is spending maximum sum of salaries.

SELECT a.dname, sum(b.salary) FROM dept a, emp b


WHERE a.dno = b.dno GROUP BY a.dname
HAVING SUM(b.salary) = (SELECT MAX(SUM(c.salary)) FROM emp c
GROUP BY c.dno);
Output:

DNAME SUM(B.SALARY)
------------------------------ -------------
PROJECT 16500

Compiled By Manas Ghosh


6
B) CORRELATED SUB QUERY

Query: List the employees who get the same salary as KOUSHIK GHOSH

SELECT * FROM emp A WHERE salary = (SELECT salary FROM emp B


where A.ecode<>B.ecode AND b.ename=’KOUSHIK GHOSH’);
Output:
ECODE ENAME SALARY DNO DESG DT_JN
---------- -------------------- ------------ ------- ------- ---------
E08 GOUTAM DEY 5000 D01 PROGRAMMER 23-OCT-97

Query: List the details of the employees getting highest salary in each department

SELECT * FROM emp A WHERE salary=(SELECT MAX(salary) FROM emp B


WHERE A.dno=B.dno) ORDER BY dno;
Output:
ECODE ENAME SALARY DNO DESG DT_JN
---------- -------------------- ------------ ------- ------- ---------
E01 KOUSHIK GHOSH 5000 D01 SYSTEM ANALYST 10-MAR-93
E08 GOUTAM DEY 5000 D01 PROGRAMMER 23-OCT-97
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96

Query: Retrieve the details of department having more than 2 employees


SELECT * FROM dept
WHERE 2 < (SELECT COUNT(*) from emp WHERE dept.dno=emp.dno);
OR
SELECT * FROM dept
WHERE (SELECT COUNT(*) from emp WHERE dept.dno=emp.dno) > 2;
Output:
DNO DNAME CITY
----- ------------------------------ -------------------------
D01 PROJECT KOLKATA
D02 RESEARCH CHENNAI

• Listing a certain number of records with the highest values for a certain column using
SQL Only.
There are times where it is needed to simply return the rows with a certain number of the highest
(or lowest) values for a certain column. For example,

Query: Display the code and salary of the first three employees getting highest salaries.
One solution for this problem is to use a correlated subquery to the same table. The following
select will return the correct rows:
SELECT ecode, salary FROM emp e1
WHERE 3 > (SELECT COUNT(*) FROM emp e2 WHERE e1.salary < e2.salary)
ORDER BY salary desc ;

Compiled By Manas Ghosh


7
Output:
ECODE SALARY
----- -------------------
E06 6500
E04 6000
E09 5500

For every row processed by the main query, the correlated subquery returns a count (COUNT(*) )
of the number of rows with higher salaries (WHERE e1.salary < e2.salary). Then the main query
only returns rows that have fewer than three salaries that are higher (WHERE 3 > ...). For
example, for ECODE=E09, the salary is "5500". There is only 2 rows with a higher salary
(ECODE=E06, E04), so the subquery returns "2", which is less than 3, causing the "WHERE 3 >
..." to evaluate to TRUE, thereby returning the row.

Query: Retrieve the 3rd highest salary of the EMP table

SELECT DISTINCT (e1.salary) FROM emp e1 WHERE 3 = (SELECT COUNT


(DISTINCT (e2.salary)) FROM emp e2 WHERE e1.salary<=e2.salary);
Output:

SALARY
----------
5500

Query: Display the details of the employee(s) getting third highest salary.

SELECT * FROM emp WHERE salary =( SELECT DISTINCT (e1.salary) FROM emp
e1 WHERE 3 = (SELECT COUNT (DISTINCT (e2.salary)) FROM emp e2 WHERE
e1.salary<=e2.salary));
Output:
ECODE ENAME SALARY DNO DESG DT_JN
---------- -------------------- ------------ ------- ------- ---------
E09 PINAKI BOSE 5500 D02 PROGRAMMER 26-AUG-94

Subqueries returning single value from each of the multiple columns

It is possible for a subquery to return more than one column for each row that it returns. In the
case that a simple subquery returns multiple columns a comparison can be made by creating a
list in comparing the return values of the subquery and the columns to be compared. Below is the
general syntax for a subquery returning multiple columns.
Single row subquery syntax for multiple columns is given below-
SELECT <column_list>
FROM <table_list>
WHERE ( column_name1, …, column_nameK ) = ( SELECT …..);

Compiled By Manas Ghosh


8
Illustration:

Query: List the details of the employees getting highest salary in each department

select * from emp


where (dno, salary)in(select dno, max(salary) from emp
group by dno);
Output:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E01 KOUSHIK GHOSH 5000 D01 SYSTEM ANALYST 10-MAR-93
E08 GOUTAM DEY 5000 D01 PROGRAMMER 23-OCT-97
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96

 Multiple value sub query(introduced with IN)

A subquery can return multiple values. In that case relational operator cannot be used for
comparison. Multi-row subqueries require a multivalue comparison operator such as IN. The IN
Subquery tests whether a scalar value matches the single query column value in any subquery
result row. The general form of such a query is as follows.
SELECT <column list>
FROM <table_list>
WHERE <column_name> [NOT] IN (SELECT statement);
Note: The comparison operator can use IN or NOT IN because the inner query can return a list.
Single value comparison operators such as >, <, <=, >= and = are not allowed.

A. Subqueries returning multiple values from single column

o NON-CORRELATED SUBQUERY

Query: List the departments having more than 2 employees working in that department.

SELECT * FROM dept WHERE dno IN (SELECT dno FROM emp GROUP BY dno
HAVING COUNT(*)>2);
Output:
DNO DNAME CITY
----- ------------------------------ -------------------------
D01 PROJECT KOLKATA
D02 RESEARCH CHENNAI

Query: Retrieve employee details who are not assigned in any project.

SELECT * FROM emp


WHERE ecode NOT IN (SELECT DISTINCT ecode FROM assign);

Compiled By Manas Ghosh


9
Output:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95

Query: Retrieve department details in which no PROGRAMMER is working.

SELECT * FROM DEPT


WHERE dno NOT IN(SELECT DISTINCT dno FROM emp
WHERE desg='PROGRAMMER');
Output:

DNO DNAME CITY


----- ------------------------------ -----------
D03 PERSONNEL KOLKATA

o CORRELATED SUBQUERY

Query: Which designations are common to more than one department?


SELECT DISTINCT A.desg
FROM emp A WHERE A.desg IN (SELECT B.desg
FROM emp B WHERE A.dno <> B.dno);
Output:

DESG
------------------------------
PROGRAMMER

NESTED SUBQUERY

It is possible to nest subqueries but it is not a practice that makes sense beyond three or four
levels deep. The following example demonstrates a nested subquery. Both subqueries are multi-
row subqueries.

Query: Retrieve the code, department number and name of each employee who are
working in the project located in the same city as their departments

Compiled By Manas Ghosh


10
ECODE
E01
E02
E03
Step 3: Outer query Step 2: Next E06
runs most inner E07
query runs. E08
SELECT ecode, dno, ename FROM emp E09
WHERE ecode IN (SELECT DISTINCT ecode FROM assigns
7 rows
WHERE pid IN (SELECT DISTINCT pid FROM dept, project
selected
WHERE city=location));
.
PID
Output: -----
Step 1: Most P01
inner query runs. P02
ECODE DNO ENAME P03
----- ----- ------------------------------
E01 D01 KOUSHIK GHOSH
E02 D01 JAYANTA DUTTA
E03 D02 HARI NANDAN TUNGA
E06 D02 JISHNU BANERJEE
E07 D01 RANI BOSE
E08 D01 GOUTAM DEY
E09 D02 PINAKI BOSE

7 rows selected.

Query: List the department details having maximum number of employees

SELECT * FROM dept


WHERE dno IN(SELECT dno FROM emp GROUP BY dno
HAVING COUNT(*) = (SELECT MAX(COUNT(*)) FROM emp
GROUP BY dno));
Output:
DNO DNAME CITY
----- ------------------------------ -------------
D01 PROJECT KOLKATA

B. Subqueries returning multiple values from the multiple columns

Multi row subquery syntax for multiple columns is as follows

SELECT <column_list>
FROM <table_list>
WHERE ( <column_name1>, …, <column_nameK> ) IN ( SELECT …..);

Compiled By Manas Ghosh


11
Query: Retrieve the code, name and department number of the employees who are
working in the project in the same city as their department.

SELECT DISTINCT E.ecode, ename, E.dno


FROM emp E, dept D, assigns A
WHERE E.dno=D.dno
AND E.ecode=A.ecode
AND (pid,city) IN (SELECT pid,location FROM project);
Output:
ECODE ENAME DNO
----- ------------------------------ -----
E01 KOUSHIK GHOSH D01
E02 JAYANTA DUTTA D01
E03 HARI NANDAN TUNGA D02
E06 JISHNU BANERJEE D02
E07 RANI BOSE D01
E08 GOUTAM DEY D01
E09 PINAKI BOSE D02

7 rows selected.
If we use equality operator(=) instead of IN operator in the third predicate then oracle generates
an error as inner subquery returns multiple rows
SELECT DISTINCT E.ecode, ename, E.dno
FROM emp E, dept D, assigns A
WHERE E.dno=D.dno
AND E.ecode=A.ecode
AND (pid,city) = (SELECT pid, location FROM project)
Output:
AND (pid,city) = (SELECT pid, location FROM project)
*
ERROR at line 5:
ORA-01427: single-row subquery returns more than one row

 Quantified Subqueries( introduced with comparison operator accompanied by


ANY/ALL)
Subqueries introducing with comparison operator and ANY/ALL ALL implies that all the values
results from the inner query must be taken into account. ANY, SOME and ALL are multi-valued
comparison operators that allow the use of single valued comparison operators such as >, <, <=,
>= and = on a list of values. These comparison operators can be used in any multi-row subquery.

Syntax:
SELECT <select_list>
WHERE <expression> <relational_operator> [ANY | ALL] (subquery);

➢ ALL
ALL works with a comparison operator and a list. The ALL comparison operator is true whenever
all the values, produced by the subquery, compares favorably with the column being compared.
>ALL means greater than all the values these return from the subquery i.e., greater than the
largest value among the values returns from the inner query.

Compiled By Manas Ghosh


12
>ALL (1, 2, 3) implies >3
Similarly <ALL (1, 2, 3) implies <1
=ALL(1,2,3) means =1 AND =2 AND =3 which is meaningless. ALL is used
basically with in equalities rather than equalities because a value can not be “equal to all” the
values. A value can be equal to all of the results of a subquery if all the said results are infact
identical.

Query: Find the employee getting the salary more than every employee of the ‘PROJECT’
department.

SELECT * FROM emp


WHERE salary>ALL(SELECT salary FROM emp
WHERE dno=(SELECT dno FROM dept WHERE dname = 'PROJECT'));
Output:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96
E09 PINAKI BOSE 5500 D02 PROGRAMMER 26-AUG-94

Query: Find the details of the employee(s) who is/are getting highest salary.

It can be written as using MAX( ) function


SELECT * FROM emp WHERE salary = (SELECT MAX(salary) FROM emp);
Using ALL it can be rewritten as

SELECT * FROM emp


WHERE sal>=ALL(SELECT salary FROM emp);
Output:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96

Query: List the department details having maximum number of employees

SELECT * FROM dept


WHERE dno IN(SELECT dno FROM emp GROUP BY dno
HAVING COUNT(*) >= ALL(SELECT COUNT(*) FROM emp
GROUP BY dno));
Output:
DNO DNAME CITY
----- ------------------------------ -------------
D01 PROJECT KOLKATA

It is to be noted that if the inner query introduced with ALL and a comparison operator, returns
NULL as one of its values, the entire query fails.

Compiled By Manas Ghosh


13
➢ ANY (SOME is the same as ANY)

ANY works with a comparison operator and a list. The ANY comparison operator is true
whenever one or more of the list elements compares favorably with the column being compared.
>ANY means that greater than at least one value i.e. greater than the minimum.
>ANY (1, 2, 3) is equivalent to >1
<ANY means that less than the maximum i.e. <ANY(1,2,3) implies <3
=ANY(1,2,3) means =1 OR =2 OR =3
<> ANY (1, 2, 3) means NOT 1 OR NOT 2 OR NOT 3
The following equivalences are listed.

• >=ALL (LIST) is equivalent to = MAX(LIST)


• <=ALL (LIST) is equivalent to = MIN (LIST)
• <> ANY is equivalent to = NOTIN (LIST)
• =ANY is equivalent to = IN (LIST)

Query: Find the employees getting salary larger than the minimum salary of the
PERSONNEL department.

It can be written as follows using MIN() group function.


SELECT * FROM emp
WHERE salary> (SELECT MIN(salary) FROM emp
WHERE dno =(SELECT dno FROM dept WHERE dname=’PERSONNEL’));

Using ANY query will be

SELECT * FROM emp


WHERE salary>ANY (SELECT salary FROM emp, dept
WHERE emp.dno = dept.dno AND dname = ‘PERSONNEL’);
Output:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E01 KOUSHIK GHOSH 5000 D01 SYSTEM ANALYST 10-MAR-93
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96
E08 GOUTAM DEY 5000 D01 PROGRAMMER 23-OCT-97
E09 PINAKI BOSE 5500 D02 PROGRAMMER 26-AUG-94

Query: Find the employees getting same salary as the salary of any employee of the
PERSONNEL department.

SELECT * FROM emp


WHERE salary=ANY(SELECT salary FROM emp, dept
WHERE emp.dno = dept.dno AND dname='PERSONNEL');
OR
SELECT * FROM emp
WHERE salary=ANY(SELECT salary FROM emp
WHERE dno = (SELECT dno FROM dept WHERE dname='PERSONNEL'))

Compiled By Manas Ghosh


14
Output:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E03 HARI NANDAN TUNGA 4000 D02 PROGRAMMER 01-JUL-95
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96

Query: Find the employees who do not work in any project.

Look at the ASSIGN table. The employee with codes ‘E04’ and ‘E05’ are absent in the ASSIGN
table. Obviously their records will be the output. But the following query will give all the rows of
the EMP table.

SELECT * FROM emp


WHERE ecode <>ANY(SELECT DISTINCT ecode FROM assign);
Output:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E01 KOUSHIK GHOSH 5000 D01 SYSTEM ANALYST 10-MAR-93
E02 JAYANTA DUTTA 3500 D01 PROGRAMMER 15-JAN-94
E03 HARI NANDAN TUNGA 4000 D02 PROGRAMMER 01-JUL-95
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96
E07 RANI BOSE 3000 D01 PROJECT ASSISTANT 17-JUN-97
E08 GOUTAM DEY 5000 D01 PROGRAMMER 23-OCT-97
E09 PINAKI BOSE 5500 D02 PROGRAMMER 26-AUG-94

Because <> ANY (1, 2, 3) means NOT 1 OR NOT 2 OR NOT 3 whereas NOT IN (1, 2, 3) implies
NOT 1 AND NOT 2 AND NOT 3. Therefore the query should be

SELECT * FROM emp


WHERE ecode NOT IN(SELECT DISTINCT ecode FROM assign);
Output:
ECODE ENAME SALARY DNO DESG DT_JN
---------- -------------------- ------------ ------- ------- ---------
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95

Comparison of ALL & ANY

Whenever a legal subquery fails to produce output, ALL is automatically TRUE but ANY is
automatically FALSE.
Illustration:
Look at the DEPT table there is no department at BANGALORE.

Compiled By Manas Ghosh


15
Query: Find the employees getting salary larger than the minimum salary of the
department located at BANGALORE.

The following query would result no output.

SELECT * FROM emp


WHERE salary>ANY(SELECT salary FROM emp, dept
WHERE emp.dno = dept.dno AND city= 'BANGALORE');
Output:
no rows selected

But the following query would produce entire emp table.

SELECT * FROM emp


WHERE salary>ALL(SELECT salary FROM emp, dept
WHERE emp.dno = dept.dno AND city= 'BANGALORE')
Output:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E01 KOUSHIK GHOSH 5000 D01 SYSTEM ANALYST 10-MAR-93
E02 JAYANTA DUTTA 3500 D01 PROGRAMMER 15-JAN-94
E03 HARI NANDAN TUNGA 4000 D02 PROGRAMMER 01-JUL-95
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96
E07 RANI BOSE 3000 D01 PROJECT ASSISTANT 17-JUN-97
E08 GOUTAM DEY 5000 D01 PROGRAMMER 23-OCT-97
E09 PINAKI BOSE 5500 D02 PROGRAMMER 26-AUG-94

 Subqueries that are an existence test (introduced with EXISTS)

The keywords EXISTS and NOT EXISTS are designed for use only with subqueries. They are
equivalent to the existential quantifier (and its negated form) that is seen in tuple calculus. Both
versions return either true or false only. EXISTS keyword is a WHERE clause tests for existence
or non existence of data that meets the criteria of the subquery. Existence or non existence
implies presence or absence of the “empty set” of rows. If the subquery returns at least one row,
the subquery evaluates TRUE. The EXISTS operator is used with correlated subqueries. It has
the following general format-
SELECT <select_list>
WHERE [NOT] EXISTS (subquery);

It should be noted that Any valid EXISTS subquery must contain an outer reference (the
subquery where clause references a column in the outer query). It must be a correlated subquery.

Rules with EXISTS:


• EXISTS is not preceded by a column name, constant or other expression.

Compiled By Manas Ghosh


16
• The SELECT list of the subquery introduced by EXISTS almost always contains an
asterisk (*) OR ‘X’. Because we are testing for existence of rows that meets the subquery
conditions.

Query: Find the names of the employees who work in the RESEARCH department.
SELECT DISTINCT ename FROM emp
WHERE EXISTS
(SELECT * FROM dept
WHERE dept.dno = emp.dno AND dname = ‘RESEARCH’);
Output:
ENAME
------------------------------
HARI NANDAN TUNGA
JISHNU BANERJEE
PINAKI BOSE
Query: Which designations are common to more than one department?
SELECT DISTINCT desg FROM emp A
WHERE EXISTS( SELECT * FROM emp B
WHERE A.desg=B.desg
AND A.dno<>B.dno);

Output:
DESG
----------------------------
PROGRAMMER

Query: Retrieve employee details who are not assigned in any project.

SELECT * FROM emp


WHERE NOT EXISTS (SELECT * FROM assign WHERE emp.ecode=assign.ecode);
OR
SELECT * FROM emp
WHERE NOT EXISTS (SELECT ‘X’ FROM assign WHERE emp.ecode=assign.ecode);
Output:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95
Query: show all employees for whom there does not exist an employee who is paid less. In
other words, the highest paid employee.
It can be easily written using simple subquery as follows –
SELECT *
FROM emp
WHERE salary=(SELECT MAX(salary) from emp);

Compiled By Manas Ghosh


17
But using NOT EXISTS it can be written as follows -

SELECT *
FROM emp
WHERE NOT EXISTS
(SELECT *
FROM emp e2
WHERE e2.salary > emp.salary);
Output:

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96

SCALAR SUBQUERIES

A Scalar Subquery can appear as a scalar value in the select list and where predicate of another
query.
For scalar subquery,
• the subquery must reference just one column in the select list.
• it must also retrieve no more than one row. If the subquery retrieves more than one row, it
generates a run-time error and aborts query execution.

Query: List the code, name of the employees along with the corresponding city of the
department in which they are working

SELECT ecode, ename,(SELECT city FROM dept


WHERE emp.dno=dept.dno) " CITY OF DEPT"
FROM emp;
Output:

ECODE ENAME CITY OF DEPT


----- ------------------------------ ------------------
E01 KOUSHIK GHOSH KOLKATA
E02 JAYANTA DUTTA KOLKATA
E03 HARI NANDAN TUNGA CHENNAI
E04 JAYANTA GANGULY KOLKATA
E05 RAJIB HALDER KOLKATA
E06 JISHNU BANERJEE CHENNAI
E07 RANI BOSE KOLKATA
E08 GOUTAM DEY KOLKATA
E09 PINAKI BOSE CHENNAI

9 rows selected.

When the subquery returns no rows, a database null is used as the result of the subquery.
e.g.
Query: Find the employees of the department located at BANGALORE.

Compiled By Manas Ghosh


18
SELECT ecode, ename,(SELECT city FROM dept
WHERE emp.dno=dept.dno AND city = 'BANGALORE') " CITY OF DEPT"
FROM emp;
Output:

ECODE ENAME CITY OF DEPT


----- ------------------------------ -------------------------
E01 KOUSHIK GHOSH
E02 JAYANTA DUTTA
E03 HARI NANDAN TUNGA
E04 JAYANTA GANGULY
E05 RAJIB HALDER
E06 JISHNU BANERJEE
E07 RANI BOSE
E08 GOUTAM DEY
E09 PINAKI BOSE

9 rows selected.

Query: List the employees working in the personnel department

SELECT * FROM emp WHERE ’PERSONNEL’= (SELECT dname FROM dept WHERE
emp.dno=dept.dno);
Output:
ECODE ENAME SALARY DNO DESG DT_JN
---------- -------------------- ------------ ------- ------- ---------
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95

If the scalar subquery returns multiple rows then oracle generates an error. For example, to list
the code, name of the employees along with the corresponding project id which they are
assigned.

SELECT ecode, ename,


(SELECT pid FROM assign WHERE emp.ecode=assign.ecode)
"PROJECT ID" FROM emp;
Output:

(SELECT pid FROM assign WHERE emp.ecode=assign.ecode) "PROJECT


ID"
*
ERROR at line 2:
ORA-01427: single-row subquery returns more than one row
Here multiple PIDs results from the scalar query. Therefore, the whole query fails.

TABLE SUBQUERIES

Table Subqueries are queries used in the FROM clause, replacing a table name. Basically, the
result set of the Table Subquery acts like a base table in the FROM list. Table Subqueries can

Compiled By Manas Ghosh


19
have a correlation name in the FROM list. It is also known as inline view. They can also be in
outer joins.

Query: List the details of the employees along with the corresponding location (city) of the
RESEARCH department.

The following two queries produce the same result:

SELECT E.*,city
FROM emp E, dept D WHERE E.dno=D.dno AND dname='RESEARCH';

SELECT E.*,city
FROM emp E, (SELECT dno, city From dept WHERE dname='RESEARCH') D
WHERE E.dno=D.dno;
Output:

ECODE ENAME SALARY DNO DESG DT_JN CITY


-------- ---------------------- --------- -------- ----------------- ---------- --------
E03 HARI NANDAN TUNGA 4000 D02 PROGRAMMER 01-JUL-95 CHENNAI
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96 CHENNAI
E09 PINAKI BOSE 5500 D02 PROGRAMMER 26-AUG-94 CHENNAI

Query: retrieve only the 5th row from EMP table?

SELECT * FROM (SELECT ecode, ename, salary, ROWNUM RN FROM emp WHERE
ROWNUM < 6) WHERE RN = 5;
Output:
ECODE ENAME SALARY RN
----- ------------------------------ ---------- ----------
E05 RAJIB HALDER 4000 5

Query: Retrieve only rows 5 to 8 from EMP table.


SELECT * FROM (SELECT ecode,ename,ROWNUM RN FROM emp WHERE ROWNUM<9)
WHERE RN BETWEEN 5 AND 8 ;
Output:
ECODE ENAME RN
----- ------------------------------ ----------
E05 RAJIB HALDER 5
E06 JISHNU BANERJEE 6
E07 RANI BOSE 7
E08 GOUTAM DEY 8

It is to be noted that the 9 is just one greater than the maximum row of the required rows (means
x= 5, y=8, so the inner values is y+1).

Query: Retrieve only rows 5 to 8 from EMP table and also display the department name
corresponding to each department number of the selected employees.

Compiled By Manas Ghosh


20
SELECT *
FROM (SELECT A.*, ROWNUM rnum
FROM (SELECT ecode, ename, dname
FROM emp e, dept d WHERE e.dno = d.dno
ORDER BY e.ecode) A
WHERE ROWNUM <= 8) WHERE rnum >= 5;
Output:
ECODE ENAME DNAME RNUM
----- ------------------------------ ------------------------------ ----------
E05 RAJIB HALDER PERSONNEL 5
E06 JISHNU BANERJEE RESEARCH 6
E07 RANI BOSE PROJECT 7
E08 GOUTAM DEY PROJECT 8

❑ Subqueries and Joins


Sometimes it is possible to use a join to get the same results as a subquery. Usually this is
possible when the return value(s) of the inner query are foreign keys that are related to tables in
the FROM clause of the outer query. An example follows:

SELECT ecode, ename, salary


FROM emp WHERE dno =(SELECT dno FROM dept WHERE dname=’PROJECT’);
Output:

ECODE ENAME SALARY


----- ------------------------------ ----------
E01 KOUSHIK GHOSH 5000
E02 JAYANTA DUTTA 3500
E07 RANI BOSE 3000
E08 GOUTAM DEY 5000
The above subquery can be transformed into the following two table equijoin which will give the
same output as above.
SELECT ecode, ename, salary
FROM emp, dept WHERE emp.dno=dept.dno AND dname=’PROJECT’;
Now the question is – which is best join or subquery? There is no concrete answer of this
question but it can be stated that Subqueries are used when it is needed to compare aggregate to
other values whereas join is mainly used it is needed to display results from multiple tables.

❑ Creating Table Using Sub query


A table can be created from an existing table. The syntax is

CREATE TABLE <table name> (<column name>, ...) AS sub-query ;


It matches the number of specified columns to the number of subquery columns. If no column
name is specified, then subquery column names and default values will be used in creating the
table.

For example,

Compiled By Manas Ghosh


21
CREATE TABLE dept02
AS
SELECT ecode, ename, salary FROM emp WHERE dno=’D02’;
Output:
Table created.
The above example creates a table named DEPT02 with three columns ECODE, NAME, and
SALARY (as default from the subquery results) and inserts into this table the rows retrieved from
the subquery.
The ‘dept02’ table contains the records of the employees working in the department ‘D02’.
SELECT * FROM dept02;
Output:

ECODE ENAME SALARY


----- ------------------------------ ----------
E03 HARI NANDAN TUNGA 4000
E06 JISHNU BANERJEE 6500
E09 PINAKI BOSE 5500

The ‘dept02’ table has three columns ecode, ename and salary. The type and size of these
columns are same as the ‘emp’ table.

DESC dept02

Output:

Name Null? Type


----------------------------------------- -------- -------------
ECODE NOT NULL VARCHAR2(5)
ENAME VARCHAR2(30)
SALARY NUMBER(7,2)

To create an empty copy of an existing table, the following SQL statement has to be entered.

CREATE TABLE new_table AS


SELECT { * |column_list} FROM old_table
WHERE 1 = 2;
For example,
CREATE TABLE empl
AS
SELECT * FROM emp WHERE 1=2;
Here only structure will be copied. Rows will not be copied. It is evident from the following SQL
statements.
DESC empl;
Output:
Name Null? Type
----------------------------------------- -------- -------------
ECODE NOT NULL VARCHAR2(5)
ENAME VARCHAR2(30)
SALARY NUMBER(7,2)

Compiled By Manas Ghosh


22
DNO VARCHAR2(5)
DESG VARCHAR2(30)
DT_JN DATE

SELECT * FROM empl;


Output:
no rows selected.

Update Using Subquery

Subquery can also be used with UPDATE statement.

Query: Increase the salary of the employees working in the RESEARCH department by 5%.

UPDATE emp
SET salary=salary*1.05
WHERE dno=(SELECT dno FROM dept WHERE dname=’RESEARCH’);
Output:
3 rows updated.

SELECT * FROM emp;


Output:
ECODE ENAME SALARY DNO DESG DT_JN
---------- -------------------- ------------ ------- ------- ---------
E01 KOUSHIK GHOSH 5000 D01 SYSTEM ANALYST 10-MAR-93
E02 JAYANTA DUTTA 3500 D01 PROGRAMMER 15-JAN-94
E03 HARI NANDAN TUNGA 4200 D02 PROGRAMMER 01-JUL-95
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95
E06 JISHNU BANERJEE 6825 D02 SYSTEM MANAGER 19-SEP-96
E07 RANI BOSE 3000 D01 PROJECT ASSISTANT 17-JUN-97
E08 GOUTAM DEY 5000 D01 PROGRAMMER 23-OCT-97
E09 PINAKI BOSE 5775 D02 PROGRAMMER 26-AUG-94

9 rows selected.

Updated rows are shown with bold font.

Subquery can be used in SET clause of the UPDATE statement. The syntax is given below.

UPDATE <table_name >


SET (<column_name1>,<column_name2>........)=(subquery)
WHERE <condition>;

Query: Increase the salary of the employees of the dept ‘D03’ by 2% of the highest salary
drawn by the employee of ‘D01’ department;

Compiled By Manas Ghosh


23
UPDATE emp
SET salary = salary + 0.02*(SELECT MAX(salary)
FROM emp WHERE dno=’D01’) WHERE dno=’D03’;

Output:
2 rows updated.

SELECT * FROM emp;


Output:
ECODE ENAME SALARY DNO DESG DT_JN
---------- -------------------- ------------ ------- ------- ---------
E01 KOUSHIK GHOSH 5000 D01 SYSTEM ANALYST 10-MAR-93
E02 JAYANTA DUTTA 3500 D01 PROGRAMMER 15-JAN-94
E03 HARI NANDAN TUNGA 4200 D02 PROGRAMMER 01-JUL-95
E04 JAYANTA GANGULY 6100 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4100 D03 CLERK 07-OCT-95
E06 JISHNU BANERJEE 6825 D02 SYSTEM MANAGER 19-SEP-96
E07 RANI BOSE 3000 D01 PROJECT ASSISTANT 17-JUN-97
E08 GOUTAM DEY 5000 D01 PROGRAMMER 23-OCT-97
E09 PINAKI BOSE 5775 D02 PROGRAMMER 26-AUG-94

9 rows selected.

Updated rows are shown with bold font.

It should be noted that the subquery should return a single value. Otherwise an error message
will be displayed. If the query is written in which multiple values are returned from the subquery,
then the UPDATE command fails. Notice the following query.

UPDATE emp
SET salary = salary + 0.02*(SELECT salary FROM emp
WHERE dno=’D01’) WHERE dno=’D03’;
Output:

SET salary = salary + 0.02*(SELECT salary FROM emp WHERE dno='D01')


*
ERROR at line 2:
ORA-01427: single-row subquery returns more than one row.

Correlated subquery can be applied with UPDATE command. For example,

Query: Increase the salary of all employees by 2% of the highest salary drawn by the
employee of their respective department;

UPDATE emp A
SET salary = salary + 0.02*(SELECT MAX(salary) FROM emp B WHERE
A.dno=B.dno);

Compiled By Manas Ghosh


24
Output:
9 rows updated.
Let’s see the result of the updating.
SELECT ecode, ename, dno, salary FROM emp ORDER BY dno;
Output:

ECODE ENAME DNO SALARY


----- ------------------------------ ----- ----------
E01 KOUSHIK GHOSH D01 5100
E02 JAYANTA DUTTA D01 3600
E08 GOUTAM DEY D01 5100
E07 RANI BOSE D01 3100
E03 HARI NANDAN TUNGA D02 4336.5
E09 PINAKI BOSE D02 5911.5
E06 JISHNU BANERJEE D02 6961.5
E04 JAYANTA GANGULY D03 6222
E05 RAJIB HALDER D03 4222

9 rows selected.

Multiple columns can be specified with SET clause along with subquery. The subquery should
return single value from each multiple columns.

Query: Transfer the employee with code ‘E02’ to department ‘D02’ as one of the highest
paid salaried employee of that department.

UPDATE emp
SET (dno,salary) = (SELECT dno,MAX(salary) FROM emp
WHERE dno =’D02’ GROUP BY dno) WHERE ecode = ‘E02’;
Output:
1 row updated.

SELECT ecode, ename, dno, salary FROM emp ORDER BY dno;


Output:

ECODE ENAME DNO SALARY


----- ------------------------------ ----- ----------
E01 KOUSHIK GHOSH D01 5100
E07 RANI BOSE D01 3100
E08 GOUTAM DEY D01 5100
E02 JAYANTA DUTTA D02 6961.5
E03 HARI NANDAN TUNGA D02 4336.5
E09 PINAKI BOSE D02 5911.5
E06 JISHNU BANERJEE D02 6961.5
E04 JAYANTA GANGULY D03 6222
E05 RAJIB HALDER D03 4222

10 rows selected.

Compiled By Manas Ghosh


25
Deleting rows Using Subquery

Like UPDATE command, subquery can be used with DELETE command also.
Consider the EMP table with original data.

ECODE ENAME SALARY DNO DESG DT_JN


---------- -------------------- ------------ ------- ------- ---------
E01 KOUSHIK GHOSH 5000 D01 SYSTEM ANALYST 10-MAR-93
E02 JAYANTA DUTTA 3500 D01 PROGRAMMER 15-JAN-94
E03 HARI NANDAN TUNGA 4000 D02 PROGRAMMER 01-JUL-95
E04 JAYANTA GANGULY 6000 D03 ACCOUNTANT 12-SEP-96
E05 RAJIB HALDER 4000 D03 CLERK 07-OCT-95
E06 JISHNU BANERJEE 6500 D02 SYSTEM MANAGER 19-SEP-96
E07 RANI BOSE 3000 D01 PROJECT ASSISTANT 17-JUN-97
E08 GOUTAM DEY 5000 D01 PROGRAMMER 23-OCT-97
E09 PINAKI BOSE 5500 D02 PROGRAMMER 26-AUG-94

Query: Delete the records of the employees of the RESEARCH department.

DELETE FROM emp


WHERE dno=(SELECT dno FROM dept WHERE dname=’RESEARCH’);
Output:
3 rows deleted.

Let’s see the result of deletion.

SELECT ecode, ename, dno, salary FROM emp ORDER BY dno;


Output:

ECODE ENAME DNO SALARY


----- ------------------------------ ----- ----------
E01 KOUSHIK GHOSH D01 5000
E02 JAYANTA DUTTA D01 3500
E07 RANI BOSE D01 3000
E08 GOUTAM DEY D01 5000
E04 JAYANTA GANGULY D03 6000
E05 RAJIB HALDER D03 4000

6 rows selected.

Now the following query is applied.

Query - Delete all employees who make more than the average salary:

DELETE from emp


WHERE salary > (SELECT avg(salary) FROM emp)
Output:
3 rows deleted.

Compiled By Manas Ghosh


26
SELECT ecode, ename, dno, salary FROM emp ORDER BY dno;
Output:

ECODE ENAME DNO SALARY


----- ------------------------------ ----- ----------
E02 JAYANTA DUTTA D01 3500
E07 RANI BOSE D01 3000
E05 RAJIB HALDER D03 4000

It is to be noted that the average would change while deletion of each row was occurring.
But this was not occurred as SQL do the following:
1. Evaluate the where clause for each tuple of the relation being deleted from. No tuples are
deleted in this step, but the result of evaluating the where clause is stored for each tuple.
2. After the where clause is evaluated for each tuple, all tuples that satisfied the WHERE
clause are deleted.
That is, SQL marks tuples for deletion first, and then deletes all tuples. This guarantees that the
where clause is always evaluated over the value of the relation before any deletions occur.

Compiled By Manas Ghosh


27

You might also like