Datbase Sheet
Datbase Sheet
1. Find the names of all the instructors from Computer Science department or Biology department. - select name from instructor where
dept_name='Comp. Sci.' or dept_name='Biology'; 2. Find the names of courses run by the Biology department. - select title from course where
dept_name='Biology'; 3. Find the names and total credits taken by students who have taken at least 80 total credits. - select name, tot_cred
from student where tot_cred >= 80; 4.Show the names and salary of all instructors in Computer Science department in descending order of
their salary. - select name, salary from instructor where dept_name='Comp. Sci.' order by salary desc; 5. Find the location (building and room
number) of classrooms whose capacity is between 50 and 100. - select building, room_number from classroom where capacity >= 50 and
capacity <= 100; / select building, room_number from classroom where capacity between 50 and 100; 6. Find the total credits across all courses
offered by a particular department (say, Biology) - select sum(credits) from course where dept_name='Biology'; / Renaming: select sum(credits)
total_credits from course where dept_name='Biology'; 7. Find names of all courses whose prerequisite is a particular course (say, CS-101). -
select title from course, prereq where prereq_id='CS-101' and course.course_id=prereq.course_id; / select title from course natural join prereq where
prereq_id='CS-101'; / select title from course inner join prereq where prereq_id='CS-101' and course.course_id=prereq.course_id; 8. Find average
salary of an instructor. - select avg(salary) from instructor; 9. Find average salary of an instructor in Computer Science department. - select
avg(salary) from instructor where dept_name='Comp. Sci.'; 10. Show the names of instructors who earn more than the average salary of an
instructor in Computer Science department. - select name from instructor, (select avg(salary) as avg_sal_CS from instructor where
dept_name='Comp. Sci.') as T where salary > T.avg_sal_CS; / select name from instructor where salary > all (select avg(salary) from instructor where
dept_name = 'Comp. Sci.'); 11. Show the department name and the average salary in that department sorted by average salary. - select
dept_name, avg(salary) as avg_salary from instructor group by dept_name; 12. Show the student name and course title for students who got ‘A’
grade in that course. - select name, title from course, student, takes where grade='A' and student.ID = takes.ID and course.course_id =
takes.course_id; (without join) / select name, title from student natural join takes natural join course where grade='A'; (natural join) - this query is not
correct in general but works in this case because there’s an attribute - dept_name common in course and student also which we don’t need. 14. Find
the names of courses in Physics department which have 2 or 3 credits. - select title from course where dept_name = 'Physics' and (credits = 2 or
credits = 3); / mysql> select title from course where dept_name = 'Physics' and credits in (2,3); 15. For the student with ID 44553 (or any other
fixed value, say 45678), show course ID, title and credits of all courses registered for by the student. - select course_id, title, credits from takes
natural join course where ID = 44553; 16. For the student with ID 44553 (or any other fixed value, say 45678), show the total number of credits
of all courses by the student. - select sum(credits) from takes natural join course where ID = 12345; 17. For the student with ID 44553 (or any
other fixed value, say 45678), show the total number of credits of all courses by the student in Fall semester. - select sum(credits) from takes
natural join course where ID=45678 and semester = 'Fall'; / select sum(credits) from takes natural join course where ID=44553 and semester ='Fall';
18. Same as above three queries but also show the ID of the student along with total credits. - select distinct name from student natural join
takes, course where course.course_id = takes.course_id and course.dept_name = 'Comp. Sci.'; 19. Find the names of students who have taken
any Computer Science course. A student’s name should not be displayed more than once. - select ID from instructor where ID not in (select ID
from instructor natural join teaches); / select ID from instructor where ID except (select ID from instructor natural join teaches); 20. Display the IDs of
all instructors who have never taught a course. - select ID from instructor as T where not exists (select ID from instructor natural join teaches
where instructor.ID=T.ID); / select ID from instructor where ID not in (select ID from instructor natural join teaches); 21. Find the number of courses
offered by Computer Science department. - select count(*) from course where dept_name = 'Comp. Sci.'; 22. Find the number of courses
offered by Computer Science department in the Spring semester. Ensure that multiple offerings are not considered. - select count(distinct
course_id) from course natural join section where dept_name = 'Comp. Sci.' and semester = 'Spring'; 23. Display the IDs of all instructors who
have never taught a course in a Fall semester. 24. As above, but display the names of the instructors also, not just the IDs. - select ID / name
from instructor where ID not in (select ID from instructor natural join teaches where semester = 'Fall'); select ID / name from instructor as T where not
exists (select * from instructor natural join teaches where semester = 'Fall' and instructor.ID = T.ID); 25.For all students whose name has ‘an’ in it,
show their names and the names of courses taken by them. - select name, title from student natural join takes, course where course.course_id =
takes.course_id and name like '%an%'; 26. For all students whose name has ‘an’ in it, show the names of courses taken by them. A course’s
name should not be displayed more than once. - select distinct title from student natural join takes, course where course.course_id =
takes.course_id and name like '%an%'; 27. Find names of instructors who teach courses scheduled in Taylor building. Ensure that an
instructor’s name is not displayed more than once. - select distinct name from instructor natural join teaches natural join section where
building='Taylor'; 28. Same as above, but additionally display their salary. - select distinct name, salary from instructor natural join teaches natural
join section where building='Taylor'; 29. Find all courses whose identifier starts with the string ”CS-1”. - select * from course where course_id like
'CS-1%'; 30. Find names of instructors who have taught at least one of the above courses. - select name from instructor where ID in (select ID
from teaches natural join course where course_id like 'CS-1%'); / select name from instructor where ID = some (select ID from teaches natural join
course where course_id like 'CS-1%'); 31. Find title of courses that do not need any prerequisites. - select title from course as T where course_id
not in (select course_id from course natural join prereq where course.course_id = T.course_id); / select title from course as T where not exists (select
course_id from course natural join prereq where course.course_id = T.course_id); 32. Show the names of students and how many advisors they
have. - Wrong one (ignore that we are showing ID and not name): select s_ID, count(i_ID) from advisor natural join student group by s_ID; Almost
correct one: select s_ID, count(i_ID) from advisor, student where ID=s_ID group by s_ID; But, we want names: select name, count(i_ID) from advisor,
student where ID=s_ID group by s_ID 33. Show the names of instructors and how many advisees they have. - select name, count(s_ID) from
advisor, instructor where ID=i_ID group by i_ID; 34. Show the course ID and the enrollment count across all sections for all courses. - select
course_id, count(ID) from section natural join takes group by course_id; 35. Show the course ID and the enrollment count across all sections for
all courses which have at least 2 students enrolled. - select course_id, count(ID) from section natural join takes group by course_id having
count(ID) >= 2; 36. Show the maximum and minimum enrollment across all sections of all courses. - select max(T.cnt), min(T.cnt) from (select
count(ID) as cnt from section natural join takes group by course_id) as T; 37. For every section of each course, show the section details followed
by the enrollment count. What happens to sections when no student is enrolled for that section. - select course_id, sec_id, semester, year,
count(*) from takes natural join section group by course_id, sec_id, semester, year; 38. Show all sections with the maximum enrollment (along
with the enrollment), using a subquery. - select S.course_id, S.sec_id, S.semester, S.year from (select course_id, sec_id, semester, year, count(*)
as enrollment from takes group by course_id, sec_id, semester, year) as S, (select max(T.enrollment) as enroll from (select course_id, sec_id,
semester, year, count(*) as enrollment from takes group by course_id, sec_id, semester, year) as T) as Y where S.enrollment >= enroll;