Ans Assignment2a

May 5, 2017 | Author: Murugan Ramachandran Pillai R | Category: N/A
Share Embed Donate


Short Description

DBMS Query Assignment 2a...

Description

Assignment 2a: Intermediate SQL: Aggregates and grouping and ordering

1. Find the number of instructors who have never taught any course. If the result of your query is empty, add appropriate data (and include corresponding insert statements) to ensure the result is not empty. Ans: select count(distinct ID) from instructor where ID not in (select ID from teaches); Output: 3 2. Find the total capacity of every building in the university Ans: select building, sum(capacity) from classroom group by building; Output: "Painter"; "Taylor"; "Packard"; "Watson";

10 70 500 80

3. Find the maximum number of teachers for any single course section. Your output should be a single number. For example if CS-101 section 1 in Spring 2012 had 3 instructors teaching the course, and no other section had more instructors teaching the section, your answer would be 3. Ans: select max(totc) from (select count(id) as totc from teaches group by course_id, sec_id,semester, year) as teacher_count Output: 1

4. Find all departments that have at least one instructor, and list the names of the departments along with the number of instructors; order the result in descending order of number of instructors. Ans: select dept_name, count(ID) from instructor group by dept_name having count(ID) > 0 order by count(ID) desc; Output: "Comp. Sci."; "Finance"; "Physics"; "History"; "Elec. Eng."; "Music"; "Biology";

3 2 2 2 1 1 1

5. As in the previous question, but this time you shouold include departments even if they do not have any instructor, with the count as 0 Ans: select dept_name, (select count(*) from instructor Inst where Inst.dept_name=Dept.dept_name) as count from department Dept order by count desc; Output: "Comp. Sci."; "Finance"; "Physics"; "History"; "Elec. Eng."; "Music"; "Biology";

3 2 2 2 1 1 1

6. For each student, compute the total credits they have successfully completed, i.e. total credits of courses they have taken, for which they have a non-null grade other than 'F'. Do NOT use the tot_creds attribute of student. Ans: select student.id, student.name, sum(course.credits) from student, course, takes where student.id = takes.id and takes.grade 'F' and takes.grade is not NULL and takes.course_id = course.course_id group by student.id; Output: "00128"; "19991"; "98765"; "23121"; "98988"; "44553"; "45678"; "76653"; "54321"; "55739"; "12345"; "76543";

"Zhang"; "Brandt"; "Bourikas"; "Chavez"; "Tanaka"; "Peltier"; "Levy"; "Aoi"; "Williams"; "Sanchez"; "Shankar"; "Brown";

7 3 7 3 4 4 7 3 8 3 14 7

7. Find the number of students who have been taught (at any time) by an instructor named 'Srinivasan'. Make sure you count a student only once even if the student has taken more than one course from Srinivasan. Ans: select count(distinct takes.ID) from takes, teaches, instructor where takes.course_id = teaches.course_id and teaches.ID = instructor.ID and instructor.name = 'Srinivasan'; Output: 6

Optional 8. Find the name of all instructors who get the highest salary in their department. 9. Find all students who have taken all courses taken by instructor 'Srinivasan'. (This is the division operation of relational algebra.) You can implement it by counting the number of courses taught by Srinivasan, and for each student (i.e. group by student), find the number of courses taken by that student, which were taught by Srinivasan. Make sure to count each course ID only once. 10. Find the total money spent by each department for salaries of instructors of that department. 11. Find the names of all students whose advisor has taught the maximum number of courses (multiple offerings of a course count as only 1). (Note: this is a complex query, break it into parts by using the with clause.)

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF