Lab Project #3
– SQL Queries: Widom database
Emile C Chi
Program the following queries in SQL Due Tuesday Oct 1
Your output for each query must contain the SQL code and the resulting table.
1. List the sID and sName and cName of all students who have applied to major in either CS or EE at any college select distinct s.sID, s.sName, a.cName from student s, apply a where s.sID = a.sID
and (a.major = 'CS'
or a.major = 'EE') order by s.sID; sID sName cName
123 Amy Stanford
123 Amy Berkeley
123 Amy Cornell
345 Craig Cornell
543 Craig MIT
876 Irene Stanford
987 Helen Stanford
987 Helen Berkeley
2. List the sID and sName and GPA of all students who have applied to major in CS but not in EE at any college select distinct s.sID, s.sName, s.GPA from student s where exists
(select a.sID
from apply a
where s.sID = a.sID or
and a.major = 'CS') and not exists
(select a.sID
from apply a
where s.sID = a.sID
and a.major = 'EE') order by s.sID;
select distinct s.sID, s.sName, s.GPA from student s where s.sID in
(select a.sID
from apply a
where s.sID = a.sID
and a.major = 'CS') and s.sID not in
(select a.sID
from apply a
where s.sID = a.sID
and a.major = 'EE') order by s.sID; sID sName GPA
543 Craig 3.4
876 Irene 3.9
987 Helen 3.7
3. List the sID and sName and GPA of all students who have applied to major in CS at Stanford but not at Berkeley select distinct s.sID, s.sName, s.GPA from student s where s.sID in
(select a.sID
from apply a
where s.sID = a.sID
and a.major = 'CS'
and a.cName = 'Stanford') and s.sID not in
(select a.sID
from apply a
where s.sID = a.sID
and a.major = 'CS'
and a.cName = 'Berkeley') order by s.sID; sID sName GPA
876 Irene 3.9
4. List the sID and sName and GPA, the college name and enrollment of all students and all colleges to which they applied and were accepted in descending GPA order. select distinct s.sID, s.sName, s.GPA, c.cName, c.enrollment from student s, college c , apply a where s.sID = a.sID
and a.cName = c.cName
and a.decision = 'Y' order by s.GPA desc; sID sName GPA cName Enrollment
123 Amy 3.9 Stanford 15000
123 Amy 3.9 Berkeley 36000
123 Amy 3.9 Cornell 21000
876 Irene 3.9 MIT 10000
678 Fay 3.8 Stanford 15000
987 Helen 3.7 Stanford 15000
987 Helen 3.7 Berkeley 36000
345 Craig 3.5 MIT 10000
345 Craig 3.5 Cornell 21000
765 Jay 2.9 Stanford 15000
765 Jay 2.9 Cornell 21000
5. List the sID and sName for all students where there is another student with the same name. select distinct s1.sID, s1.sName, s2.sID, s2.sName from student s1, student s2 where s1.sName = s2.sName
and s1.sID < s2.sID order by s1.sID; sID sName sID sName
123 Amy 654 Amy
345 Craig 543 Craig
6. List the sID and sName and GPA of all students who have the lowest GPA. You may use the min operator. select distinct s.sID, s.sName, s.GPA from student s where GPA = (select min(GPA)
from student) order by s.sID;
7. List the sID and sName and GPA of all students who have the lowest GPA. Do not use the min operator. select distinct s.sID, s.sName, s.GPA from student s where GPA <= all(select GPA
from student) order by s.sID; sID sName GPA
567 Edward 2.9
765 Jay 2.9
8. List the sID and sName and cName and major of all students and all colleges to which they applied. select distinct s.sID, s.sName, a.cName, a.major from student s inner join apply a on s.sID = a.sID order by s.sID; select distinct s.sID, s.sName, a.cName, a.major from student s, apply a where s.sID = a.sID order by s.sID; sID sName cName Major
123 Amy Stanford CS
123 Amy Stanford EE
123 Amy Berkeley CS
123 Amy Cornell EE
234 Bob Berkeley Biology
345 Craig MIT Bioengineering
345 Craig Cornell Bioengineering
345 Craig Cornell CS
345 Craig Cornell EE
543 Craig MIT CS
678 Fay Stanford History
765 Jay Stanford History
765 Jay Cornell History
765 Jay Cornell Psychology
876 Irene Stanford CS
876 Irene MIT Biology
876 Irene MIT marine biology
987 Helen Stanford CS
987 Helen Berkeley CS
9. List the sID and sName and cName and major of all students whether they applied to a college or not. select distinct s.sID, s.sName, a.cName, a.major from student s right join apply a on s.sID = a.sID order by s.sID; sID sName cName major
NULL NULL MIT psychology
NULL NULL MIT history
123 Amy Stanford CS
123 Amy Stanford EE
123 Amy Berkeley CS
123 Amy Cornell EE
234 Bob Berkeley biology
345 Craig MIT bioengineering
345 Craig Cornell bioengineering
345 Craig Cornell CS
345 Craig Cornell EE
543 Craig MIT CS
678 Fay Stanford history
765 Jay Stanford history
765 Jay Cornell history
765 Jay Cornell psychology
876 Irene Stanford CS
876 Irene MIT biology
876 Irene MIT marine biology
987 Helen Stanford CS
987 Helen Berkeley CS