You are on page 1of 4

1 UCLA Computer Science Department Fall 2001 Instr: C. Zaniolo TAs: P.

Michael

Student Name and ID:

CS143 MIDTERM EXAM: Closed Book, 2 Hours Attach extra pages as needed. Write your name and ID on the extra pages. Please, write neatly.

Problem 1.1 1.2 1.3 2.1 2.2 2.3 2.4 Total

Score (20%) (15%) (15%) (10%) (20%) (10%) (10%) (100%)

Extra Credit: Midterm Score:

CS143 Midterm, Fall 2001 Page: 2

Problem 1 Given a relation describing the courses taken by each student: Student(StudentID, Course, Grade) 1. (20 points) Write an SQL query to nd the ID of all students who (i) completed at least two classes, and (ii) took a grade of B or better in all classes he/she completed. (Therefore, students who got two As and two Cs would not satisfy this query. Also, the code for grades is numeric and the grade B is denoted by the number 3.0. Finally, you can assume that students only take a course once.) SELECT StudentID FROM Student AS S1 WHERE S1.StudentID NOT IN (SELECT S2.StudentID FROM Student AS S2 WHERE S2.Grade < 3.0) GROUP BY S1.StudentID HAVING count(S1*) >= 2. 2. (15 points) Write the same query in domain relational calculus (no aggregates!)

{< S > |

C1 , C2 , G1 , G2 (< S, C1 , G1 > Student < S, C2 , G2 > Student C1 = C2 ) C3 , G3 (< S, C3 , G3 > Student) G3 < 3.0 }

3. (15 points) Write the same query in relational algebra.

StudentID (StudentID=d.StudentIDCourse=d.Course (Student d (Student))) StudentID (StudentID<3.0 (Student))

CS143 Midterm, Fall 2001 Page: 3 Problem 2 We have 1 million employee tuples with unique key Eno. The length of each tuple is 50 bytes. These are stored as xed-length records in a le consisting of blocks of size 2048 bytes. On this le, we build a sparse index on Eno. The index is is organized as a B+ tree, where each key takes 18 bytes and each pointer takes 22 bytes (the leaf nodes are chained together as in the textbook). The B+ tree blocks contain 2048 bytes.

1. (10 points) How many blocks does the le use if there remains no empty record slot in the le blocks. 2048/50 = 40 records per block. 106 /40 = 25000. 2. (20 points) Compute the blocks used at each level of the B+ tree, for the best case and the worst case. N=51 pointers (50 at the leaf level.) Worst case: 25 at bottom level, 26 at other levels B+ tree, best case: Leaf level: sparse index. One pointer per block: 25.000/50 = 500 Next Level: 10 The root:1 B+ tree, worst Case: Leaf nodes: 25.000/25 = 1000 Next Level: 1000/26 = 38.4 take the FLOOR (not the ceiling). 38! Next Level: 38/26. Cannot split in two blocks. This is the root! 3. (10 points) Is this index a primary index or a secondary one? A sparse index can only be built on clustered data. Thus this is a primary index. 4. (10 points) A query asking for the records of all employees whose Eno falls in a certain range returns 800 records. Estimate the number of pages accessed to retrieve this query. Primary indexes are clustered. The le records must be sorted on the key. 800 take 2022 blocks. 3 blocks of index: 3+22=25.

CS143 Midterm, Fall 2001 Page: 4 Extra Credit Problem Given the following four operators: union, selection, projection, cartesian product. 1. (5 points) Can you express set intersection r(A, B ) s(A, B ) using these four operators? Intersection on compatible relations can be expressed as an equijoin on all columns:

r(A, B ) s(A, B ) = r.A=s.Ar.B =s.B (r s)


2. (5 points) Can you express set dierence r(A, B ) s(A, B ) using these four operators? Your answers should either express intersection/dierence using these operators or explain why such an expression does not exist. Those four operators are monotonic. Expression of these operators are also monotonic. But set dierence is not monotonic. Thus set dierence cannot be expressed using those four operators.

You might also like