Midsem-Soln Dbms
Midsem-Soln Dbms
Note: For Q1 and Q2 refer to the tables shown in Figure 1 at the end of the paper. Note
that the tables extend from page 6 to page 9 and some of the tables span two pages.
The list of tables is as follows: instructor, student, department, course, teaches,
takes, advisor and prereq. Their meaning is the same as in the lectures.
1. First describe in English each of following relational algebra queries. Then write down
the relation returned by each query for the tables in Figure 1.
(a)
Πdept name (instructor
−
ΠID,name,dept name,instructor.salary (instructor
./instructor.salary > R.salary ρR (Πsalary (instructor))))
(5)
Solution: Find the departments which instructors with the least salary belong
to.
dept name
Music
(b)
Πstudent.ID,student.name (student
./ takes
./ Πcourse id,sec id,semester,year (σname=”Einstein” (instructor ./ teaches)))
(5)
Solution: Find the IDs and names of students who have taken a course section
taught by Einstein.
ID name
44553 Peltier
(c)
ΠID,name (instructor
./ID=R.i ID
ΠR.i ID (ρR (advisor) ./R.i ID = S.i ID ∧ R.s ID 6= S.s ID (ρS (advisor))))
CS 245 Midsem (Cont.) Winter 2022-23 Page 2 of 9
(5)
Solution: Find the IDs and names of instructors who are advisors for more
than one student.
ID name
22222 Einstein
98345 Kim
45565 Katz
(d)
ΠID,name (student
./student.ID=takes.ID takes
./takes.course ID=course.course ID (σdept name = ”Biology” (course)))
(5)
Solution: Find the IDs and names of students who have taken at least one course
offered by the Biology Department.
ID name
98988 Tanaka
2. First describe in English each of following SQL queries. Then write down the relation
returned by each query for the tables in Figure 1.
(a)
s e l e c t dept name
from instructor
where s a l a r y > ( s e l e c t avg ( s a l a r y )
from instructor
where dept name = ’Comp . S c i . ’ )
(5)
Solution: Find all departments which have at least one instructor with salary
greater than the average salary in the CS department.
CS 245 Midsem (Cont.) Winter 2022-23 Page 3 of 9
dept name
Finance
Physics
Physics
Finance
Comp. Sci.
Elec. Eng.
(b)
s e l e c t dept name , count ( d i s t i n c t ID ) as i n s t r c o u n t
from instructor n a t u r a l j o i n teaches
where s e m e s t e r =’ Spring ’ and y e a r =2010
group by dept name ;
(5)
(c)
s e l e c t dept name , count ( d i s t i n c t ID ) from instructor
where s a l a r y > 50000
group by dept name ;
(5)
Solution: List the department names along with the number of faculty in the
department who earn more than 50000.
CS 245 Midsem (Cont.) Winter 2022-23 Page 4 of 9
(d)
s e l e c t count ( d i s t i n c t ID )
from takes
where ( c o u r s e i d , s e c i d , s e m e s t e r , y e a r ) i n
( s e l e c t course id , s e c i d , semester , year
from teaches
where teaches . ID= 1 0 1 0 1 ) ;
(5)
Solution: Find the total number of distinct students who have taken course
sections taught by the instructor with ID 10101.
count(distinct ID)
6
3. Consider the following set F of functional dependencies for the relation schema R =
(A, B, C, D, E):
A → BC
CD → E
B→D
E→A
(a) List all the functional dependencies in F + of the form α → β, where α has a single
attribute and α ∩ β = ∅. (You can write X → Y Z instead of X → Y and X → Z,
and so on.)
(5)
CS 245 Midsem (Cont.) Winter 2022-23 Page 5 of 9
(c) Consider the decomposition of the above schema R into R1 = (A, B, C) and R2 =
(A, D, E). Is this a lossless decomposition? Justify your answer.
(5)
4. Consider the relation schema R(A, B, C, D) with the set F of functional dependencies
AB → C
C→D
D→A
(a) Find all the BCNF violations, if any, for the schema R by the functional dependen-
cies in F + .
(5)
C→D
D→A
C→A
CD → A
AC → D
AC → AD
CD → AC
AC → CD
Note that no trivial dependency (i.e., α → β with β ⊆ α) can violate the BCNF
condition.
(b) Decompose the schema R, if necessary, into a collection of schemas each of which
is in BCNF. Write only the final answer. Do not show the intermediate steps.
(5)
Solution: The possible solutions are R = (R1 , R2 , R3 ) where the choices for
R1 , R2 , R3 are:
Note that a decomposition can be listed in any order. For this example, it can
be shown that any schema in the decomposition cannot have more than two
attributes.
CS 245 Midsem (Cont.) Winter 2022-23 Page 7 of 9
instructor
student
department
course
teaches
takes
advisor
s ID i ID
−−−−− −−−−−
00128 45565
12345 10101
23121 76543
44553 22222
CS 245 Midsem (Cont.) Winter 2022-23 Page 10 of 9
45678 22222
76543 45565
76653 98345
98765 98345
98988 76766
prereq
course id prereq id
−−−−−−−−− −−−−−−−−−
BIO−301 BIO−101
BIO−399 BIO−101
CS−190 CS−101
CS−315 CS−101
CS−319 CS−101
CS−347 CS−101
EE−181 PHY−101