Instruction Table

  1. name sql
  2. classroom
  3. CREATE TABLE classroom
  4. (
  5. building varchar(15),
  6. room_number varchar(7),
  7. capacity numeric(4,0),
  8. primary key (building, room_number)
  9. )
  10. department
  11. CREATE TABLE department
  12. (dept_name varchar(20),
  13. building varchar(15),
  14. budget numeric(12,2) check (budget > 0),
  15. primary key (dept_name)
  16. )
  17. course
  18. CREATE TABLE course
  19. (course_id varchar(8),
  20. title varchar(50),
  21. dept_name varchar(20),
  22. credits numeric(2,0) check (credits > 0),
  23. primary key (course_id),
  24. foreign key (dept_name) references department
  25. on delete set null
  26. )
  27. instructor
  28. CREATE TABLE instructor
  29. (ID varchar(5),
  30. name varchar(20) not null,
  31. dept_name varchar(20),
  32. salary numeric(8,2) check (salary > 29000),
  33. primary key (ID),
  34. foreign key (dept_name) references department
  35. on delete set null
  36. )
  37. section
  38. CREATE TABLE section
  39. (course_id varchar(8),
  40. sec_id varchar(8),
  41. semester varchar(6)
  42. check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),
  43. year numeric(4,0) check (year > 1701 and year < 2100),
  44. building varchar(15),
  45. room_number varchar(7),
  46. time_slot_id varchar(4),
  47. primary key (course_id, sec_id, semester, year),
  48. foreign key (course_id) references course
  49. on delete cascade,
  50. foreign key (building, room_number) references classroom
  51. on delete set null
  52. )
  53. teaches
  54. CREATE TABLE teaches
  55. (ID varchar(5),
  56. course_id varchar(8),
  57. sec_id varchar(8),
  58. semester varchar(6),
  59. year numeric(4,0),
  60. primary key (ID, course_id, sec_id, semester, year),
  61. foreign key (course_id,sec_id, semester, year) references section
  62. on delete cascade,
  63. foreign key (ID) references instructor
  64. on delete cascade
  65. )
  66. student
  67. CREATE TABLE student
  68. (ID varchar(5),
  69. name varchar(20) not null,
  70. dept_name varchar(20),
  71. tot_cred numeric(3,0) check (tot_cred >= 0),
  72. primary key (ID),
  73. foreign key (dept_name) references department
  74. on delete set null
  75. )
  76. takes
  77. CREATE TABLE takes
  78. (ID varchar(5),
  79. course_id varchar(8),
  80. sec_id varchar(8),
  81. semester varchar(6),
  82. year numeric(4,0),
  83. grade varchar(2),
  84. primary key (ID, course_id, sec_id, semester, year),
  85. foreign key (course_id,sec_id, semester, year) references section
  86. on delete cascade,
  87. foreign key (ID) references student
  88. on delete cascade
  89. )
  90. advisor
  91. CREATE TABLE advisor
  92. (s_ID varchar(5),
  93. i_ID varchar(5),
  94. primary key (s_ID),
  95. foreign key (i_ID) references instructor (ID)
  96. on delete set null,
  97. foreign key (s_ID) references student (ID)
  98. on delete cascade
  99. )
  100. time_slot
  101. CREATE TABLE time_slot
  102. (time_slot_id varchar(4),
  103. day varchar(1),
  104. start_hr numeric(2) check (start_hr >= 0 and start_hr < 24),
  105. start_min numeric(2) check (start_min >= 0 and start_min < 60),
  106. end_hr numeric(2) check (end_hr >= 0 and end_hr < 24),
  107. end_min numeric(2) check (end_min >= 0 and end_min < 60),
  108. primary key (time_slot_id, day, start_hr, start_min)
  109. )
  110. prereq
  111. CREATE TABLE prereq
  112. (course_id varchar(8),
  113. prereq_id varchar(8),
  114. primary key (course_id, prereq_id),
  115. foreign key (course_id) references course
  116. on delete cascade,
  117. foreign key (prereq_id) references course
  118. )

Exercise

  1. Quiz 1
  2. Write the following queries in SQL, using the university schema.
  3. a. Find the titles of courses in the Comp. Sci. department that have 3 credits.
  4. b. Find the IDs of all students who were taught by an instructor named Einstein; make sure there are no duplicates in the result.
  5. c. Find the highest salary of any instructor.
  6. d. Find all instructors earning the highest salary (there may be more than one with the same salary).
  7. e. Find the enrollment of each section that was offered in Fall 2017. (Change Mistake)
  8. f. Find the maximum enrollment, across all sections, in Fall 2017.(Change Mistake)
  9. g. Find the sections that had the maximum enrollment in Fall 2017.(Change Mistake)
  10. Quiz 2
  11. Write the following inserts, deletes or updates in SQL, using the university schema.
  12. a. Increase the salary of each instructor in the Comp. Sci. department by 10%.
  13. b. Delete all courses that have never been offered (that is, do not occur in the section relation).
  14. c. Insert every student whose tot cred attribute is greater than 100 as an instructor in the same department, with a salary of $10,000.

