Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 1
7. Nested Subqueries
SQL provides a mechanism for nesting subqueries. A subquery is a select-fromwhere expression that is
nested within another query. A common use of Subqueries is to perform tests for set membership, make set
comparisons, and determine set cardinality.
Set Membership
SQL draws on the relational calculus for operations that allow testing tuples for membership in a relation.
The in connective tests for set membership, where the set is a collection of values produced by a select
clause. The not in connective tests for the absence of set membership.
Find all the courses taught in the both the Fall 2009 and Spring 2010 semesters.
SQL> select distinct course_id
2 from section
3 where semester = 'Fall' and year= 2009 and
4 course_id in (select course_id
5 from section
6 where semester = 'Spring' and year= 2010);
COURSE_I
--------
CS-101
SQL> select * from section;
COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME
-------- -------- ------ ---------- --------------- ------- ----
BIO-101 1 Summer 2009 Painter 514 B
BIO-301 1 Summer 2010 Painter 514 A
CS-101 1 Fall 2009 Packard 101 H
CS-101 1 Spring 2010 Packard 101 F
CS-190 1 Spring 2009 Taylor 3128 E
CS-190 2 Spring 2009 Taylor 3128 A
CS-315 1 Spring 2010 Watson 120 D
CS-319 1 Spring 2010 Watson 100 B
CS-319 2 Spring 2010 Taylor 3128 C
CS-347 1 Fall 2009 Taylor 3128 A
EE-181 1 Spring 2009 Taylor 3128 C
COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME
-------- -------- ------ ---------- --------------- ------- ----
FIN-201 1 Spring 2010 Packard 101 B
HIS-351 1 Spring 2010 Painter 514 C
Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 2
MU-199 1 Spring 2010 Packard 101 D
PHY-101 1 Fall 2009 Watson 100 A
15 rows selected.
Find all the courses taught in the Fall 2009 semester but not in the Spring 2010 semester
SQL> select * from section;
COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME
-------- -------- ------ ---------- --------------- ------- ----
BIO-101 1 Summer 2009 Painter 514 B
BIO-301 1 Summer 2010 Painter 514 A
CS-101 1 Fall 2009 Packard 101 H
CS-101 1 Spring 2010 Packard 101 F
CS-190 1 Spring 2009 Taylor 3128 E
CS-190 2 Spring 2009 Taylor 3128 A
CS-315 1 Spring 2010 Watson 120 D
CS-319 1 Spring 2010 Watson 100 B
CS-319 2 Spring 2010 Taylor 3128 C
CS-347 1 Fall 2009 Taylor 3128 A
EE-181 1 Spring 2009 Taylor 3128 C
COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME
-------- -------- ------ ---------- --------------- ------- ----
FIN-201 1 Spring 2010 Packard 101 B
HIS-351 1 Spring 2010 Painter 514 C
MU-199 1 Spring 2010 Packard 101 D
PHY-101 1 Fall 2009 Watson 100 A
15 rows selected.
SQL> select distinct course_id
2 from section
3 where semester = 'Fall' and year= 2009 and
4 course_id not in (select course_id
5 from section
6 where semester = 'Spring' and year= 2010);
COURSE_I
--------
CS-347
PHY-101
Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 3
Selects the names of instructors whose names are neither “Mozart” nor “Einstein”.
SQL> select distinct name
2 from instructor
3 where name not in ('Mozart', 'Einstein');
NAME
--------------------
Crick
El Said
Srinivasan
Gold
Brandt
Wu
Kim
Singh
Katz
Califieri
10 rows selected.
SQL> select * from instructor;
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
10101 Srinivasan Comp.Sci. 65000
12121 Wu Finance 90000
15151 Mozart Music 40000
22222 Einstein Physics 95000
32343 El Said History 60000
33456 Gold Physics 87000
45565 Katz Comp.Sci. 75000
58583 Califieri History 62000
76543 Singh Finance 80000
76766 Crick Biology 72000
83821 Brandt Comp.Sci. 92000
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
98345 Kim Elec.Eng. 80000
12 rows selected.
Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 4
Set Comparison
The phrase “greater than at least one” is represented in SQL by > some.
SQL also allows < some, <= some, >= some, = some, and<> some comparisons. As an exercise, verify
that = some is identical to in, whereas <> some is not the same as not in. The keyword any is synonymous
to some in SQL. Early versions of SQL allowed only any.
SQL also allows < all, <= all, >= all, = all, and <> all comparisons. The <> all is identical to not in.
Find the names of all instructors whose salary is greater than at least one instructor in the Biology
department.
SQL> select * from instructor;
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
10101 Srinivasan Comp.Sci. 65000
12121 Wu Finance 90000
15151 Mozart Music 40000
22222 Einstein Physics 95000
32343 El Said History 60000
33456 Gold Physics 87000
45565 Katz Comp.Sci. 75000
58583 Califieri History 62000
76543 Singh Finance 80000
76766 Crick Biology 72000
83821 Brandt Comp.Sci. 92000
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
98345 Kim Elec.Eng. 80000
12 rows selected.
SQL> select name
2 from instructor
3 where salary > some (select salary
4 from instructor
5 where dept_name = 'Biology');
NAME
--------------------
Wu
Einstein
Gold
Katz
Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 5
Singh
Brandt
Kim
7 rows selected.
SQL> select distinct T.name
2 from instructor T, instructor S
3 where T.salary > S.salary and S.dept_name = 'Biology';
NAME
--------------------
Einstein
Gold
Brandt
Wu
Kim
Singh
Katz
7 rows selected.
Find the names of all instructors that have a salary value greater than that of each instructor in the
Biology department.
SQL> select * from instructor;
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
10101 Srinivasan Comp.Sci. 65000
12121 Wu Finance 90000
15151 Mozart Music 40000
22222 Einstein Physics 95000
32343 El Said History 60000
33456 Gold Physics 87000
45565 Katz Comp.Sci. 75000
58583 Califieri History 62000
76543 Singh Finance 80000
76766 Crick Biology 72000
83821 Brandt Comp.Sci. 92000
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
98345 Kim Elec.Eng. 80000
Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 6
12 rows selected.
SQL> select name
2 from instructor
3 where salary > all (select salary
4 from instructor
5 where dept_name = 'Biology');
NAME
--------------------
Wu
Einstein
Gold
Katz
Singh
Brandt
Kim
7 rows selected.
Find the departments that have the highest average salary
SQL> select * from instructor;
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
10101 Srinivasan Comp.Sci. 65000
12121 Wu Finance 90000
15151 Mozart Music 40000
22222 Einstein Physics 95000
32343 El Said History 60000
33456 Gold Physics 87000
45565 Katz Comp.Sci. 75000
58583 Califieri History 62000
76543 Singh Finance 80000
76766 Crick Biology 72000
83821 Brandt Comp.Sci. 92000
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
98345 Kim Elec.Eng. 80000
12 rows selected.
Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 7
SQL> select dept_name
2 from instructor
3 group by dept_name
4 having avg (salary) >= all (select avg (salary)
5 from instructor
6 group by dept_name);
DEPT_NAME
--------------------
Physics
Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 8
Test for Empty Relations
SQL includes a feature for testing whether a subquery has any tuples in its result. The exists construct
returns the value true if the argument subquery is nonempty
We can test for the nonexistence of tuples in a subquery by using the not exists construct. We can use the
not exists construct to simulate the set containment (that is, superset) operation: We can write “relation A
contains relation B” as “not exists (B except A).” (Although it is not part of the SQL-92 and SQL:1999
standards, the contains operator was present in some early relational systems.)
Find all courses taught in both the Fall 2009 semester and in the Spring 2010 semester.
SQL> select * from section;
COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME
-------- -------- ------ ---------- --------------- ------- ----
BIO-101 1 Summer 2009 Painter 514 B
BIO-301 1 Summer 2010 Painter 514 A
CS-101 1 Fall 2009 Packard 101 H
CS-101 1 Spring 2010 Packard 101 F
CS-190 1 Spring 2009 Taylor 3128 E
CS-190 2 Spring 2009 Taylor 3128 A
CS-315 1 Spring 2010 Watson 120 D
CS-319 1 Spring 2010 Watson 100 B
CS-319 2 Spring 2010 Taylor 3128 C
CS-347 1 Fall 2009 Taylor 3128 A
EE-181 1 Spring 2009 Taylor 3128 C
COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME
-------- -------- ------ ---------- --------------- ------- ----
FIN-201 1 Spring 2010 Packard 101 B
HIS-351 1 Spring 2010 Painter 514 C
MU-199 1 Spring 2010 Packard 101 D
PHY-101 1 Fall 2009 Watson 100 A
15 rows selected.
SQL> select course_id
2 from section S
3 where semester = 'Fall' and year= 2009 and
4 exists (select *
5 from section T
6 where semester = 'Spring' and year= 2010 and
7 S.course_id= T.course_id);
COURSE_I
Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 9
--------
CS-101
References:
 Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill
