0% found this document useful (0 votes)
31 views3 pages

Assignment 2a More SQL Aggregates - Solutions

solns of Assignment 2a( More SQL Aggregates )

Uploaded by

nisha charaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Assignment 2a More SQL Aggregates - Solutions

solns of Assignment 2a( More SQL Aggregates )

Uploaded by

nisha charaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 2a: More SQL: Aggregates -

Solutions
Solutions:

1. select count(*) from instructor where ID not in (select ID from


teaches);

2. select building, sum(capacity) from classroom group by building;

3. select max(T) from (select count(ID) as T from teaches group by


course_id, sec_id, semester, year) as temp;

4. select dept_name,count(*) as num from instructor group by


(dept_name)
order by num desc ;

5. select dept_name,
(select count(*)
from instructor
where instructor.dept_name = department.dept_name) as num
from department
order by num desc ;

alternative solution: use

select dept_name, count(ID) as num


from (department natural left outer join instructor)
order by num desc;

6. select ID, name, sum(course.credits) from takes, student, course


where grade <> 'F' and grade is not null and takes.course_id =
course.course_id and student.ID = takes.ID group by ID, name;

7. select count(distinct takes.ID)


from instructor, takes, teaches, section
where takes.course_id = teaches.course_id and takes.sec_id =
teaches.sec_id and
takes.semester = teaches.semester and
takes.year = teaches.year and section.course_id=teaches.course_id
and section.semester=teaches.semester and
section.sec_id=teaches.sec_id and section.year=teaches.year and
teaches.ID=instructor.ID and instructor.name='Srinivasan'

NOTE: A=B and B=C can be instead written as A=B and A=C (for
attributes of section, teaches and takes)
OR

select count(distinct takes.ID)


from ((instructor natural join teaches) natural join section) join takes
using (course_id, sec_id, year, semester)

Optional
8. Select name from instructor I1 ,
(select max(salary) as maxsal,dept_name from instructor I2 group by
dept_name ) I2
where (I2.maxsal = I1.salary and I1.dept_name = I2.dept_name );

9. select distinct S.ID,name from takes as S, student where S.ID =


student.ID and not exists
( (select course_id from teaches,instructor
where instructor.ID=teaches.ID and instructor.name='Srinivasan')
except (select course_id from takes as T where student.ID = T.ID ));

10. select department.dept_name, sum(salary) from instructor,


department
where instructor.dept_name = department.dept_name group by
department.dept_name ;

11. with instr_course_count as (


select id, count(distinct course_id) course_count
from teaches
group by id
),
max_course_instructor as (
select id
from instr_course_count
where course_count = (select max(course_count) from
instr_course_count)
)
select s.name
from student s join advisor a on (s.id=a.s_id)
where a.i_id in (select id from max_course_instructor)

NOTE: There are other ways to write this query. For example:
max_course_instructor can be defined without using subqueries as

max_courses as (
select max(course_count) as max_count from instr_course_count),
max_course_instructor as (
select id
from instr_course_count, ma_courses
where course_count = max_count
)

You might also like