Midsem Soln
Midsem Soln
Important
1. Write your answers in the space provided for each question in the answer
sheet. Write your roll number at the top of every page.
Note: For Q1 and Q2 refer to the university database schema shown below. The primary
key is underlined for each relation.
instructor ( ID , name , dept name , s a l a r y )
student ( ID , name , dept name , t o t c r e d )
department ( dept name , b u i l d i n g , budget )
course ( course id , t i t l e , dept name , c r e d i t s )
teaches ( ID, course id, sec id, semester, year )
takes ( ID, course id, sec id, semester, year , grade )
prereq ( course id, prereq id )
Solution:
(b) Find the ID and name of each student who has taken a course in the CSE department
in 2023.
(5)
CS 245 Midsem (Cont.) Winter 2023-24 Page 2 of 4
Solution:
Note: The second join cannot be a natural join, because this will return the
details of students only in the CSE department, due to the common attribute
dept name in the schemas student and course.
(c) Find the ID and name of each instructor who taught CS 101 in 2020.
(5)
Solution:
Solution:
SELECT s . ID , s . name
FROM student s
WHERE NOT EXISTS (
SELECT p . c o u r s e i d
FROM prereq p
WHERE p . p r e r e q i d NOT IN (
SELECT t . c o u r s e i d
FROM takes t
WHERE t . ID = s . ID
)
AND p . c o u r s e i d = ’CS 2 4 5 ’
)
CS 245 Midsem (Cont.) Winter 2023-24 Page 3 of 4
Note: We cannot write this query using ’= all’ and its variants. See slide
number 46 from ”intro to SQL”.
(b) Find all departments whose average salary is greater than the average salary in CS.
(7)
Solution:
s e l e c t dept name
from ( s e l e c t dept−name , avg ( s a l a r y ) as a v g s a l
from instructor
group by dept name )
where a v g s a l > ( s e l e c t avg ( s a l a r y ) as c s a v g
from instructor
where dept name = ’CS ’ )
(c) List the department names along with the number of instructors in the department
who earn more than 100,000.
(6)
Solution:
s e l e c t dept name , count ( h i s a l a r y )
from ( s e l e c t dept name , s a l a r y as h i s a l a r y
from instructor
where s a l a r y > 100000)
group by dept name
3. (a) List all nontrivial functional dependencies α → β satisfied by the relation r(A, B, C)
shown below with α ∩ β = ∅.
A B C
a1 b 1 c 1
a1 b 1 c 2
a2 b 1 c 1
a2 b 1 c 3
(5)
CS 245 Midsem (Cont.) Winter 2023-24 Page 4 of 4
Solution:
A→B
C→B
AC → B
AB
BC
BD
(c) Consider the decomposition of the above schema R in part (b) into R1 = (A, B, C)
and R2 = (A, D). Is this a lossless decomposition with respect to the given func-
tional dependencies? Justify your answer. (5)
Solution: This is not a lossless decomposition since R1 ∩R2 = {A}, and neither
A → R1 nor A → R2 is in the closure of the given set of dependencies.