0% found this document useful (0 votes)
5 views4 pages

Midsem Soln

The document outlines the Midsem Exam for CS 245, Database Management Systems at IIT Guwahati, including important instructions and the structure of the exam. It consists of three questions covering relational algebra, SQL queries, and functional dependencies, with a total of 50 marks. Students are required to write their answers in the provided answer sheets and follow specific guidelines for rough work.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Midsem Soln

The document outlines the Midsem Exam for CS 245, Database Management Systems at IIT Guwahati, including important instructions and the structure of the exam. It consists of three questions covering relational algebra, SQL queries, and functional dependencies, with a total of 50 marks. Students are required to write their answers in the provided answer sheets and follow specific guidelines for rough work.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS 245, Database Management Systems

Midsem Exam, Winter 2023-2024


Department of Computer Science and Engineering
IIT Guwahati
Time: Two hours

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.

2. A supplementary sheet is being provided for rough work. Do not attach


your rough work to the answer sheet. Return it separately.

3. This exam has 3 questions over 4 pages, with a total of 50 marks.

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 )

1. Write the following queries in relational algebra.


(a) Find the ID and name of each instructor in the Physics department who earns more
than some instructor in the CSE department.
(5)

Solution:

ΠR.ID,R.name (ρR (σdept name=’Physics’ (instructor)


▷◁R.salary > S.salary
ρS (σdept name=’CSE’ (instructor))

(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:

Πstudent.ID,student.name (σyear = ’2023’ (student ▷◁ takes)


▷◁takes.course ID = course.course ID
σdept name=’CSE’ (course))

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:

Πinstructor.ID,instructor.name (instructor ▷◁ σcourse ID=’CS 101’∧year = ’2020’ (teaches))

2. Specify in SQL each of following queries.


(a) Find the ID and name of each student who has taken all the prerequisite courses for
CS 245. Only the direct prerequisites need to be considered, and not the indirect
ones obtained by transitive closure.
(7)

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

(b) Consider a schema R = (A, B, C, D) with functional dependencies AB → C, C →


D and D → A. List all the candidate keys for R.
(5)

Solution: The candidate keys are

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.

You might also like