0% found this document useful (0 votes)
63 views

SQL Subqueries: T. M. Murali

The document discusses SQL subqueries. It provides examples of using subqueries to find the professor teaching a particular course, the departments of courses taken by a student, and course names used for multiple courses. Correlated subqueries are evaluated by looping through tuples and scoping attributes to the relevant subquery or relation. Subqueries can also be used in FROM clauses, with an alias, to find professor names for courses taken by a student named 'Suri'.

Uploaded by

Cma Sankaraiah
Copyright
© Attribution Non-Commercial (BY-NC)
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)
63 views

SQL Subqueries: T. M. Murali

The document discusses SQL subqueries. It provides examples of using subqueries to find the professor teaching a particular course, the departments of courses taken by a student, and course names used for multiple courses. Correlated subqueries are evaluated by looping through tuples and scoping attributes to the relevant subquery or relation. Subqueries can also be used in FROM clauses, with an alias, to find professor names for courses taken by a student named 'Suri'.

Uploaded by

Cma Sankaraiah
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 34

SQL Subqueries

T. M. Murali

September 1, 2010

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Linear Notation for Relational Algebra

Relational algebra expressions can become very long. 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 nal result. 2. The assignment symbol :=. 3. Any expression in relational algebra on the right.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Example of Linear Notation

Name pairs of students who live at the same address. Normal expression: S1.Name,S2.Name (S1.Address
= S2.Address (S1 (Students)

S2 (Students)))

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Example of Linear Notation

Name pairs of students who live at the same address. Normal expression: S1.Name,S2.Name (S1.Address 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)).
= S2.Address (S1 (Students)

S2 (Students)))

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Interpreting Queries Involving Multiple Relations

SELECT A, B FROM R, S WHERE C;

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Interpreting Queries Involving Multiple Relations

SELECT A, B FROM R, S WHERE C; Nested loops: for each tuple t1 in R for each tuple t2 in S if the attributes in t1 and t2 satisfy C output the tuples involving attributes A and B.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Interpreting Queries Involving Multiple Relations

SELECT A, B FROM R, S WHERE C; Nested loops: for each tuple t1 in R for each tuple t2 in S if the attributes in t1 and t2 satisfy C output the tuples involving attributes A and B. 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 1, 2010

CS 4604: SQL Subqueries

Motivation for Subqueries

Find the name of the professor who teaches CS 4604.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Motivation for Subqueries

Find the name of the professor who teaches CS 4604. SELECT Name FROM Professors, Teach WHERE (PID = ProfessorPID) AND (Number = 4604) AND (DeptName = CS) ;

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Motivation for Subqueries

Find the name of the professor who teaches CS 4604. SELECT Name FROM Professors, Teach WHERE (PID = ProfessorPID) AND (Number = 4604) AND (DeptName = CS) ; Do we need to take the natural join of two big relations just to get a relation with one tuple? Can we rewrite the query without using a join?

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

SQL Subquery For Example

Find the name of the professor who teaches CS 4604.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

SQL Subquery For Example

Find the name of the professor who teaches CS 4604. SELECT Name FROM Professors WHERE PID = (SELECT ProfessorPID FROM Teach WHERE (Number = 4604) AND (DeptName = CS) );

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

SQL Subquery For Example

Find the name of the professor who teaches CS 4604. SELECT Name FROM Professors WHERE PID = (SELECT ProfessorPID FROM Teach WHERE (Number = 4604) AND (DeptName = CS) ); When using =, the subquery must return a single tuple.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Conditions Involving Relations


SQL includes a number of operators that apply to a relation and produce a boolean result. These operators are very useful to apply on results of sub-queries.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Conditions Involving Relations


SQL includes a number of operators that apply to a relation and produce a boolean result. These operators are very useful to apply on results of sub-queries. Let R be a relation and t be a tuple with the same set of attributes.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Conditions Involving Relations


SQL includes a number of operators that apply to a relation and produce a boolean result. These operators are very useful to apply on results of sub-queries. Let R be a relation and t be a tuple with the same set of attributes. EXISTS R is true if and only if R contains at least one tuple.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Conditions Involving Relations


SQL includes a number of operators that apply to a relation and produce a boolean result. These operators are very useful to apply on results of sub-queries. Let R be a relation and t be a tuple with the same set of attributes. EXISTS R is true if and only if R contains at least one tuple. t IN R is true if and only if t equals a tuple in R.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Conditions Involving Relations


SQL includes a number of operators that apply to a relation and produce a boolean result. These operators are very useful to apply on results of sub-queries. Let R be a relation and t be a tuple with the same set of attributes. EXISTS R is true if and only if R contains at least one tuple. t IN R is true if and only if t equals a tuple in R. t > ALL R is true if and only if R is unary (has one attribute) and t is greater than every value in R.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Conditions Involving Relations