International Edition) sixth edition.
 Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill
International Edition) fifth edition.
 http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/
 http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/
 http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/

7. Nested Subqueries.pdf

  • 1.
    Mrs. Sunita MDol, CSE Department WIT, Solapur Page 1 7. Nested Subqueries SQL provides a mechanism for nesting subqueries. A subquery is a select-fromwhere expression that is nested within another query. A common use of Subqueries is to perform tests for set membership, make set comparisons, and determine set cardinality. Set Membership SQL draws on the relational calculus for operations that allow testing tuples for membership in a relation. The in connective tests for set membership, where the set is a collection of values produced by a select clause. The not in connective tests for the absence of set membership. Find all the courses taught in the both the Fall 2009 and Spring 2010 semesters. SQL> select distinct course_id 2 from section 3 where semester = 'Fall' and year= 2009 and 4 course_id in (select course_id 5 from section 6 where semester = 'Spring' and year= 2010); COURSE_I -------- CS-101 SQL> select * from section; COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME -------- -------- ------ ---------- --------------- ------- ---- BIO-101 1 Summer 2009 Painter 514 B BIO-301 1 Summer 2010 Painter 514 A CS-101 1 Fall 2009 Packard 101 H CS-101 1 Spring 2010 Packard 101 F CS-190 1 Spring 2009 Taylor 3128 E CS-190 2 Spring 2009 Taylor 3128 A CS-315 1 Spring 2010 Watson 120 D CS-319 1 Spring 2010 Watson 100 B CS-319 2 Spring 2010 Taylor 3128 C CS-347 1 Fall 2009 Taylor 3128 A EE-181 1 Spring 2009 Taylor 3128 C COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME -------- -------- ------ ---------- --------------- ------- ---- FIN-201 1 Spring 2010 Packard 101 B HIS-351 1 Spring 2010 Painter 514 C
  • 2.
    Mrs. Sunita MDol, CSE Department WIT, Solapur Page 2 MU-199 1 Spring 2010 Packard 101 D PHY-101 1 Fall 2009 Watson 100 A 15 rows selected. Find all the courses taught in the Fall 2009 semester but not in the Spring 2010 semester SQL> select * from section; COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME -------- -------- ------ ---------- --------------- ------- ---- BIO-101 1 Summer 2009 Painter 514 B BIO-301 1 Summer 2010 Painter 514 A CS-101 1 Fall 2009 Packard 101 H CS-101 1 Spring 2010 Packard 101 F CS-190 1 Spring 2009 Taylor 3128 E CS-190 2 Spring 2009 Taylor 3128 A CS-315 1 Spring 2010 Watson 120 D CS-319 1 Spring 2010 Watson 100 B CS-319 2 Spring 2010 Taylor 3128 C CS-347 1 Fall 2009 Taylor 3128 A EE-181 1 Spring 2009 Taylor 3128 C COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME -------- -------- ------ ---------- --------------- ------- ---- FIN-201 1 Spring 2010 Packard 101 B HIS-351 1 Spring 2010 Painter 514 C MU-199 1 Spring 2010 Packard 101 D PHY-101 1 Fall 2009 Watson 100 A 15 rows selected. SQL> select distinct course_id 2 from section 3 where semester = 'Fall' and year= 2009 and 4 course_id not in (select course_id 5 from section 6 where semester = 'Spring' and year= 2010); COURSE_I -------- CS-347 PHY-101
  • 3.
    Mrs. Sunita MDol, CSE Department WIT, Solapur Page 3 Selects the names of instructors whose names are neither “Mozart” nor “Einstein”. SQL> select distinct name 2 from instructor 3 where name not in ('Mozart', 'Einstein'); NAME -------------------- Crick El Said Srinivasan Gold Brandt Wu Kim Singh Katz Califieri 10 rows selected. SQL> select * from instructor; ID NAME DEPT_NAME SALARY ----- -------------------- -------------------- ---------- 10101 Srinivasan Comp.Sci. 65000 12121 Wu Finance 90000 15151 Mozart Music 40000 22222 Einstein Physics 95000 32343 El Said History 60000 33456 Gold Physics 87000 45565 Katz Comp.Sci. 75000 58583 Califieri History 62000 76543 Singh Finance 80000 76766 Crick Biology 72000 83821 Brandt Comp.Sci. 92000 ID NAME DEPT_NAME SALARY ----- -------------------- -------------------- ---------- 98345 Kim Elec.Eng. 80000 12 rows selected.
  • 4.
    Mrs. Sunita MDol, CSE Department WIT, Solapur Page 4 Set Comparison The phrase “greater than at least one” is represented in SQL by > some. SQL also allows < some, <= some, >= some, = some, and<> some comparisons. As an exercise, verify that = some is identical to in, whereas <> some is not the same as not in. The keyword any is synonymous to some in SQL. Early versions of SQL allowed only any. SQL also allows < all, <= all, >= all, = all, and <> all comparisons. The <> all is identical to not in. Find the names of all instructors whose salary is greater than at least one instructor in the Biology department. SQL> select * from instructor; ID NAME DEPT_NAME SALARY ----- -------------------- -------------------- ---------- 10101 Srinivasan Comp.Sci. 65000 12121 Wu Finance 90000 15151 Mozart Music 40000 22222 Einstein Physics 95000 32343 El Said History 60000 33456 Gold Physics 87000 45565 Katz Comp.Sci. 75000 58583 Califieri History 62000 76543 Singh Finance 80000 76766 Crick Biology 72000 83821 Brandt Comp.Sci. 92000 ID NAME DEPT_NAME SALARY ----- -------------------- -------------------- ---------- 98345 Kim Elec.Eng. 80000 12 rows selected. SQL> select name 2 from instructor 3 where salary > some (select salary 4 from instructor 5 where dept_name = 'Biology'); NAME -------------------- Wu Einstein Gold Katz
  • 5.
    Mrs. Sunita MDol, CSE Department WIT, Solapur Page 5 Singh Brandt Kim 7 rows selected. SQL> select distinct T.name 2 from instructor T, instructor S 3 where T.salary > S.salary and S.dept_name = 'Biology'; NAME -------------------- Einstein Gold Brandt Wu Kim Singh Katz 7 rows selected. Find the names of all instructors that have a salary value greater than that of each instructor in the Biology department. SQL> select * from instructor; ID NAME DEPT_NAME SALARY ----- -------------------- -------------------- ---------- 10101 Srinivasan Comp.Sci. 65000 12121 Wu Finance 90000 15151 Mozart Music 40000 22222 Einstein Physics 95000 32343 El Said History 60000 33456 Gold Physics 87000 45565 Katz Comp.Sci. 75000 58583 Califieri History 62000 76543 Singh Finance 80000 76766 Crick Biology 72000 83821 Brandt Comp.Sci. 92000 ID NAME DEPT_NAME SALARY ----- -------------------- -------------------- ---------- 98345 Kim Elec.Eng. 80000
  • 6.
    Mrs. Sunita MDol, CSE Department WIT, Solapur Page 6 12 rows selected. SQL> select name 2 from instructor 3 where salary > all (select salary 4 from instructor 5 where dept_name = 'Biology'); NAME -------------------- Wu Einstein Gold Katz Singh Brandt Kim 7 rows selected. Find the departments that have the highest average salary SQL> select * from instructor; ID NAME DEPT_NAME SALARY ----- -------------------- -------------------- ---------- 10101 Srinivasan Comp.Sci. 65000 12121 Wu Finance 90000 15151 Mozart Music 40000 22222 Einstein Physics 95000 32343 El Said History 60000 33456 Gold Physics 87000 45565 Katz Comp.Sci. 75000 58583 Califieri History 62000 76543 Singh Finance 80000 76766 Crick Biology 72000 83821 Brandt Comp.Sci. 92000 ID NAME DEPT_NAME SALARY ----- -------------------- -------------------- ---------- 98345 Kim Elec.Eng. 80000 12 rows selected.
  • 7.
    Mrs. Sunita MDol, CSE Department WIT, Solapur Page 7 SQL> select dept_name 2 from instructor 3 group by dept_name 4 having avg (salary) >= all (select avg (salary) 5 from instructor 6 group by dept_name); DEPT_NAME -------------------- Physics
  • 8.
    Mrs. Sunita MDol, CSE Department WIT, Solapur Page 8 Test for Empty Relations SQL includes a feature for testing whether a subquery has any tuples in its result. The exists construct returns the value true if the argument subquery is nonempty We can test for the nonexistence of tuples in a subquery by using the not exists construct. We can use the not exists construct to simulate the set containment (that is, superset) operation: We can write “relation A contains relation B” as “not exists (B except A).” (Although it is not part of the SQL-92 and SQL:1999 standards, the contains operator was present in some early relational systems.) Find all courses taught in both the Fall 2009 semester and in the Spring 2010 semester. SQL> select * from section; COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME -------- -------- ------ ---------- --------------- ------- ---- BIO-101 1 Summer 2009 Painter 514 B BIO-301 1 Summer 2010 Painter 514 A CS-101 1 Fall 2009 Packard 101 H CS-101 1 Spring 2010 Packard 101 F CS-190 1 Spring 2009 Taylor 3128 E CS-190 2 Spring 2009 Taylor 3128 A CS-315 1 Spring 2010 Watson 120 D CS-319 1 Spring 2010 Watson 100 B CS-319 2 Spring 2010 Taylor 3128 C CS-347 1 Fall 2009 Taylor 3128 A EE-181 1 Spring 2009 Taylor 3128 C COURSE_I SEC_ID SEMEST YEAR BUILDING ROOM_NU TIME -------- -------- ------ ---------- --------------- ------- ---- FIN-201 1 Spring 2010 Packard 101 B HIS-351 1 Spring 2010 Painter 514 C MU-199 1 Spring 2010 Packard 101 D PHY-101 1 Fall 2009 Watson 100 A 15 rows selected. SQL> select course_id 2 from section S 3 where semester = 'Fall' and year= 2009 and 4 exists (select * 5 from section T 6 where semester = 'Spring' and year= 2010 and 7 S.course_id= T.course_id); COURSE_I
  • 9.
    Mrs. Sunita MDol, CSE Department WIT, Solapur Page 9 -------- CS-101 References:  Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill International Edition) sixth edition.  Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill International Edition) fifth edition.  http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/  http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/  http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/