Write the following queries in SQL. No duplicates should be printed for any answ
ID: 3826126 • 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.
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
Sorry. I could answer only the first 2.
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.
Print the average level of students in each class, for every class.
Hey. You gave level as a string, and the aggregate methods cannot be applied on strings.
So, taking the level is of type int, the query is:
SELECT Cname, AVG(Level) FROM Student S, Enrolled E GROUP BY Cname;
Print the average age and average level of students in each major, for every major.
SELECT Major, AVG(Age), AVG(level) FROM Student GROUP BY Major;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.