a2.sql 7.4 KB

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