Sample Midterm
Sample Midterm
• Good luck!
Name:
NEU ID (optional):
1
1 Multiple-Choice Questions (40 points)
Circle ALL the correct choices: there may be more than one correct choice, but there is always
at least one correct choice. NO partial credit: the set of all the correct answers must be checked.
There are 10 multiple choice questions worth 4 points each.
1. Which of the following describes the role of a Data Definition Language (DDL) in a DBMS?
2. The following ERD describes a relation between employees, managers, and departments:
3. Suppose that S(A, ...) is a relation containing m rows and T (B, ...) is a relation containing
n rows. Assume that A is the primary key of S and B is a foreign key reference to S(A).
Suppose that we perform an inner join between S and T on S.A = T.B. The maximum
number of rows that can be in the output is:
(a) m
(b) n
(c) m + n
(d) mn
(e) max(m, n)
2
INSERT INTO s (a , b ) VALUES (1 ,2);
INSERT INTO s (a , b ) VALUES (1 ,4);
INSERT INTO s (a , b ) VALUES (3 ,4);
INSERT INTO r (a ,b , c ) VALUES (1 ,2 ,10);
INSERT INTO r (a ,b , c ) VALUES (3 ,3 ,3);
INSERT INTO r (a ,b , c ) VALUES (1 ,7 ,3);
INSERT INTO r (a ,b , c ) VALUES (1 ,2 ,8);
INSERT INTO r (a ,b , c ) VALUES (3 ,3 ,5);
INSERT INTO r (a ,b , c ) VALUES (1 ,1 ,10);
INSERT INTO r (a ,b , c ) VALUES (2 ,2 ,10);
(a) 1
(b) 3
(c) 5
(d) 10
You are asked to find the entire lineage (all superiors) of employee with id 100 up to the
highest level in the organization. Which SQL statement will retrieve this information?
3
(b) WITH RecursiveCTE AS (
SELECT EmployeeID , FirstName , LastName , ManagerID
FROM Employees
WHERE EmployeeID = 100
UNION ALL
SELECT e . EmployeeID , e . FirstName , e . LastName , e . ManagerID
FROM Employees e
JOIN RecursiveCTE r ON e . ManagerID = r . EmployeeID
)
SELECT FirstName , LastName FROM RecursiveCTE ;
4
8. What normal form the following table is in?
(a) 1NF
(b) 2NF
(c) 3NF
(d) BCNF
10. What is the purpose of a cursor object when working with databases in Python?
5
2 ERD (15 points)
Design a system for movie rentals that manages movie collections, customer rentals, payments, and
employee interactions. Draw an ERD using the provided entities and their relationships. Mark
primary keys, and indicate the cardinality and participation (total/partial) of relationships.
System description:
• A director has an id, first name, last name, and birth date.
• Each movie has one director, but a director can direct multiple movies.
• A customer has id, first name, last name, address, and email.
• A movie can be rented multiple times by different customers at different times. For each
rental, the system stores the rental date, due date, and return date.
• Each rental is associated with a payment. A payment has amount and date.
• An employee has id, first name, last name, and hire date.
• A rental can be processed by only one employee, but an employee can process multiple rentals.
6
3 Data Normalization (15 points)
Consider the relational schema R(A, B, C, D, E, G) with the functional dependencies:
F = {AB → C, C → A, BC → D, ACD → B, D → EG, BE → C, CG → BD, CE → AG}.
7
4 SQL (30 points)
You are given the following schemas of a database for a coaching club.
• The relation coach contains data on coaches that work in the club. Each coach has an id,
name, e-mail address, date of starting his/her job as a coach (from_date) and hourly rate.
• The relation types contains data on the coaching types offered in the club, including the type
name (e.g., ”life coaching”, ”career coaching”, etc.) and description.
• The relation coaches describes which coaching types are offered by each coach. Each coaching
type has at least one coach who offers it.
• The relation clients contains data on clients of the club. Each client has an id, name,
address and mobile phone. Each client has at least one training program.
• The relation training_program contains data on the training programs of the clients. For
each client, it stores the starting date of the program (start_date), id of the coach, the
coaching type (type_name), and the number of total hours planned for the program. The
cost of the program is calculated based on the number of hours and the hourly rate of the
selected coach.
1. Find all the clients that have been trained in a coaching type whose description contains the
word ”life”.
2. Find pairs of different clients who started their training at the same day with the same coach.
The result columns should include the ids of the customers and the starting date of the
training. Each such pair should appear only once in the result.
8
3. Find all the clients who have never been trained by a coach named Levi.
4. Find for each client the total amount he/she has to pay for all his/her training programs.
5. Find customers who have been trained in all the coaching types offered by the club.
6. Find customers who have had at least 3 different training programs, and all their training
programs have been carried out by the same coach.