34
SQL Subqueries T. M. Murali September 2, 2009 T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

SQL Subqueries

T. M. Murali

September 2, 2009

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 2: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Linear Notation for Relational Algebra

I Relational algebra expressions can become very long.I Use linear notation to store results of intemediate expressions.

1. A relation name and a parenthesised list of attributes for that relation.Use Answer as the conventional name for the final result.

2. The assignment symbol :=.3. Any expression in relational algebra on the right.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 3: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Example of Linear Notation

I Name pairs of students who live at the same address.

I Normal expression:

πS1.Name,S2.Name(σS1.Address = S2.Address(ρS1(Students)× ρS2(Students)))

I Linear notation:

Pairs(P1, N1, A1, P2, N2, A2) := ρS1(Students)× ρS2(Students)

Matched(P1, N1, A1, P2, N2, A2) :=σA1 = A2(Pairs(P1, N1, A1, P2, N2, A2))

Answer(Name1, Name2) := πN1,N2(Matched(P1, N1, A1, P2, N2, A2)).

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 4: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Example of Linear Notation

I Name pairs of students who live at the same address.

I Normal expression:

πS1.Name,S2.Name(σS1.Address = S2.Address(ρS1(Students)× ρS2(Students)))

I Linear notation:

Pairs(P1, N1, A1, P2, N2, A2) := ρS1(Students)× ρS2(Students)

Matched(P1, N1, A1, P2, N2, A2) :=σA1 = A2(Pairs(P1, N1, A1, P2, N2, A2))

Answer(Name1, Name2) := πN1,N2(Matched(P1, N1, A1, P2, N2, A2)).

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 5: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Interpreting Queries Involving Multiple Relations

SELECT A, B FROM R, S WHERE C;

I Nested loops:

for each tuple t1 in Rfor each tuple t2 in S

if the attributes in t1 and t2 satisfy Coutput the tuples involving attributes A and B.

I Conversion to relational algebra: πA,B(σC (R × S)).

1. Compute R × S .2. Apply the selection operator σC () to R × S .3. Project the resulting tuples to attributes A and B.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 6: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Interpreting Queries Involving Multiple Relations

SELECT A, B FROM R, S WHERE C;

I Nested loops:

for each tuple t1 in Rfor each tuple t2 in S

if the attributes in t1 and t2 satisfy Coutput the tuples involving attributes A and B.

I Conversion to relational algebra: πA,B(σC (R × S)).

1. Compute R × S .2. Apply the selection operator σC () to R × S .3. Project the resulting tuples to attributes A and B.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 7: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Interpreting Queries Involving Multiple Relations

SELECT A, B FROM R, S WHERE C;

I Nested loops:

for each tuple t1 in Rfor each tuple t2 in S

if the attributes in t1 and t2 satisfy Coutput the tuples involving attributes A and B.

I Conversion to relational algebra: πA,B(σC (R × S)).

1. Compute R × S .2. Apply the selection operator σC () to R × S .3. Project the resulting tuples to attributes A and B.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 8: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Motivation for Subqueries

I Find the name of the professor who teaches “CS 4604.”

SELECT NameFROM Professors, TeachWHERE (PID = ProfessorPID) AND (Number = ’4604’) AND(DeptName = ’CS’) ;

I Do we need to take the natural join of two big relations just to get arelation with one tuple?

I Can we rewrite the query without using a join?

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 9: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Motivation for Subqueries

I Find the name of the professor who teaches “CS 4604.”SELECT NameFROM Professors, TeachWHERE (PID = ProfessorPID) AND (Number = ’4604’) AND(DeptName = ’CS’) ;

I Do we need to take the natural join of two big relations just to get arelation with one tuple?

I Can we rewrite the query without using a join?

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 10: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Motivation for Subqueries

I Find the name of the professor who teaches “CS 4604.”SELECT NameFROM Professors, TeachWHERE (PID = ProfessorPID) AND (Number = ’4604’) AND(DeptName = ’CS’) ;

I Do we need to take the natural join of two big relations just to get arelation with one tuple?

I Can we rewrite the query without using a join?

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 11: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

SQL Subquery For Example

I Find the name of the professor who teaches “CS 4604.”

SELECT NameFROM ProfessorsWHERE PID =

(SELECT ProfessorPIDFROM TeachWHERE (Number = 4604) AND (DeptName = ’CS’));

I When using =, the subquery must return a single tuple.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 12: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

SQL Subquery For Example

I Find the name of the professor who teaches “CS 4604.”

SELECT NameFROM ProfessorsWHERE PID =

(SELECT ProfessorPIDFROM TeachWHERE (Number = 4604) AND (DeptName = ’CS’));

I When using =, the subquery must return a single tuple.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 13: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

SQL Subquery For Example

I Find the name of the professor who teaches “CS 4604.”

SELECT NameFROM ProfessorsWHERE PID =

(SELECT ProfessorPIDFROM TeachWHERE (Number = 4604) AND (DeptName = ’CS’));

I When using =, the subquery must return a single tuple.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 14: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Conditions Involving Relations

I SQL includes a number of operators that apply to a relation andproduce a boolean result.

I These operators are very useful to apply on results of sub-queries.

I Let R be a relation and t be a tuple with the same set of attributes.

I EXISTS R is true if and only if R contains at least one tuple.

I t IN R is true if and only if t equals a tuple in R.I t > ALL R is true if and only if R is unary (has one attribute) and t

is greater than every value in R.I Can use any of the other five comparison operators.I If we use <>, R need not be unary.

I t > ANY R (which is unary) is true if and only if t is greater than atleast one value in R.

I We can use NOT to negate EXISTS, ALL, and ANY.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 15: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Conditions Involving Relations

I SQL includes a number of operators that apply to a relation andproduce a boolean result.

I These operators are very useful to apply on results of sub-queries.

I Let R be a relation and t be a tuple with the same set of attributes.

I EXISTS R is true if and only if R contains at least one tuple.

I t IN R is true if and only if t equals a tuple in R.I t > ALL R is true if and only if R is unary (has one attribute) and t

is greater than every value in R.I Can use any of the other five comparison operators.I If we use <>, R need not be unary.

I t > ANY R (which is unary) is true if and only if t is greater than atleast one value in R.

I We can use NOT to negate EXISTS, ALL, and ANY.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 16: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Conditions Involving Relations

I SQL includes a number of operators that apply to a relation andproduce a boolean result.

I These operators are very useful to apply on results of sub-queries.

I Let R be a relation and t be a tuple with the same set of attributes.

I EXISTS R is true if and only if R contains at least one tuple.

I t IN R is true if and only if t equals a tuple in R.I t > ALL R is true if and only if R is unary (has one attribute) and t

is greater than every value in R.I Can use any of the other five comparison operators.I If we use <>, R need not be unary.

I t > ANY R (which is unary) is true if and only if t is greater than atleast one value in R.

I We can use NOT to negate EXISTS, ALL, and ANY.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 17: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Conditions Involving Relations

I SQL includes a number of operators that apply to a relation andproduce a boolean result.

I These operators are very useful to apply on results of sub-queries.

I Let R be a relation and t be a tuple with the same set of attributes.

I EXISTS R is true if and only if R contains at least one tuple.

I t IN R is true if and only if t equals a tuple in R.

I t > ALL R is true if and only if R is unary (has one attribute) and tis greater than every value in R.

I Can use any of the other five comparison operators.I If we use <>, R need not be unary.

I t > ANY R (which is unary) is true if and only if t is greater than atleast one value in R.

I We can use NOT to negate EXISTS, ALL, and ANY.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 18: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Conditions Involving Relations

I SQL includes a number of operators that apply to a relation andproduce a boolean result.

I These operators are very useful to apply on results of sub-queries.

I Let R be a relation and t be a tuple with the same set of attributes.

I EXISTS R is true if and only if R contains at least one tuple.

I t IN R is true if and only if t equals a tuple in R.I t > ALL R is true if and only if R is unary (has one attribute) and t

is greater than every value in R.

I Can use any of the other five comparison operators.I If we use <>, R need not be unary.

I t > ANY R (which is unary) is true if and only if t is greater than atleast one value in R.

I We can use NOT to negate EXISTS, ALL, and ANY.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 19: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Conditions Involving Relations

I SQL includes a number of operators that apply to a relation andproduce a boolean result.

I These operators are very useful to apply on results of sub-queries.

I Let R be a relation and t be a tuple with the same set of attributes.

I EXISTS R is true if and only if R contains at least one tuple.

I t IN R is true if and only if t equals a tuple in R.I t > ALL R is true if and only if R is unary (has one attribute) and t

is greater than every value in R.I Can use any of the other five comparison operators.I If we use <>, R need not be unary.

I t > ANY R (which is unary) is true if and only if t is greater than atleast one value in R.

I We can use NOT to negate EXISTS, ALL, and ANY.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 20: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Conditions Involving Relations

I SQL includes a number of operators that apply to a relation andproduce a boolean result.

I These operators are very useful to apply on results of sub-queries.

I Let R be a relation and t be a tuple with the same set of attributes.

I EXISTS R is true if and only if R contains at least one tuple.

I t IN R is true if and only if t equals a tuple in R.I t > ALL R is true if and only if R is unary (has one attribute) and t

is greater than every value in R.I Can use any of the other five comparison operators.I If we use <>, R need not be unary.

I t > ANY R (which is unary) is true if and only if t is greater than atleast one value in R.

I We can use NOT to negate EXISTS, ALL, and ANY.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 21: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Conditions Involving Relations

I SQL includes a number of operators that apply to a relation andproduce a boolean result.

I These operators are very useful to apply on results of sub-queries.

I Let R be a relation and t be a tuple with the same set of attributes.

I EXISTS R is true if and only if R contains at least one tuple.

I t IN R is true if and only if t equals a tuple in R.I t > ALL R is true if and only if R is unary (has one attribute) and t

is greater than every value in R.I Can use any of the other five comparison operators.I If we use <>, R need not be unary.

I t > ANY R (which is unary) is true if and only if t is greater than atleast one value in R.

I We can use NOT to negate EXISTS, ALL, and ANY.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 22: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Subqueries Using Conditions

I Find the departments of the courses taken by the student with name’Suri’.

SELECT DeptNameFROM TakeWHERE StudentPID IN

(SELECT PIDFROM StudentsWHERE (Name = ’Suri’));

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 23: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Subqueries Using Conditions

I Find the departments of the courses taken by the student with name’Suri’.

SELECT DeptNameFROM TakeWHERE StudentPID IN

(SELECT PIDFROM StudentsWHERE (Name = ’Suri’));

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 24: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Correlated Subqueries

I Find course names that have been used for two or more courses.

SELECT CourseNameFROM Courses AS FirstWHERE CourseName IN

(SELECT CourseNameFROM CoursesWHERE (Number <> First.Number)

AND (DeptName <> First.DeptName));

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 25: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Correlated Subqueries

I Find course names that have been used for two or more courses.

SELECT CourseNameFROM Courses AS FirstWHERE CourseName IN

(SELECT CourseNameFROM CoursesWHERE

(Number <> First.Number)AND (DeptName <> First.DeptName));

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 26: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Correlated Subqueries

I Find course names that have been used for two or more courses.

SELECT CourseNameFROM Courses AS FirstWHERE CourseName IN

(SELECT CourseNameFROM CoursesWHERE (Number <> First.Number)

AND (DeptName <> First.DeptName));

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 27: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Evaluating Correlated Subqueries

SELECT CourseNameFROM Courses AS FirstWHERE CourseName IN

(SELECT CourseNameFROM CoursesWHERE (Number <> First.Number)

AND (DeptName <> First.DeptName));

I Evaluate query by looping over tuples of First and for each tuple,evaluate the subquery.

