Midterm Exam #2 (Version B) CS 122A Spring 2018

Similar documents
Midterm Exam #2 (Version B) CS 122A Spring 2018

Midterm Exam #2 (Version C) CS 122A Spring 2018

Midterm Exam #2 (Version A) CS 122A Winter 2017

Midterm Exam (Version B) CS 122A Spring 2017

Endterm Exam (Version B) CS 122A Spring 2017

Midterm Exam #1 (Version B) CS 122A Spring 2018

Midterm Exam #1 Version A CS 122A Winter 2017

Midterm Exam #1 Version B CS 122A Winter 2017

Spring 2013 CS 122C & CS 222 Midterm Exam (and Comprehensive Exam, Part I) (Max. Points: 100)

CSE 344 Midterm. Monday, November 9th, 2015, 9:30-10:20. Question Points Score Total: 70

1 (10) 2 (8) 3 (12) 4 (14) 5 (6) Total (50)

CIS 330: Applied Database Systems

CSE344 Midterm Exam Fall 2016

CSE414 Midterm Exam Spring 2018

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

SQL: Queries, Programming, Triggers

A7-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

CSE 344 Midterm. Monday, November 9th, 2015, 9:30-10:20. Question Points Score Total: 70

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

SQL Lab Query 5. More SQL Aggregate Queries, Null Values. SQL Lab Query 8. SQL Lab Query 5. SQL Lab Query 8. SQL Lab Query 9

SQL: Queries, Constraints, Triggers

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

SQL. Chapter 5 FROM WHERE

CS 222/122C Fall 2016, Midterm Exam

Midterm Examination CS 265 Spring 2015 Name: I will not use notes, other exams, or any source other than my own brain on this exam: (please sign)

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

CS222P Fall 2017, Final Exam

Mahathma Gandhi University

Basant Group of Institution

Midterm Exam. October 30th, :15-4:30. CS425 - Database Organization Results

CSE 344 Midterm. Wednesday, February 19, 2014, 14:30-15:20. Question Points Score Total: 100

Database Management Systems Paper Solution

Enterprise Database Systems

The SQL data-definition language (DDL) allows defining :

Keys, SQL, and Views CMPSCI 645

CSE 344 Midterm. Monday, Nov 4, 2013, 9:30-10:20. Question Points Score Total: 100

Midterm Review. Winter Lecture 13

Database Systems ( 資料庫系統 )

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

CSE 344 Midterm Exam

CSE344 Midterm Exam Winter 2017

Midterm Examination CS 265 Spring 2015 Name: KEY Total on any question cannot exceed 5 and cannot be less than 0 JPK, CK, JDK

Integrity constraints, relationships. CS634 Lecture 2

Introduction to Data Management. Lecture #10 (Relational Calculus, Continued)

CSE 344 Midterm. Wednesday, February 19, 2014, 14:30-15:20. Question Points Score Total: 100

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables

University of California, Berkeley. (2 points for each row; 1 point given if part of the change in the row was correct)

Introduction to Data Management. Lecture #13 (Relational Calculus, Continued) It s time for another installment of...

Review: Where have we been?

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Announcements. Agenda. Database/Relation/Tuple. Discussion. Schema. CSE 444: Database Internals. Room change: Lab 1 part 1 is due on Monday

CS 461: Database Systems. Final Review. Julia Stoyanovich

CompSci 516 Data Intensive Computing Systems

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

Announcements. Outline UNIQUE. (Inner) joins. (Inner) Joins. Database Systems CSE 414. WQ1 is posted to gradebook double check scores

(a) (14 points) Find all people who are friends with Clara Oswin Oswald both on Facebook and Twitter since Return their names.

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

Agenda. Discussion. Database/Relation/Tuple. Schema. Instance. CSE 444: Database Internals. Review Relational Model

Principles of Data Management

The SQL Query Language. Creating Relations in SQL. Adding and Deleting Tuples. Destroying and Alterating Relations. Conceptual Evaluation Strategy

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Introduction to Data Management. Lecture #11 (Relational Algebra)

CS W Introduction to Databases Spring Computer Science Department Columbia University

Database Systems. October 12, 2011 Lecture #5. Possessed Hands (U. of Tokyo)

Introduction to Database Systems CSE 414

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Informationslogistik Unit 4: The Relational Algebra

CS145 Final Examination

CSE 344 Introduction to Data Management. Section 2: More SQL

Introduction to Data Management. Lecture 14 (SQL: the Saga Continues...)

Informationslogistik Unit 5: Data Integrity & Functional Dependency

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Example Examination. Allocated Time: 100 minutes Maximum Points: 250

CSE 344 Midterm. Wednesday, Oct. 31st, 2018, 1:30-2:20. Question Points Score Total: 100

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

Outer Join, More on SQL Constraints

Basic operators: selection, projection, cross product, union, difference,

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Microsoft Querying Data with Transact-SQL - Performance Course

Database Systems CSE 414

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

Course No: 4411 Database Management Systems Fall 2008 Midterm exam

SQL - Data Query language

Lecture 2: Introduction to SQL

CSE 344 Final Examination

Midterm 1: CS186, Spring I. Storage: Disk, Files, Buffers [11 points] SOLUTION. cs186-

CSC 261/461 Database Systems Lecture 5. Fall 2017

CS317 File and Database Systems

Chapter 19 Query Optimization

Querying Data with Transact-SQL

1- a> [5pts] Create the new table by writing and executing a full SQL DDL statement based on the following schema:

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

Chapter 1 SQL and Data

Transcription:

NAME: SEAT NO.: STUDENT ID: Midterm Exam #2 (Version B) CS 122A Spring 2018 Max. Points: 100 (Please read the instructions carefully) Instructions: - The total time for the exam is 50 minutes; be sure to budget your time accordingly. - The exam is closed book and closed notes but open cheat sheet. - Read each question first, in its entirety, and then carefully answer each part of the question. - If you don t understand something, ask one of the exam proctors for clarification. - If you still find ambiguities in a question, note the interpretation you are taking. QUESTION TOPIC POINTS SCORE 1 How Queryous 20 2 Never Ending SQL 20 3 Name That Query 30 4 And Briefly 10 5 True, False, or Null? 20 TOTAL All 100

1

2 Question 1: How Queryous (20 points) (20 pts) Consider again the Hoofers Piloting Club database, and suppose (if needed or helpful) that the tables contain the data shown below. Pilot( pid, pname, rating, age) Reserves( pid, aid, date ) Airplanes( aid, aname, color) Write the following queries in the indicated language: (10 pts) Relational Algebra: Print the aids of airplanes that are reserved by a pilot with a rating of at least 5 as well as a pilot named Abby. (10 pts) SQL: For each pilot, print their pid, name, rating, and if they have any red airplanes reserved the reservation details (date, aid, and aname).

3 Question 2: Never Ending SQL (20 points) And again! Just when you thought it was safe. For this problem, you are to assume that the Reserves table has been created with pid and aid each being defined as FOREIGN KEYs with the ON DELETE CASCADE option for their respective tables (Pilots and Airplanes). Note that a new table has just been added to the mix PlanePrefs which has no primary or foreign keys. PlanePrefs is initially empty and will soon be used by the club to assist them in advertising airplanes and other services to their club members. Pilots( pid, pname, rating, age) Reserves( pid, aid, date ) Airplanes( aid, aname, color) PlanePrefs(pid, date, aname, color) In addition, assume that the following additional SQL goodies have already been defined: DELIMITER $$ CREATE TRIGGER TrackActivity CREATE PROCEDURE DropPlane(oldplane INT(11)) AFTER INSERT ON Reserves FOR EACH ROW BEGIN BEGIN DELETE FROM Airplanes WHERE aid = oldplane; INSERT INTO PlanePrefs (pid, date, aname, color) END $$ VALUES( NEW.pid, NEW.date, (SELECT aname FROM Airplanes WHERE aid = NEW.aid), (SELECT color FROM Airplanes WHERE aid = NEW.aid)); END; $$ DELIMITER ; (a) (10 pts) Show below the result of running the following statement against the initial four tables above. (I.e., show the contents of the tables after execution. You may skip any tables that are unaffected.) CALL DropPlane(1);

4 Question 2: SQL Never Ending SQL ( continued ) Here s all of the database information once again. You are to assume that Reserves table was created with pid and aid each being defined as FOREIGN KEYs with the ON DELETE CASCADE option for their respective tables (Pilots and Airplanes). Note the new table PlanePrefs which has no primary or foreign keys. This new table, which they just added, is currently empty and will be used by the club to assist them in advertising airplanes and other services to their club members. Pilots( pid, pname, rating, age) Reserves( pid, aid, date ) Airplanes( aid, aname, color) PlanePrefs(pid, date, aname, color) In addition, assume that the following additional SQL goodies have already been defined: DELIMITER $$ CREATE TRIGGER TrackActivity CREATE PROCEDURE DropPlane(oldplane INT(11)) AFTER INSERT ON Reserves FOR EACH ROW BEGIN BEGIN DELETE FROM Airplanes WHERE aid = oldplane; INSERT INTO PlanePrefs (pid, date, aname, color) END $$ VALUES( NEW.pid, NEW.date, (SELECT aname FROM Airplanes WHERE aid = NEW.aid), (SELECT color FROM Airplanes WHERE aid = NEW.aid)); END; $$ DELIMITER ; (b) (5 pts) Show below the result of running the following statement starting from the initial four tables above. (I.e., show the contents of the tables after execution. You may skip any tables that are unaffected.) INSERT INTO Reserves(pid, aid, date) VALUES (3,2, 2018-05-30 ), (5,4, 2018-05-31 ); (c) (5 pts) Show below the results of executing the following statement against the initial tables above. CREATE VIEW AgeSummary( age, numpilots) AS SELECT age, COUNT(*) as numpilots FROM Pilots GROUP BY age ; SELECT * FROM AgeSummary A WHERE A.age >= 21;

5 Question 3: Name That Query (30 points) (30 pts) Consider the Hoofers Piloting Club database: Pilots( pid, pname, rating, age) Reserves( pid, aid, rdate ) Airplanes( aid, aname, color) What follows is a list of queries in various languages. For the first five queries, Q1-Q5, list all of the equivalent queries from the remainder of the list (Q6-Q16). Note that there may be several, or perhaps none, and that two queries are equivalent if their results are identical under all database states. Q1 (6 pts) Select the ids and names of pilots to whom alcohol can t be legally served at a UCI party;(proof of age 21 or over required) Equivalent queries : Q2 (6 pts) SELECT MAX(age) FROM Pilots; Equivalent queries: Q3 (6 pts) SELECT aid FROM Airplanes WHERE color = red AND color = purple ; Equivalent queries: Q4 (6 pts) { t(age) p Pilots (t.age = p.age (p.rating < 3)) } Equivalent queries: Q5 (6 pts) π aid,aname,color ((σ age=22 Pilots) (Reserves Airplanes)) Equivalent queries: Q6 π age (σ rating < 3 Pilots) Q7 SELECT * FROM Airplanes A WHERE A.aid IN (SELECT R.aid FROM Reserves R WHERE R.pid IN (SELECT P.pid FROM Pilots P WHERE P.age = 22)); Q8 SELECT P.age FROM Pilots P ORDER BY P.age ASC LIMIT 1 ; Q9 SELECT pid, pname FROM Pilots WHERE age < 21; Q10 SELECT R.aid FROM Reserves R WHERE ; Q11 SELECT R.aid FROM Reserves R, Airplanes A1, Airplanes A2 WHERE (A1.color = red OR R.aid = A1.aid ) AND (A2.color = purple OR R.aid = A2.aid); Q12 { t(age) p Pilots (t.age = p.age p2 Pilots (p2.age > p1.age)} Q13 SELECT pid, pname FROM Pilots WHERE age < 21 OR age IS NULL; Q14 SELECT DISTINCT A.aid, A.aname, A.color FROM Reserves R, Pilots P, Airplanes A WHERE P.age = 22 AND P.pid = R.pid AND R.aid = A.aid; Q15 SELECT age FROM Pilots WHERE rating < 3; Q16 { t(age) p Pilots (t.age = p.age (p.rating >= 3)) }

6 Question 4: Short Answers (10 points) (a) (1 pt each) Given two relations T and S, where T has N T tuples, S has N S tuples, and N T > N S > 0, what are the minimum and maximum possible result cardinalities for the following relational algebra queries expressed in terms of N T and N S? π A T: min size: max size: T S: min size: max size: S - T: min size: max size: (b) (4 points) Suppose that the table Items(i_id, iname, price, shipping, tax) contains three tuples: (352, 'Whatzit', 70.00, 4.00, NULL) (76, 'Gadget', 45.00, 2.00, 1.20) (457, 'Widget', NULL, NULL, 1.50) For each of the following SQL queries, show the output that it would produce if run against Products : (i) (2 pts) SELECT SUM(price + shipping) AS taxfreecost FROM Items; (ii) (2 pts) SELECT i_id, (price * tax) + shipping AS totalcost FROM Items; Question 5: True, False, or Null? (20 points)

7 (2 pts each) For each of the following statements, indicate whether the statement is true (circle ) or false (circle ): Most real SQL implementations disallow CHECK constraints that involve correlated subqueries. Enforcing the uniqueness of a table s column (e.g., User.email) would be a good use of a trigger. Consider the query SELECT * FROM Orders o INNER JOIN Lineitems l ON (o.oid = l.oid). The result tuple count for this query could be larger if INNER were LEFT OUTER instead. Two tables (or queries) are considered by SQL to be UNION-compatible if they have the same number of columns and the same column names in the same order. The query (SELECT * FROM Boat B WHERE B.color = yellow ) UNION ALL (SELECT * FROM Boat B WHERE B.color!= yellow ) is a way to (inefficiently ) return all Boat tuples. The SQL set operation EXCEPT is not actually necessary because it is always possible to express an equivalent query by appropriately using a subquery instead. It s possible to write a relational algebra query involving small Sailor, Boat, and Reservation relations whose result tuple count is infinite. The use of triggers and stored procedures is advisable when writing database applications that need to be easily run on different relational DBMS choices (e.g., MySQL, Oracle, DB2, ). If CREATE TABLE Shirts (name VARCHAR(40), size ENUM('small', 'medium', 'large')) is the DDL for the table Shirts, SELECT size FROM Shirts can have at most 3 result rows. It s possible to write a tuple relational calculus query involving Sailor, Boat, and Reservation relations containing less than 20 tuples each whose result tuple count is infinite.