Advertisement
Tusohian

Question Answer of SQL

Jul 26th, 2018
2,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.52 KB | None | 0 0
  1. a/ Find the titles OF courses IN the Comp. Sci. department that have 3
  2. Credits.
  3. ---------------------------------
  4. SELECT title FROM COURSE
  5. WHERE credits=3 AND dept_name='Comp. Sci.'
  6.  
  7.  
  8.  
  9. b/Find the IDs OF ALL students who were taught BY an instructor named
  10. Einstein; make sure there are no duplicates IN the RESULT. 
  11. ----------------------------------------------
  12. WITH a(c,s,se,y) AS
  13. (SELECT course_id, sec_id, semester, YEAR FROM teaches
  14. WHERE ID = (SELECT ID FROM instructor WHERE name='Einstein'))
  15. SELECT id FROM takes,a WHERE course_id=c AND sec_id=s AND YEAR=y AND semester=se
  16.  
  17.  
  18. ----
  19. OR
  20. ----
  21.  
  22.  
  23. SELECT b.id
  24. FROM instructor, takes b, teaches a
  25. WHERE name='Einstein' AND instructor.id=a.id AND a.semester=b.semester AND a.YEAR=b.YEAR AND a.course_id=b.course_id
  26.  
  27.  
  28.  
  29. c/ Find the highest salary OF any instructor.  
  30. -----------------------------------------------
  31. SELECT MAX(salary) FROM instructor
  32.  
  33.  
  34.  
  35. d/Find ALL instructors earning the highest salary (there may be more
  36. than one WITH the same salary).
  37. -------------------------------------------
  38. SELECT salary, name FROM instructor
  39. WHERE salary = (SELECT MAX(salary) FROM instructor)
  40.  
  41.  
  42.  
  43. e/Find the enrollment OF each SECTION that was offered IN Fall 2009.
  44. ----------------------------------------------------------------
  45. SELECT sec_id, COUNT(sec_id) FROM SECTION NATURAL JOIN takes
  46. WHERE semester='Fall' AND YEAR=2009
  47. GROUP BY sec_id
  48.  
  49.  
  50.  
  51. f/ Find the maximum enrollment, across ALL sections, IN Fall 2009.
  52. --------------------------------------------------------------------
  53. SELECT MAX(enrollment)
  54. FROM (SELECT COUNT(ID) AS enrollment
  55. FROM SECTION NATURAL JOIN takes
  56. WHERE semester = 'Fall' AND YEAR = 2009
  57. GROUP BY sec_id)
  58.  
  59.  
  60.  
  61.  
  62. g/Find the sections that had the maximum enrollment IN Fall 2009
  63. ----------------------------------------
  64. WITH sec_enrollment AS (
  65. SELECT sec_id, COUNT(id) AS enrollment FROM SECTION NATURAL JOIN takes
  66. WHERE semester = 'Fall' AND YEAR = 2009
  67. GROUP BY sec_id)
  68. SELECT sec_id FROM sec_enrollment
  69. WHERE enrollment = (SELECT MAX(enrollment) FROM sec_enrollment)
  70.  
  71.  
  72. ----
  73. OR
  74. ----
  75.  
  76.  
  77. WITH a(m) AS
  78. (SELECT MAX(c) FROM
  79. (SELECT COUNT(id) AS c, sec_id FROM takes
  80. WHERE semester='Fall' AND YEAR=2009
  81. GROUP BY sec_id, course_id)),
  82. b(d,e,f) AS
  83. (SELECT COUNT(ID) AS c, sec_id, course_id FROM takes
  84. WHERE semester='Fall' AND YEAR=2009
  85. GROUP BY sec_id,course_id)
  86. SELECT e,f
  87. FROM a,b
  88. WHERE m=d
  89.  
  90.  
  91.  
  92.  
  93. h/Find the names OF ALL students who have taken at least one Comp. Sci.
  94. course; make sure there are no duplicate names IN the RESULT.
  95. ---------------------------------------------------------
  96. SELECT DISTINCT name FROM student NATURAL JOIN takes,course
  97. WHERE course.dept_name='Comp. Sci.' AND tot_cred>0
  98.  
  99.  
  100.  
  101.  
  102. i/Find the IDs AND names OF ALL students who have NOT taken any course
  103. offering BEFORE Spring 2009.
  104. -------------------------------------------------
  105. SELECT id, name FROM student NATURAL JOIN takes
  106. INTERSECT
  107. SELECT id, name FROM student NATURAL JOIN takes
  108. WHERE year>=2009
  109.  
  110.  
  111.  
  112. j/FOR each department, find the maximum salary OF instructors IN that
  113. department. You may assume that every department has at least one
  114. instructor.
  115. -------------------------------------------------
  116. SELECT dept_name, MAX(salary) FROM instructor
  117. GROUP BY dept_name
  118.  
  119.  
  120. K/Find the lowest, across ALL departments, OF the per-department maximum
  121. salary computed BY the preceding query.
  122. --------------------------------------------------
  123. SELECT MIN(maximum_salary) FROM (SELECT dept_name, MAX(salary) maximum_salary FROM instructor
  124. GROUP BY dept_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement