Lab Problem 4
Lab Problem 4
1. Find the names of those departments whose budget is higher than that of Astronomy. List them
in alphabetic order.
dept_name
Athletics
Biology
Cybernetics
Finance
History
Math
Physics
Psychology
2.Display a list of all instructors, showing each instructor's ID and the number of sections
taught. Make sure to show the number of sections as 0 for instructors who have not taught any
section.
ID number_of_sections
16807 0
31955 0
35579 0
37687 0
4034 0
50885 0
52647 0
57180 0
58558 0
59795 0
63395 0
64871 0
72553 0
74426 0
78699 0
79653 0
95030 0
96895 0
97302 0
15347 1
25946 1
4233 1
42782 1
48507 1
48570 1
3. For each student who has retaken a course at least twice (i.e., the student has taken the course
at least three times), show the course ID and the student's ID. Please display your results in
order of course ID and do not display duplicate rows.
SELECT course_id, ID
FROM takes
GROUP BY course_id, ID
HAVING COUNT(*) >= 3
ORDER BY course_id ASC, ID ASC;
course_id ID
362 16480
362 16969
362 27236
362 39925
362 39978
362 44881
362 49611
362 5414
362 69581
362 9993
4. Find the names of Biology students who have taken at least 3 Accounting courses.
SELECT s.name
FROM student s
JOIN takes t ON s.ID = t.ID
JOIN course c ON t.course_id = c.course_id
WHERE s.dept_name = 'Biology'
AND c.dept_name = 'Accounting'
GROUP BY s.ID, s.name
HAVING COUNT(DISTINCT c.course_id) >= 3;
name
Michael
Dalton
Shoji
Uchiyama
Kaminsky
Giannoulis
WITH EnrollmentCounts AS (
SELECT course_id, sec_id, COUNT(ID) AS enrollment_count
FROM takes
WHERE semester = 'Fall' AND year = 2010
GROUP BY course_id, sec_id
)
SELECT course_id, sec_id
FROM EnrollmentCounts
WHERE enrollment_count = (SELECT MAX(enrollment_count) FROM EnrollmentCounts);
6. Find student names and the number of law courses taken for students who have taken at least
half of the available law courses. (These courses are named things like Tort Law or Environment-
tal Law).
name COUNT(*)
Nakajima 4
Nikut 4
Hahn- 4
Nanda 4
Schinag 4
7. Find the rank and name of the 10 students who earned the most A grades (A-, A, A+). Use
alphabetical order by name to break ties. Note: the browser SQLite does not support window
functions.
rank name
1 Neuhold
2 Greene
3 Hons
4 Lepp
5 Lingamp
6 Mandviwall
7 Drig
8 Fabregas
9 Haigh
10 Heilprin
Part -02
ID salary
14365 32241.56
15347 72140.88
16807 98333.65
19368 124651.41
22591 59706.49
25946 90891.69
28097 35023.18
28400 84982.92
31955 71351.42
3199 82534.37
3335 80797.83
34175 115469.11
35579 62579.61
36897 43770.36
37687 104563.38
4034 61387.56
41930 50482.03
4233 88791.45
42782 34272.67
43779 79070.08
48507 107978.47
48570 87549.80
50330 108011.81
50885 32570.50
52647 87958.01
2. Find out the ID and salary of the instructor who gets more than $85,000.
ID salary
16807 98333.65
19368 124651.41
25946 90891.69
34175 115469.11
37687 104563.38
4233 88791.45
48507 107978.47
48570 87549.80
50330 108011.81
52647 87958.01
63287 103146.87
63395 94333.99
6569 105311.38
73623 90038.09
74420 121141.99
74426 106554.73
77346 99382.59
79653 89805.83
90376 117836.50
95709 118143.98
96895 119921.41
99052 93348.83
3. Find out the department names and their budget at the university.
dept_name budget
Accounting 441840.92
Astronomy 617253.94
Athletics 734550.70
Biology 647610.55
Civil Eng. 255041.46
Comp. Sci. 106378.69
Cybernetics 794541.46
Elec. Eng. 276527.61
English 611042.66
Finance 866831.75
Geology 406557.93
History 699140.86
Languages 601283.60
Marketing 210627.58
Math 777605.11
Mech. Eng. 520350.65
Physics 942162.76
Pol. Sci. 573745.09
Psychology 848175.04
Statistics 395051.74
4. List out the names of the instructors from Computer Science who have more than $70,000.
SELECT name FROM instructor WHERE dept_name = 'Comp. Sci.' and salary > 70000;
name
Bourrier
Bondi
5. For all instructors in the university who have taught some course, find their names and the
course ID of all courses they taught.
name course_id
Lembr 200
Lembr 843
Bawa 457
Wieland 545
Wieland 581
Wieland 591
DAgostino 338
DAgostino 338
DAgostino 352
DAgostino 400
DAgostino 400
DAgostino 482
DAgostino 599
DAgostino 642
DAgostino 663
DAgostino 867
DAgostino 962
DAgostino 972
DAgostino 991
Liley 192
Kean 366
Kean 808
Atanassov 603
Atanassov 604
Gustafsson 169
6.Find the names of all instructors whose salary is greater than at least one instructor in the
Biology department.
name
Bawa
Yazdi
Wieland
DAgostino
Liley
Atanassov
Moreira
Gustafsson
Bourrier
Bondi
Soisalon-Soininen
Arias
Murata
Tung
Luo
Romero
Lent
Sarkar
Shuming
Bancilhon
Dusserre
Desyl
Jaekel
McKinnon
Mingoz
7. Find the advisor of the student with ID 12345
77600.188200
9. Find the names of all departments whose building name includes the substring ‘Watson’.
10. Find the names of instructors with salary amounts between $90,000 and $100,000.
SELECT name FROM instructor WHERE salary BETWEEN 90000 AND 100000;
name
Yazdi
Liley
McKinnon
Sullivan
Mahmoud
Dale
11. Find the instructor names and the courses they taught for all instructors in the Biology
department who have taught some course.
name course_id
Queiroz 559
Valtchev 415
Valtchev 702
12. Find the courses taught in Fall-2009 semester.
SELECT course_id FROM teaches WHERE
semester = 'Fall' and year=2009;
course_id
960
242
105
334
304
486
237
13. Find the set of all courses taught either in Fall-2009 or in Spring-2010.
course_id
960
242
692
679
105
334
304
443
486
493
735
270
237
14. Find the set of all courses taught in the Fall-2009 as well as in Spring-2010.
15. Find all courses taught in the Fall-2009 semester but not in the Spring-2010 semester.
(SELECT course_id
FROM teaches
WHERE semester = 'Fall' AND year = 2009)
EXCEPT
(SELECT course_id
FROM teaches
WHERE semester = 'Spring' AND year = 2010);
course_id
960
242
105
334
304
486
237
16. Find all instructors who appear in the instructor relation with null values for salary.
105311.380000
18. Find the total number of instructors who teach a course in the Spring-2010 semester.
dept_name avg_salary
Accounting 48716.592500
Astronomy 79070.080000
Athletics 77098.198000
Biology 61287.250000
Comp. Sci. 98133.470000
Cybernetics 96346.567500
Elec. Eng. 74162.740000
English 72089.050000
Finance 105311.380000
Geology 99382.590000
Languages 57421.856667
Marketing 84097.437500
Mech. Eng. 79813.020000
Physics 114576.900000
Pol. Sci. 100053.073333
Psychology 61143.050000
Statistics 67795.441667
20. Find the number of instructors in each department who teach a course in the Spring-2010
semester.
SELECT dept_name, COUNT(ID) FROM department
NATURAL JOIN instructor
GROUP BY dept_name;
dept_name COUNT(ID)
Accounting 4
Astronomy 1
Athletics 5
Biology 2
Comp. Sci. 2
Cybernetics 4
Elec. Eng. 4
English 4
Finance 1
Geology 1
Languages 3
Marketing 4
Mech. Eng. 2
Physics 2
Pol. Sci. 3
Psychology 2
Statistics 6
21. List out the departments where the average salary of the instructors is more than $42,000.
dept_name avg_salary
Accounting 48716.592500
Astronomy 79070.080000
Athletics 77098.198000
Biology 61287.250000
Comp. Sci. 98133.470000
Cybernetics 96346.567500
Elec. Eng. 74162.740000
English 72089.050000
Finance 105311.380000
Geology 99382.590000
Languages 57421.856667
Marketing 84097.437500
Mech. Eng. 79813.020000
Physics 114576.900000
Pol. Sci. 100053.073333
Psychology 61143.050000
Statistics 67795.441667
22. For each course section offered in 2009, find the average total credits (tot cred) of all students
enrolled in the section, if the section had at least 2 students.
23. Find all the courses taught in both the Fall-2009 and Spring-2010 semesters.
SELECT course_id FROM takes
WHERE (semester = 'Fall' and year = 2009) and (semester = 'Spring' and year = 2010);
24. Find all the courses taught in the Fall-2009 semester but not in the Spring-2010 semester.
SELECT course_id FROM takes
WHERE semester = "Fall" and year = 2009
AND course_id NOT IN
(SELECT course_id FROM takes
WHERE semester = "Spring" and year = 2010);
course_id
242
334
486
960
105
105
304
237
237
242
237
334
960
105
304
105
486
304
334
960
486
960
105
334
237
25. Select the names of instructors whose names are neither Mozart nor Einstein
SELECT name FROM instructor
WHERE name NOT in ("Morzat","Einstein");
name
Lembr
Bawa
Yazdi
Wieland
DAgostino
Liley
Kean
Atanassov
Moreira
Gustafsson
Bourrier
Bondi
Soisalon-Soininen
Morris
Arias
Murata
Tung
Luo
Vicentino
Romero
Lent
Sarkar
Shuming
Konstantinides
Bancilhon
26. Find the total number of (distinct) students who have taken course sections taught by the
instructor with ID 110011.
0
27. Find the ID and names of all instructors whose salary is greater than at least one instructor in
the History department.
SELECT ID,name FROM instructor
WHERE salary > some(
SELECT salary FROM instructor
WHERE dept_name = "History");
28. Find the names of all instructors that have a salary value greater than that of each instructor
in the Biology department.
name
Yazdi
Wieland
Liley
Atanassov
Gustafsson
Bourrier
Bondi
Arias
Luo
Romero
Lent
Sarkar
Shuming
Bancilhon
Jaekel
McKinnon
Mingoz
Pimenta
Sullivan
Voronina
Kenje
Mahmoud
Levine
Bietzk
Sakurai
29.Find the departments that have the highest average salary.
Physics 114576.900000
30.Find all courses taught in both the Fall 2009 semester and in the Spring-2010 semester.
SELECT course_id FROM teaches
WHERE ( semester="Fall" and year= 2009)
AND ( semester="Spring" and year = 2010);
31.Find all students who have taken all the courses offered in the Biology department.
SELECT T.ID, S.name
FROM takes T
JOIN student S ON T.ID = S.ID
JOIN course C ON T.course_id = C.course_id
WHERE C.dept_name = 'Biology'
GROUP BY T.ID, S.name
HAVING COUNT(DISTINCT T.course_id) = (SELECT COUNT(*) FROM course WHERE
dept_name = 'Biology');
32. Find all courses that were offered at most once in 2009.
SELECT course_id FROM takes
WHERE year = 2009
GROUP BY course_id
HAVING COUNT(*)<=1;
33. Find all courses that were offered at least twice in 2009.
SELECT course_id FROM takes
WHERE year = 2009
GROUP BY course_id
HAVING COUNT(*)>=2;
course_id
105
237
242
304
334
486
604
960
972
34.Find the average instructors' salaries of those departments where the average salary is
greater than $42,000.
dept_name avg_salary
Accounting 48716.592500
Astronomy 79070.080000
Athletics 77098.198000
Biology 61287.250000
Comp. Sci. 98133.470000
Cybernetics 96346.567500
Elec. Eng. 74162.740000
English 72089.050000
Finance 105311.380000
Geology 99382.590000
Languages 57421.856667
Marketing 84097.437500
Mech. Eng. 79813.020000
Physics 114576.900000
Pol. Sci. 100053.073333
Psychology 61143.050000
Statistics 67795.441667
35. Find the maximum across all departments of the total salary at each department.
Statistics 406772.65
36. List all departments along with the number of instructors in each department.
dept_name no_of_instructor
Accounting 4
Astronomy 1
Athletics 5
Biology 2
Comp. Sci. 2
Cybernetics 4
Elec. Eng. 4
English 4
Finance 1
Geology 1
Languages 3
Marketing 4
Mech. Eng. 2
Physics 2
Pol. Sci. 3
Psychology 2
Statistics 6