Exercise Answer

Question 1

a. Find the titles of courses in the Comp. Sci. department that have 3 credits.

  1. -- a. Find the titles of courses in
  2. -- the Comp. Sci. department that have 3 credits.
  3. select title, credits
  4. from course
  5. where dept_name = "Comp. Sci." and credits == 3
title credits
Robotics 3
Image Processing 3
Database System Concepts 3

b. Find the IDs of all students who were taught by an instructor named Einstein; make sure there are no duplicates in the result.

  1. -- b. Find the IDs of all students
  2. -- who were taught by an instructor named Einstein;
  3. -- make sure there are no duplicates in the result.
  4. select ID,course_id,grade
  5. from takes
  6. where (course_id, sec_id, semester, year) in
  7. (select course_id, sec_id, semester, year
  8. from teaches, instructor
  9. where teaches.ID = instructor.ID and name= "Einstein")
ID course_id grade
44553 PHY-101 B-

The correct answer (b)

  1. select distinct student.ID
  2. from (student join takes using(ID))
  3. join (instructor join teaches using (ID))
  4. using (course_id, sec_id, semester, year)
  5. where instructor.name = 'Einstein'

c. Find the highest salary of any instructor.

  1. -- c. Find the highest salary of any instructor.
  2. select max(salary) as MAX, name, dept_name
  3. from instructor
  4. group by dept_name
  5. select max(salary), name
  6. from instructor
MAX name dept_name
72000 Crick Biology
92000 Brandt Comp. Sci.
80000 Kim Elec. Eng.
90000 Wu Finance
62000 Califieri History
40000 Mozart Music
95000 Einstein Physics

d. Find all instructors earning the highest salary (there may be more than one with the same salary).

  1. -- d. Find all instructors earning
  2. -- the highest salary (there may be more than one with the same salary).
  3. select ID, name
  4. from instructor
  5. where salary = (
  6. select max(salary)
  7. from instructor
  8. )

The correct answer (d)

  1. select ID, name
  2. from instructor
  3. where salary = (select max(salary) from instructor)

e. Find the enrollment of each section that was offered in Fall 2017.

  1. -- e. Find the enrollment of each section that was offered in Autumn 2009.
  2. select *
  3. from section
  4. where (course_id,sec_id,semester,year) in
  5. (select course_id, sec_id, semester, year
  6. from takes
  7. where semester = "Fall" and year = "2017")

The correct answer (e)

  1. select course_id, sec_id, count(ID)
  2. from section natural join takes
  3. where semester = 'Fall'
  4. and year = '2017'
  5. group by course_id, sec_id

f. Find the maximum enrollment, across all sections, in Fall 2017.

  1. -- f. Find the maximum enrollment, across all sections, in Autumn 2009.
  2. select course_id, count(course_id) as Num
  3. from takes
  4. where semester = "Fall" and year = "2017"
  5. group by "course_id"
  6. order by course_id asc

The correct answer (f)

  1. select max(enrollment)
  2. from (select count(ID) as enrollment
  3. from section natural join takes
  4. where (semester = 'Fall'
  5. and year = '2017')
  6. group by course_id, sec_id)

g. Find the sections that had the maximum enrollment in Fall 2017.

  1. -- g. Find the sections that had the maximum enrollment in Autumn 2009.
  2. select course_id, count(course_id) as Num
  3. from takes
  4. where year = "2017" and semester = "Fall"
  5. group by "course_id"
  6. order by course_id asc

The correct answer (g)

  1. with sec_enrollment as (
  2. select course_id, sec_id, count(ID) as enrollment
  3. from section natural join takes
  4. where (semester = 'Fall'
  5. and year = '2017')
  6. group by course_id, sec_id)
  7. select course_id, sec_id
  8. from sec_enrollment
  9. where enrollment = (select max(enrollment) from sec_enrollment)

Question 2

Quiz 2
Write the following inserts, deletes or updates in SQL, using the university schema.

a. Increase the salary of each instructor in the Comp. Sci. department by 10%.

  1. -- a. Increase the salary of each instructor in the Comp. Sci. department by 10%.
  2. update instructor
  3. set salary = salary * 1.10
  4. where dept_name = 'Comp. Sci.'

b. Delete all courses that have never been offered (that is, do not occur in the section relation).

  1. -- b. Delete all courses that have never been offered
  2. -- (that is, do not occur in the section relation).
  3. delect from course
  4. where course_id not in (
  5. select course_id from section)

c. Insert every student whose tot_cred attribute is greater than 100 as an instructor in the same department, with a salary of $10,000.

  1. -- c. Insert every student whose tot cred attribute is greater than 100
  2. -- as an instructor in the same department, with a salary of $10,000.
  3. insert into instructor
  4. select ID, name, dept_name, 30000
  5. from student
  6. where tot_cred > 100
  7. -- Error >>
  8. -- Uncaught Error: CHECK constraint failed: instructor
  9. -- need to focus on constraint. (salary > 29000)