SQL includes a number of operators that apply to a relation and produce a boolean result. These operators are very useful to apply on results of sub-queries. Let R be a relation and t be a tuple with the same set of attributes. EXISTS R is true if and only if R contains at least one tuple. t IN R is true if and only if t equals a tuple in R. t > ALL R is true if and only if R is unary (has one attribute) and t is greater than every value in R.
Can use any of the other ve comparison operators. If we use <>, R need not be unary.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Conditions Involving Relations


SQL includes a number of operators that apply to a relation and produce a boolean result. These operators are very useful to apply on results of sub-queries. Let R be a relation and t be a tuple with the same set of attributes. EXISTS R is true if and only if R contains at least one tuple. t IN R is true if and only if t equals a tuple in R. t > ALL R is true if and only if R is unary (has one attribute) and t is greater than every value in R.
Can use any of the other ve comparison operators. If we use <>, R need not be unary.

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

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Conditions Involving Relations


SQL includes a number of operators that apply to a relation and produce a boolean result. These operators are very useful to apply on results of sub-queries. Let R be a relation and t be a tuple with the same set of attributes. EXISTS R is true if and only if R contains at least one tuple. t IN R is true if and only if t equals a tuple in R. t > ALL R is true if and only if R is unary (has one attribute) and t is greater than every value in R.
Can use any of the other ve comparison operators. If we use <>, R need not be unary.

t > ANY R (which is unary) is true if and only if t is greater than at least one value in R. We can use NOT to negate EXISTS, ALL, and ANY.
T. M. Murali September 1, 2010 CS 4604: SQL Subqueries

Subqueries Using Conditions

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

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Subqueries Using Conditions

Find the departments of the courses taken by the student with name Suri. SELECT DeptName FROM Take WHERE StudentPID IN (SELECT PID FROM Students WHERE (Name = Suri) );

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Correlated Subqueries

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

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Correlated Subqueries

Find course names that have been used for two or more courses. SELECT CourseName FROM Courses AS First WHERE CourseName IN (SELECT CourseName FROM Courses WHERE

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Correlated Subqueries

Find course names that have been used for two or more courses. SELECT CourseName FROM Courses AS First WHERE CourseName IN (SELECT CourseName FROM Courses WHERE (Number <> First.Number) OR (DeptName <> First.DeptName));

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Evaluating Correlated Subqueries

SELECT CourseName FROM Courses AS First WHERE CourseName IN (SELECT CourseName FROM Courses WHERE (Number <> First.Number) OR (DeptName <> First.DeptName));

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

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Evaluating Correlated Subqueries

SELECT CourseName FROM Courses AS First WHERE CourseName IN (SELECT CourseName FROM Courses WHERE (Number <> First.Number) OR (DeptName <> First.DeptName));

Evaluate query by looping over tuples of First and for each tuple, evaluate the subquery. Scoping rules: an attribute in a subquery belongs to one of the tuple variables in that subquerys FROM clause, or to the immediately surrounding subquery, and so on.
T. M. Murali September 1, 2010 CS 4604: SQL Subqueries

Subqueries in FROM clauses


Can use a subquery as a relation in a FROM clause. We must give such a relation an alias using the AS keyword. Let us nd dierent ways of writing the query Find the names of Professors who have taught the student whose name is Suri.

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Subqueries in FROM clauses


Can use a subquery as a relation in a FROM clause. We must give such a relation an alias using the AS keyword. Let us nd dierent ways of writing the query Find the names of Professors who have taught the student whose name is Suri. The old way:
SELECT Professors.Name FROM Professors, Take, Teach, Students WHERE (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 1, 2010

CS 4604: SQL Subqueries

Professors who have Taught Suri

Find the (names of (Professors who have taught (courses taken by student with name Suri))).

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Professors who have Taught Suri

Find the (names of (Professors who have taught (courses taken by student with name Suri))).
SELECT Name FROM Professors WHERE PID IN (SELECT ProfessorPID FROM Teach WHERE (Number, DeptName) IN (SELECT Number, DeptName FROM Take, Students WHERE (StudentPID = PID) AND (Students.Name = Suri)));

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Professors who have Taught Suri


Find the (names of (Professors who have taught (courses taken by (student with rst name Suri)))).

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

Professors who have Taught Suri


Find the (names of (Professors who have taught (courses taken by (student with rst name Suri)))).
SELECT Name FROM Professors WHERE PID IN (SELECT ProfessorPID FROM Teach WHERE (Number, DeptName) IN (SELECT Number, DeptName FROM Take WHERE StudentPID IN (SELECT PID FROM Students WHERE Name = Suri)));

T. M. Murali

September 1, 2010

CS 4604: SQL Subqueries

You might also like