Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 6.47 KB | None | 0 0
  1. CSCE 2501                Design OF SQL Queries            Dr. KHALIL
  2. Consider the given relational DATABASE schema FOR a university application. The PRIMARY KEY OF each relation IS underlined.
  3. STUDENT (StudentId, Fname, Lname, Gender, Bdate, Class)
  4. DEPARTMENT (DeptCode, DeptName)
  5. STUDENT_MAJOR (StudentId, MajorDeptCode)
  6. STUDENT_MINOR (StudentId, MinorDeptCode)
  7. INSTRUCTOR (InstId, Fname, Lname, STATUS, DeptCode, Salary)
  8. COURSE (DeptCode, CourseNo, CourseName, CreditHours)
  9. PREREQUISITE (DeptCode, CourseNo, PreDeptCode, PreCourseNo)
  10. SECTION (DeptCode, CourseNo, SectionINo, Semester, YEAR, MW/UTR, TIME, InstId)
  11. TRANSCRIPT(StudentId, DeptCode, CourseNo, SectionNo, Semester, YEAR, Grade)
  12.  
  13. 1.  WRITE one UPDATE statement TO increase salaries OF CSCE instructors BY 10%, ECNG instructors BY 15% AND MENG instructors BY 5% (CSCE, EENG AND MENG are departments’ codes)
  14. UPDATE instructor
  15. SET Salary = CASE
  16.     WHEN deptCode = 'CSCE' THEN Salary * 1.10
  17.     WHEN deptCode = 'ECNG' THEN Salary * 1.15
  18.     WHEN deptCode = 'MENG' THEN Salary * 1.05
  19.     ELSE NULL
  20. END
  21. WHERE deptCode IN ('CSCE', 'ECNG', 'MENG');
  22.  
  23.  
  24. 2.  Display the Salary OF each Instructor after a raise OF 10%.
  25. SELECT salary*1.1
  26. FROM instructor;
  27.  
  28.  
  29. 3.  Generate a list OF ALL CSCE AND CSCI students. The list should contain the student Id, FULL name, AND major. The should be ordered BY major (ASC) AND gender (DESC).
  30. CREATE VIEW CompMajors (S_ID, SM_DCode, S_Fname, S_Lname, S_Gender)
  31. AS (
  32.     SELECT S.studentId, SM.majorDeptCode, S.Fname, S.Lname, S.Gender
  33.     FROM student S INNER JOIN Student_Major SM
  34.     ON S.studentId = SM.studentId
  35. );
  36.    
  37. -----
  38.  
  39. SELECT *
  40. FROM CompMajors
  41. WHERE SM_DCode = 'CSCE' OR SM_DCode = 'CSCI'
  42. ORDER BY SM_DCode ASC, S_Gender DESC;
  43.  
  44.  
  45. 4.  GET ALL senior students (Class=4) ordered ascendengly according TO student LAST name.
  46. SELECT *
  47. FROM student S
  48. WHERE S.Class = 4
  49. ORDER BY S.Lname;
  50.  
  51.  
  52. 5.  Display the average instructor salary FOR each department.
  53. SELECT I.deptCode, AVG(I.Salary)
  54. FROM instructor I INNER JOIN department D
  55. ON I.deptCode = D.deptCode
  56. GROUP BY I.deptCode;
  57.  
  58. 6.  GET list OF ALL junior, senior AND graduating senior CSCE students (classes  3, 4, AND 5)
  59. SELECT *
  60. FROM student S INNER JOIN Student_Major SM
  61. ON S.studentId = SM.studentId
  62. WHERE (S.Class = 3 OR S.Class = 4 OR S.Class = 5) AND SM.majorDeptCode = 'CSCE';
  63.  
  64. 7.  GET Id AND name OF ALL students whose birthdate IS missing.
  65. SELECT studentId, Fname, Lname
  66. FROM student
  67. WHERE Bdate IS NULL;
  68.  
  69. 8.  GET Id AND name OF ALL students who were born IN 1999.
  70. SELECT studentId, Fname, Lname
  71. FROM student
  72. WHERE Bdate BETWEEN '1999-01-01' AND '1999-12-31';
  73.  
  74. 9.  GET a list OF ALL senior students whose FIRST name contains ‘sh’.
  75. SELECT *
  76. FROM student
  77. WHERE Class = 4 AND (Fname LIKE '%sh%' OR  Fname LIKE '%sh' OR  Fname LIKE 'sh%');
  78.  
  79. 10. GET a list OF ALL senior students whose FIRST names START WITH ‘Moh’ AND LAST names END BY ‘sh’.
  80. SELECT *
  81. FROM student
  82. WHERE Class = 4 AND Fname LIKE 'Moh%' AND Lname LIKE '%sh';
  83.  
  84. 11. GET Id AND name OF ALL students who were born IN January.
  85. SELECT studentId, Fname, Lname, Bdate
  86. FROM student
  87. WHERE Bdate LIKE '_____01___';
  88.  
  89.  
  90. 12. GET Id AND name OF ALL students who were born ON the 15th OF March.
  91. SELECT studentId, Fname, Lname, Bdate
  92. FROM student
  93. WHERE Bdate LIKE '_____03-15';
  94.  
  95. 13. Define a VIEW TO GET age OF each VIEW. USE the defined VIEW TO GET a list OF ALL male CSCE students whose age exceeded 21 years.
  96. 14. Display ALL CSCE sections OF Fall 2019 (CSCE IS a department code)
  97. SELECT *
  98. FROM SECTION s
  99. WHERE s.deptCode = 'CSCE' AND s.Semester = 'Fall' AND s.YEAR = 2019;
  100.  
  101. 15. GET a list OF ALL sections OF Spring 2020 which are NOT assigned yet an Instructor.
  102. SELECT *
  103. FROM SECTION s
  104. WHERE s.InstId IS NULL AND s.Semester = 'Spring' AND s.YEAR = 2020;
  105.  
  106. 16. GET list OF ALL junior, senior AND graduating senior Computer Engineering students (classes  3, 4, AND 5)
  107. SELECT s.studentId, Fname, Lname, Gender, Bdate, Class, deptName
  108. FROM student S INNER JOIN Student_Major SM
  109. ON S.studentId = SM.studentId
  110. INNER JOIN department d
  111. ON d.deptCode = SM.majorDeptCode
  112. WHERE (S.Class = 3 OR S.Class = 4 OR S.Class = 5) AND d.deptName = 'Computer Engineering';
  113.  
  114. 17. GET a list OF ALL Computer Engineering students HAVING more than one minor.
  115. 18. GET the total NUMBER OF students IN each major.
  116. SELECT COUNT(*), majorDeptCode
  117. FROM Student_Major
  118. GROUP BY majorDeptCode;
  119.  
  120. 19. GET a list OF ALL majors HAVING no students.
  121. SELECT majorDeptCode
  122. FROM Student_Major
  123. GROUP BY majorDeptCode
  124. HAVING COUNT(*) = 0;
  125.  
  126. 20. GET a list OF ALL Electronics Engineering students HAVING DOUBLE major.
  127. SELECT S.studentId, COUNT(*) AS DOUBLE_MAJORS
  128. FROM Student_Major SM INNER JOIN student S
  129. ON S.studentId = SM.studentId
  130. GROUP BY S.studentId
  131. HAVING COUNT(s.studentId) = 2;
  132.  
  133. 21. Define a VIEW TO include the schedule OF each instructor IN each semester. USE the defined VIEW TO dispay the teaching schedule OF Dr. Khalil FOR the Fall 2019 semester. Also, USE the same defined VIEW TO GET a list OF ALL sections that are taught BY “Khalil” IN 2018, AND 2019.
  134. 22. GET a list OF ALL students HAVING DOUBLE major IN Computer Engineering AND Electronics Engineering.
  135. 23. GET a list OF ALL courses HAVING no prerequisite courses.
  136. 24. Retrieve names OF ALL Computer Science Faculty who are NOT teaching any course IN Fall 2019. WRITE the query USING: a) OUTER JOIN b) Subquery WITHINORNOT IN” operator c) Subquery query USINGEXISTSORNOT EXISTS” d) SET operator “Minus”
  137. 25. Retrieve names OF ALL majors HAVING a NUMBER OF students less than 5.
  138. 26. Define a VIEW TO include the total NUMBER OF students IN each grade FOR each SECTION IN years 2018 AND 2019. USE the VIEW TO GET the total NUMBER od students scoring an ‘A’ FOR ALL sections OF 2019.
  139. 27. FOR each course GET the course name AND the total NUMBER OF its prerequisite courses.
  140. 28. Define a VIEW TO include the total NUMBER OF taken courses AND the total NUMBER OF achieved credit hours OF each student IN each semester. USE the defined VIEW TO GET a list OF ALL graduating senior CSCE students WITH the total NUMBER OF achieved credit hours.
  141. 29. FOR each SECTION IN 2017, 2018 AND 2019, display the SECTION identifying attributes, SECTION’s instructor name AND the total name OF students IN each grade.
  142. 30. GET each department that doesn’t have any faculty. WRITE the query USING: a) OUTER JOIN b) Subquery WITHINORNOT IN” operator c) Subquery query USINGEXISTSORNOT EXISTS” d) SET operator “Minus”.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement