Write the following queries in SQL. No duplicates should be printed for any answ
ID: 3825559 • Letter: W
Question
Write the following queries in SQL. No duplicates should be printed for any answer.
Consider the following relations:
Student(snum: integer, sname: string, major: string, level: string, age: integer)
Class(name: string, time: TIME, room: string, fid: integer)
Enrolled(snum: integer, cname: string)
Faculty(fid: integer, fname: string, deptid: integer)
The meaning of these relations is straightforward; for example, Enrolled has one record per student-class pair such that the student is enrolled in the class.
Find the level of all students who are enrolled in a class that starts before 9:00AM.
Find the course with the most students enrolled that starts before 9:00AM.
Find the number of unique students that every professor teaches.
Find the names of all Sophomore Computer Science majors who are enrolled in a class taught by Mark Sheldon.
Find the name of the youngest student who is an American Studies major or in an Introduction to International Relationsclass.
Print the average level of students in each class, for every class.
Print the average age and average level of students in each major, for every major.
Find the major in which the most students have more than one class with a given professor.
Find all pairs of students taking the same courses.
Find all pairs of students taking courses from the same professors.
For each major, find the student with the largest gap in their schedule (time between two classes).
Explanation / Answer
Given relations are:
Student(snum: integer, sname: string, major: string, level: string, age: integer)
Class(name: string, time: TIME, room: string, fid: integer)
Enrolled(snum: integer, cname: string)
Faculty(fid: integer, fname: string, deptid: integer).
1.Find the level of all students who are enrolled in a class that starts before 9:00AM.
select S.level from Student S,Class C,Enrolled E where E.snum=S.snum and E.cname=C.name and c.time= 09.00;
2. Find the course with the most students enrolled that starts before 9:00AM.
select C.name from Student S,Class C,Enrolled E where E.snum=S.snum and E.cname=C.name and max (c.time= 09.00);
3. Find the number of unique students that every professor teaches.
selct Distinct count(C.snum) from Student S,Class C,Enrolled E, Faculity F where E.snum=S.snum and E.cname=C.name and C.fid=F.fid;
4. Find the names of all Sophomore Computer Science majors who are enrolled in a class taught by Mark Sheldon.
select S.sname from Student S,Class C,Enrolled E,Faculity F where E.snum=S.snum and E.cname=C.name
and C.fid=F.fid and S.major= 'Sophomore Computer Science' and F.fname='Mark Sheldon'.
5. Find the name of the youngest student who is an American Studies major or in an Introduction to International Relationsclass.
select S.sname from Student S,Class C,Enrolled E,Faculity F where E.snum=S.snum and E.cname=C.name
and C.fid=F.fid and S.major= 'Sophomore Computer Science OR S.major= 'Introduction to International Relationsclass' and a.age BETWEEN 20 AND 30.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.