Question

In: Computer Science

Database Design and SQL Consider the following relations: Student (snum: integer, sname: string, major: string, level:...

Database Design and SQL

Consider the following relations:

Student (snum: integer, sname: string, major: string, level: string, age: integer)
Class (name: string, meets_at: time, room: string, fid: integer)
Enrollment (snum: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)


Write the following queries in SQL. Also, there should be no duplicates printed in any of the answers.

a) Find the names of all students who are enrolled in two classes that meet at the same time.
b) Find the names of faculty members for whom the combined enrollment of the courses that they teach is less than five.
c) Print the Level and the average age of students for that Level, for each Level.
d) Print the Level and the average age of students for that Level, for all Levels except JR.
e) Find the names of students who are enrolled in the maximum number of classes.
f) Find the names of students who are not enrolled in any class.

Solutions

Expert Solution

/****************** ANSWER A**********************/
SELECT DISTINCT S.SNAME
FROM
  STUDENT S
WHERE
  S.SNUM IN (SELECT E1.SNUM
             FROM
               ENROLLMENT E1,
               ENROLLMENT E2,
               CLASS C1,
               CLASS C2
             WHERE
               E1.SNUM = E2.SNUM
               AND E1.CNAME = E2.CNAME
               AND E1.CNAME = C1.NAME
               AND E2.CNAME = C2.NAME
               AND C1.MEETS_AT = C2.MEETS_AT
  );

/****************** ANSWER B**********************/
SELECT DISTINCT F.fname
FROM
  FACULTY F
WHERE 5 > (SELECT COUNT(E.SNUM)
           FROM CLASS C, ENROLLMENT E
           WHERE C.NAME = E.CNAME AND C.FID = F.FID);


/****************** ANSWER C**********************/
SELECT
  S.LEVEL,
  AVG(S.AGE) AS 'AVERAGE AGE'
FROM STUDENT S
GROUP BY S.LEVEL;


/****************** ANSWER D**********************/

SELECT
  S.LEVEL,
  AVG(S.AGE) AS 'AVERAGE AGE'
FROM STUDENT S
WHERE S.LEVEL <> 'JR'
GROUP BY S.LEVEL;


/****************** ANSWER E**********************/

SELECT DISTINCT S.SNAME
FROM STUDENT S
WHERE S.SNUM IN (SELECT E.SNUM
                 FROM ENROLLMENT E
                 GROUP BY E.SNUM);


/****************** ANSWER E**********************/

SELECT DISTINCT S.SNAME
FROM STUDENT S
WHERE S.SNUM NOT IN (SELECT E.SNUM
                     FROM ENROLLMENT E);

Related Solutions

Question (3) Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer)...
Question (3) Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer) Class(name: string, meets at: string, 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. Write the following queries in Oracle SQL. No duplicates should be printed in any of the answers. i) (2 points)...
Database Design and SQL The following relations keep track of airline flight information: Flights (flno: integer,...
Database Design and SQL The following relations keep track of airline flight information: Flights (flno: integer, from : string, to: string, distance: integer, departs: time, arrive: time, price: integer) Aircraft (aid: integer, aname : string, cruisingrange: integer) Certified (eid: integer, aid: integer) Employees (eid: integer, ename : string, salary: integer) The Employees relation describe pilots and other kinds of employees as well. Every pilot is certified for some aircraft and only pilots are certified to fly. Based on the schemas,...
INTRO TO DATABASE Consider the Sailors-Boats-Reserves database described below. Sailors(sid: integer, sname: string, rating: integer, age:...
INTRO TO DATABASE Consider the Sailors-Boats-Reserves database described below. Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date) Write each of the following queries in SQL. 1. Find the names and ages of sailors who have reserved at least two boats. 2. For each boat reserved by at least 2 distinct sailors, find the boat id and the average age of sailors who reserved it.
Consider the following schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string)...
Consider the following schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The key fields are underlined, and the domain of each field is listed after the field name. Therefore sid is the key for Suppliers, pid is the key for Parts, and sid and pid together form the key for Catalog. The Catalog relation lists the prices charged for parts by Suppliers. WRITE THE FOLLOWING QUERIES IN RELATIONAL ALGEBRA...
INRO TO DATABASES Consider the following Schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname:...
INRO TO DATABASES Consider the following Schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in SQL using join, nested queries and set operators. 1. Find names of suppliers who supply every red or green part. 2. Find the sids of suppliers who supply every red part or supply every green part. 3. Find sids...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours) Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size) where, Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (year i.e, freshman, sophomore, junior, etc). Courses:...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours)Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size), Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (yeari.e, freshman, sophomore, junior, etc). Courses: All courses have...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours) Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size) where, Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (year i.e, freshman, sophomore, junior, etc). Courses:...
Given a relational database that consists of the following relations: Performer (pid: integer, pname: string, years_of_experience:...
Given a relational database that consists of the following relations: Performer (pid: integer, pname: string, years_of_experience: integer, age: integer) Movie (mname: string, genre: string, minutes: integer, release_year: integer, did: integer) Acted (pid: integer, mname: string) Director (did: integer, dname: string, earnings: real) Do the following using your Azure SQL database: a) Use SQL statements to create the relations. b) Populate the relations using SQL statements with the given data posted on Canvas. c) Implement the SQL queries for the following:...
The following relations are part of a school database: STUDENT(STUD#, STUD_NAME, MAJOR, YEAR, GPA) TEACHER(FACULTY#, DEPT,...
The following relations are part of a school database: STUDENT(STUD#, STUD_NAME, MAJOR, YEAR, GPA) TEACHER(FACULTY#, DEPT, TEACHERNAME) ENROLLMENT(STUD#, COURSE#, GRADE) RESPONSIBILITY(FACULTY#, COURSE#) Using PROJECT, SELECT and JOIN, write the sequence of operations to answer each of the following questions: What are the names of teachers who are responsible for courses in which students whose name is 'JONES' are enrolled? Use relational Algebra and relational calculus for this question.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT