6 sql queries and subqueries
6 sql queries and subqueries
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND R.bid=103;
Example of Conceptual Evaluation (1)
Sailors Reserves
SELECT s2.name
FROM SALESREPS s1, SALESREPS s2
WHERE s1.name=‘Bob’ AND
s1.manager=s2.empl_num;
• Professors (PID: string, Name: string, Office: string, Age: integer, DepartmentName:
string)
SELECT DeptName
FROM Take
WHERE StudentPID IN
( SELECT PID
FROM Students
WHERE (Name = ‘Suri’)
);
Correlated vs Uncorrelated
• 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)
AND (DeptName <> First.DeptName)
);
Evaluating Correlated Subqueries
SELECT CourseName
FROM Courses AS First
WHERE CourseName IN
(SELECT CourseName
FROM Courses
WHERE (Number <> First.Number)
AND (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 subquery’s FROM clause, or to the
immediately surrounding subquery, and so on.
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 find different ways of writing the query “Find the names of
Professors who have taught the student whose first 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 %’);
• “Find the names of (Professors who have taught (courses taken
by (student with first name ’Suri’))).”
SELECT Name
FROM Professors
WHERE PID IN
(SELECT ProfessorPID
FROM Teach
WHERE (Number, DeptName) IN
( SELECT Number, DeptName
FROM TakeWHERE StudentPID IN
(SELECT PID
FROM Students
WHERE Name = ’Suri %’)
)
);
Aggregate Operators
• COUNT (*)
• COUNT ( [DISTINCT] A)
– A is a column
• SUM ( [DISTINCT] A)
• AVG ( [DISTINCT] A)
• MAX (A)
• MIN (A)
• Count the number of sailors
SELECT COUNT (*)
FROM Sailors S
21
Find the average age of sailors with
rating = 10
Sailors(sid: integer, sname: string, rating: integer,
age: real)
22
Count the number of different
sailor names
Sailors(sid: integer, sname: string, rating: integer,
age: real)
SELECT COUNT (DISTINCT S.sname)
FROM Sailors S
23
Find the age of the oldest sailor
Sailors(sid: integer, sname: string, rating: integer,
age: real)
SELECT MAX(S.AGE)
FROM Sailors S
24
Find name and age of the oldest
sailor(s)
SELECT S.sname, MAX (S.age)
FROM Sailors S
25
GROUP BY and HAVING
• So far, aggregate operators are applied to all (qualifying)
tuples.
– Can we apply them to each of several groups of tuples?
• Example: find the age of the youngest sailor for each rating
level.
– In general, we don’t know how many rating levels exist, and what the
rating values for these levels are!
– Suppose we know that rating values go from 1 to 10; we can write 10
queries that look like this:
29
Starwars Exercises
char(name, race, homeworld, affiliation)
planets(name, type, affiliation)
timetable(cname, pname, movie, arrival, departure)
30
Starwars Exercises
char(name, race, homeworld, affiliation)
planets(name, type, affiliation)
timetable(cname, pname, movie, arrival, departure)
SELECT count(*)
FROM timetable, characters
WHERE movie=3 and pname =‘Dagobah’ and
timetable.cname=characters.name and
characters.race=‘Human’;
31
Starwars Exercises
char(name, race, homeworld, affiliation)
planets(name, type, affiliation)
timetable(cname, pname, movie, arrival, departure)
32
Starwars Exercises
char(name, race, homeworld, affiliation)
planets(name, type, affiliation)
timetable(cname, pname, movie, arrival, departure)
33
Starwars Exercises
char(name, race, homeworld, affiliation)
planets(name, type, affiliation)
timetable(cname, pname, movie, arrival, departure)
• For each character and for each neutral planet, how much
time total did the character spend on the planet?
34