I Scoping rules: an attribute in a subquery belongs to one of the tuplevariables in that subquery’s FROM clause, or to the immediatelysurrounding subquery, and so on.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 28: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Evaluating Correlated Subqueries

SELECT CourseNameFROM Courses AS FirstWHERE CourseName IN

(SELECT CourseNameFROM CoursesWHERE (Number <> First.Number)

AND (DeptName <> First.DeptName));

I Evaluate query by looping over tuples of First and for each tuple,evaluate the subquery.

I Scoping rules: an attribute in a subquery belongs to one of the tuplevariables in that subquery’s FROM clause, or to the immediatelysurrounding subquery, and so on.

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 29: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Subqueries in FROM clauses

I Can use a subquery as a relation in a FROM clause.

I We must give such a relation an alias using the AS keyword.

I Let us find different ways of writing the query “Find the names ofProfessors who have taught the student whose first name is ’Suri’.”

I The old way:

SELECT Professors.NameFROM Professors, Take, Teach, StudentsWHERE (Professors.PID = Teach.ProfessorPID)

AND (Teach.CourseNumber = Take.CourseNumber)AND (Teach.DeptName = Take.DeptName)AND (Take.StudentPID = Student.PID)AND (Student.Name = ’Suri’);

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 30: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Subqueries in FROM clauses

I Can use a subquery as a relation in a FROM clause.

I We must give such a relation an alias using the AS keyword.

I Let us find different ways of writing the query “Find the names ofProfessors who have taught the student whose first name is ’Suri’.”

I The old way:

SELECT Professors.NameFROM Professors, Take, Teach, StudentsWHERE (Professors.PID = Teach.ProfessorPID)

AND (Teach.CourseNumber = Take.CourseNumber)AND (Teach.DeptName = Take.DeptName)AND (Take.StudentPID = Student.PID)AND (Student.Name = ’Suri’);

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 31: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Professors who have Taught ’Suri’

I “Find the (names of (Professors who have taught (courses taken bystudent with first name ’Suri’))).”

SELECT NameFROM ProfessorsWHERE PID IN

(SELECT ProfessorPIDFROM TeachWHERE (Number, DeptName) IN

(SELECT Number, DeptNameFROM Take, StudentsWHERE (StudentPID = PID) AND

(Students.Name = ’Suri’)));

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 32: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Professors who have Taught ’Suri’

I “Find the (names of (Professors who have taught (courses taken bystudent with first name ’Suri’))).”

SELECT NameFROM ProfessorsWHERE PID IN

(SELECT ProfessorPIDFROM TeachWHERE (Number, DeptName) IN

(SELECT Number, DeptNameFROM Take, StudentsWHERE (StudentPID = PID) AND

(Students.Name = ’Suri’)));

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 33: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Professors who have Taught ’Suri’

I “Find the (names of (Professors who have taught (courses taken by(student with first name ’Suri’)))).”

SELECT NameFROM ProfessorsWHERE PID IN

(SELECT ProfessorPIDFROM TeachWHERE (Number, DeptName) IN

(SELECT Number, DeptNameFROM TakeWHERE StudentPID IN

(SELECT PIDFROM StudentsWHERE Name = ’Suri’)));

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

Page 34: SQL Subqueries - Virginia Techcourses.cs.vt.edu/~cs4604/Fall09/lectures/lecture-04-sql-subqueries.pdfSQL Subquery For Example I Find the name of the professor who teaches\CS 4604."

Professors who have Taught ’Suri’

I “Find the (names of (Professors who have taught (courses taken by(student with first name ’Suri’)))).”

SELECT NameFROM ProfessorsWHERE PID IN

(SELECT ProfessorPIDFROM TeachWHERE (Number, DeptName) IN

(SELECT Number, DeptNameFROM TakeWHERE StudentPID IN

(SELECT PIDFROM StudentsWHERE Name = ’Suri’)));

T. M. Murali September 2, 2009 CS 4604: SQL Subqueries