a2.sql 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. -- Current term is 1185
  2. /*
  3. The student number and name of second year students who have obtained
  4. a grade lower than 65 in at least two courses in a department with the
  5. name “computer science”.
  6. */
  7. -- ran on empty
  8. select distinct sname, snum from student where year = 2 and snum in
  9. (
  10. select snum from mark m1 where grade < 65 and deptcode = 'CS' and snum in
  11. (
  12. select snum from mark where grade < 65 and deptcode = 'CS' and snum != m1.snum
  13. )
  14. );
  15. /*
  16. The number and name of professors who are not in the pure math (PM)
  17. department, and who are teaching CS245 for the first time.
  18. */
  19. -- ran on empty
  20. select distinct pnum, pname from professor where deptcode not in
  21. (
  22. select deptcode from department where deptname = 'PM'
  23. ) and pnum in
  24. (
  25. select pnum from class where deptcode = 'CS' and cnum = 245 and term = 1185
  26. ) and pnum not in
  27. (
  28. select pnum from class where deptcode = 'CS' and cnum = 245 and term != 1185
  29. );
  30. /*
  31. The number, name and year of each student who has obtained a grade in
  32. CS240 that is within 3 marks of the highest ever grade recorded for that
  33. course.
  34. */
  35. select s.snum, s.sname, s.year from student s where s.snum in
  36. (
  37. select snum from mark where deptcode = 'CS' and cnum = 240 and grade >= ((select grade from mark where deptcode = 'CS' and cnum = 240 order by grade desc limit 1) - 3)
  38. );
  39. /*
  40. The number and name of students who have completed two years, who
  41. have a final grade of at least 85 in every computer science course that
  42. they have taken, and who have always been taught by a professor in the
  43. computer science (CS) department.
  44. */
  45. select snum, sname from student where year > 2 and snum not in
  46. (
  47. select snum from mark where deptcode = 'CS' and grade < 85
  48. ) and snum not in
  49. (
  50. select e.snum from enrollment e where exists
  51. (
  52. select * from class c where
  53. c.deptcode = e.deptcode and c.cnum = e.cnum and c.term = e.term and c.section = e.section and c.pnum not in
  54. (
  55. select pnum from professor where deptcode != 'CS'
  56. )
  57. )
  58. );
  59. /*
  60. A sorted list of all departments who do not have a professor currently
  61. teaching a course offered by a different department.
  62. */
  63. select deptname from department where deptcode not in
  64. (
  65. select p.deptcode from professor p where pnum in
  66. (
  67. select pnum from class where term = 1185 and deptcode != p.deptcode and pnum = p.pnum
  68. )
  69. ) order by deptname;
  70. /*
  71. For each pair of classes for the same course that were taught in the same
  72. term, and that where also taught by different professors: the minimum
  73. grades obtained and the maximum grades obtained. In addition to these
  74. four values, each result should include the number and name of each professor,
  75. as well as the identifying attributes for each class.
  76. */
  77. with combineTable (grade, pname, pnum, deptcode, cnum, term, section) as
  78. (select m.grade as grade, p.pname as pname, p.pnum as pnum,
  79. cl.deptcode as deptcode, cl.cnum as cnum, cl.term as term, cl.section as section
  80. from mark m, enrollment e, class cl, course co, professor p
  81. where m.snum = e.snum and m.deptcode = e.deptcode and m.cnum = e.cnum and m.term = e.term and m.section = e.section
  82. and
  83. cl.deptcode = e.deptcode and cl.cnum = e.cnum and cl.term = e.term and cl.section = e.section
  84. and
  85. cl.deptcode = co.deptcode and cl.cnum = co.cnum
  86. and
  87. cl.pnum = p.pnum)
  88. select
  89. (select grade from mark where deptcode = c1.deptcode and cnum = c1.cnum and term = c1.term and section = c1.section order by grade desc limit 1)
  90. ,
  91. (select grade from mark where deptcode = c1.deptcode and cnum = c1.cnum and term = c1.term and section = c1.section order by grade asc limit 1)
  92. ,
  93. (select grade from mark where deptcode = c2.deptcode and cnum = c2.cnum and term = c2.term and section = c2.section order by grade desc limit 1)
  94. ,
  95. (select grade from mark where deptcode = c2.deptcode and cnum = c2.cnum and term = c2.term and section = c2.section order by grade asc limit 1),
  96. c1.cnum, c1.term, c1.pnum, c1.pname, c1.deptcode, c1.section, c1.pnum,
  97. c2.cnum, c2.term, c2.pnum, c2.pname, c2.deptcode, c2.section, c2.pnum
  98. from combineTable c1, combineTable c2
  99. where c1.term = c2.term and c1.deptcode = c2.deptcode and c1.cnum = c2.cnum and c1.pnum != c2.pnum;
  100. /*
  101. Pairs of distinct professors such that whenever the first one teaches a class
  102. in a particular term the second also teaches a class for the same course
  103. in the same term. Report a professor number and name for both the
  104. professors.
  105. */
  106. select p1.pnum, p1.pname, p2.pnum, p2.pname from professor p1, professor p2
  107. where p1.pnum != p2.pnum and not exists
  108. (
  109. select * from class c, class c2 where
  110. c.pnum = p1.pnum and c2.pnum = p2.pnum and (
  111. c.deptcode != c2.deptcode or
  112. c.term != c2.term or
  113. c.cnum != c2.cnum)
  114. );
  115. -- NOW CAN USE AGGREGATION
  116. /*
  117. The course number and total enrollment count for all of its classes of each
  118. course. Also, include only those course numbers for courses with a total
  119. enrollment count among the three lowest such counts.
  120. */
  121. -- Not sure if this will return an error, or 3 rows per old.deptcode, old.cnum since the 3rd parameter (the subquery) has 3 rows
  122. -- didn't break running on an empty dataset
  123. select old.deptcode, old.cnum, (select count(*) from enrollment where deptcode = old.deptcode and cnum = old.cnum group by deptcode, cnum order by count(*) asc limit 3) from class old;
  124. /*
  125. The percentage of professors in pure math who have always taught no
  126. more than a single course in any given term. (Note that a percentage
  127. should be a number between 0 and 100.)
  128. */
  129. select count(*) from professor p where deptcode = 'PM' and pnum in
  130. (
  131. select c.pnum from class c where c.pnum = p.pnum and c.pnum not in
  132. (
  133. select c1.pnum from class c1 where pnum = p.pnum and c1.term = c.term and (c1.deptcode != c.deptcode or c1.cnum != c.cnum)
  134. )
  135. )
  136. / (select count(*) from professor where deptcode = 'PM');
  137. /*
  138. The number of different third or fourth year students in each section of
  139. each course taught by a pure math professor in past terms. The result
  140. should include the professor number, professor name, course number and
  141. section, and should also be sorted first by the name of the professor,
  142. then by the professor number, third by the course number, and finally by
  143. section. (Note that a section is identified by a term and a section number.
  144. Also assume that sorting by section means sorting by term and then by
  145. section number. The result will therefore have a total of six columns.)
  146. */
  147. -- ran on empty dataset
  148. select (select pname from professor where pnum = c.pnum), c.pnum, c.cnum, c.term, c.section,
  149. (select count(distinct snum) from mark where snum in
  150. (
  151. select snum from student where year between 3 and 4
  152. ) and deptcode = c.deptcode and cnum = c.cnum and term = c.term and section = c.section)
  153. from class c where pnum in
  154. (
  155. select pnum from professor where deptcode = 'PM'
  156. ) and term != 1185;
  157. /*
  158. The ratio of professors in pure math (PM) to professors in applied math
  159. (AM) who have taught a class in which the average grade obtained in the
  160. class was greater than 77.
  161. */
  162. select count(*) from professor p where deptcode = 'PM' and pnum in
  163. (
  164. (select c.pnum from class c where
  165. (
  166. select avg(grade) from mark where c.deptcode = deptcode and c.cnum = cnum and c.term = term and c.section = section
  167. ) > 77)
  168. )
  169. /
  170. (select count(*) from professor p where deptcode = 'AM' and pnum in
  171. (
  172. select c.pnum from class c where
  173. (
  174. select avg(grade) from mark where c.deptcode = deptcode and c.cnum = cnum and c.term = term and c.section = section
  175. ) > 77
  176. ));
  177. /*
  178. For the current term, report how many courses there are in the schedule
  179. with a particular number of classes. For example an output
  180. {[5, 1], [4, 2], [1, 5]}
  181. indicates that there are 5 courses with a single class (section), 4 courses
  182. with 2 classes, and 1 course with 5 classes scheduled in the curent term.
  183. */
  184. -- ran on empty dataset
  185. with eachclass (sectioncount) as
  186. (select count(*) from class
  187. where term = 1185
  188. group by (deptcode, cnum))
  189. select count(*), sectioncount from eachclass group by sectioncount;