SlideShare a Scribd company logo
1 of 9
Download to read offline
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/

More Related Content

Similar to 7. Nested Subqueries.pdf

ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....Racharla Rohit Varma
 
EvolveExecutionPlans.pdf
EvolveExecutionPlans.pdfEvolveExecutionPlans.pdf
EvolveExecutionPlans.pdfPraveenPolu1
 
Dimensional performance benchmarking of SQL
Dimensional performance benchmarking of SQLDimensional performance benchmarking of SQL
Dimensional performance benchmarking of SQLBrendan Furey
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?Andrej Pashchenko
 
Database Management System
Database Management SystemDatabase Management System
Database Management SystemHitesh Mohapatra
 
Best sql plsql_material for B.TECH
Best sql plsql_material for B.TECH Best sql plsql_material for B.TECH
Best sql plsql_material for B.TECH AmIt Prasad
 
OpenWorld 2018 - Common Application Developer Disasters
OpenWorld 2018 - Common Application Developer DisastersOpenWorld 2018 - Common Application Developer Disasters
OpenWorld 2018 - Common Application Developer DisastersConnor McDonald
 
May Woo Bi Portfolio
May Woo Bi PortfolioMay Woo Bi Portfolio
May Woo Bi Portfoliomaywoo
 
Analysing Performance of Algorithmic SQL and PLSQL.pptx
Analysing Performance of Algorithmic SQL and PLSQL.pptxAnalysing Performance of Algorithmic SQL and PLSQL.pptx
Analysing Performance of Algorithmic SQL and PLSQL.pptxBrendan Furey
 
Date data type and Globalization in Oracle
Date data type and Globalization in OracleDate data type and Globalization in Oracle
Date data type and Globalization in OracleMasoud Haji Hassan Pour
 
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdfCC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdfozaixyzo
 

Similar to 7. Nested Subqueries.pdf (20)

ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
 
EvolveExecutionPlans.pdf
EvolveExecutionPlans.pdfEvolveExecutionPlans.pdf
EvolveExecutionPlans.pdf
 
Les01
Les01Les01
Les01
 
Dimensional performance benchmarking of SQL
Dimensional performance benchmarking of SQLDimensional performance benchmarking of SQL
Dimensional performance benchmarking of SQL
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?
 
Lecture02_IDB.pptx
Lecture02_IDB.pptxLecture02_IDB.pptx
Lecture02_IDB.pptx
 
Les01
Les01Les01
Les01
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
Oracle Advanced SQL
Oracle Advanced SQLOracle Advanced SQL
Oracle Advanced SQL
 
Best sql plsql_material for B.TECH
Best sql plsql_material for B.TECH Best sql plsql_material for B.TECH
Best sql plsql_material for B.TECH
 
OpenWorld 2018 - Common Application Developer Disasters
OpenWorld 2018 - Common Application Developer DisastersOpenWorld 2018 - Common Application Developer Disasters
OpenWorld 2018 - Common Application Developer Disasters
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
Les01 Writing Basic Sql Statements
Les01 Writing Basic Sql StatementsLes01 Writing Basic Sql Statements
Les01 Writing Basic Sql Statements
 
Database Security
Database SecurityDatabase Security
Database Security
 
Sql 3
Sql 3Sql 3
Sql 3
 
May Woo Bi Portfolio
May Woo Bi PortfolioMay Woo Bi Portfolio
May Woo Bi Portfolio
 
SQLQueries
SQLQueriesSQLQueries
SQLQueries
 
Analysing Performance of Algorithmic SQL and PLSQL.pptx
Analysing Performance of Algorithmic SQL and PLSQL.pptxAnalysing Performance of Algorithmic SQL and PLSQL.pptx
Analysing Performance of Algorithmic SQL and PLSQL.pptx
 
Date data type and Globalization in Oracle
Date data type and Globalization in OracleDate data type and Globalization in Oracle
Date data type and Globalization in Oracle
 
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdfCC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
 

More from Sunita Milind Dol (20)

9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
 
3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
 
Assignment12
Assignment12Assignment12
Assignment12
 
Assignment11
Assignment11Assignment11
Assignment11
 
Assignment10
Assignment10Assignment10
Assignment10
 
Assignment9
Assignment9Assignment9
Assignment9
 
Assignment8
Assignment8Assignment8
Assignment8
 
Assignment7
Assignment7Assignment7
Assignment7
 
Assignment6
Assignment6Assignment6
Assignment6
 
Assignment5
Assignment5Assignment5
Assignment5
 
Assignment4
Assignment4Assignment4
Assignment4
 
Assignment3
Assignment3Assignment3
Assignment3
 
Assignment2
Assignment2Assignment2
Assignment2
 
Assignment1
Assignment1Assignment1
Assignment1
 
Handout#12
Handout#12Handout#12
Handout#12
 
Handout#11
Handout#11Handout#11
Handout#11
 
Handout#10
Handout#10Handout#10
Handout#10
 
Handout#09
Handout#09Handout#09
Handout#09
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

7. Nested Subqueries.pdf

  • 1. 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
  • 2. 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
  • 3. 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.
  • 4. 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
  • 5. 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
  • 6. 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.
  • 7. 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
  • 8. 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
  • 9. 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/