5.11 Parametrized subqueries 97
All three queries result in the table:
CNAME
-------------------------
Introduction to Computing
Computer Programming
Computer Architecture
Example 5.10.4 Let us find the students who obtained the highest grade in
cs110. Although there are methods that we explain later that yield much simpler
solutions for this type of query, for the moment we want to illustrate the oper all
condition. We operate on two copies of GRADES. The copy used in the inner
select is intended for computing the grades obtained in cs110:
select stno from GRADES where cno = ’cs110’
and grade >= all(select grade from GRADES
where cno = ’cs110’);
We obtain the table:
STNO
----
5544
Example 5.10.5 Let us find the students who obtained a grade higher than any
grade given by a certain instructor, say Prof. Will. Using the all... subquery
we can write:
select stno from GRADES
where grade >= all(select grade from GRADES
where empno in (select empno from INSTRUCTORS
where name like ’Will%’));
If we alter this query and replace the instructor with Prof. Davis, who teaches
no courses, then the set computed by the query
select stno from GRADES
where grade >= all(select grade from GRADES
where empno in (select empno from INSTRUCTORS
where name like ’Davis%’));
is empty. Therefore, every grade satisfies the inequality, and we obtain all
student numers for students who took any course!