CSE-3421M Test #2. Queries

Similar documents
EECS-3421a: Test #2 Queries

3 February 2011 CSE-3421M Test #1 p. 1 of 14. CSE-3421M Test #1. Design

CSE-3421 Test #1 Design

2. (10 points) SQL. Some Quidditch League! [Exercise] Consider the Movie database with the schema in Figure 2 on page 15 for the questions below.

CSE-3421: Exercises. Winter 2011 CSE-3421 Exercises p. 1 of 18

6 February 2014 CSE-3421M Test #1 w/ answers p. 1 of 14. CSE-3421M Test #1. Design

14 October 2015 EECS-3421A Test #1 p. 1 of 14. EECS-3421A Test #1. Design

CSCI-6421 Final Exam York University Fall Term 2004

Name: Database Systems ( 資料庫系統 ) Midterm exam, November 15, 2006

CSE344 Midterm Exam Winter 2017

CSE 414 Midterm. Friday, April 29, 2016, 1:30-2:20. Question Points Score Total: 100

CS 564 Final Exam Fall 2015 Answers

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

CS2300: File Structures and Introduction to Database Systems

Chapter 2: Intro to Relational Model

CSC 261/461 Database Systems Lecture 5. Fall 2017

EECS-3421a: Test #1 Design

I will not use a source other than my brain on this exam: (please sign)

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

CSE 414 Midterm. April 28, Name: Question Points Score Total 101. Do not open the test until instructed to do so.

CSE344 Midterm Exam Fall 2016

CSE344 Final Exam Winter 2017

Course No: 4411 Database Management Systems Fall 2008 Midterm exam

Lecture Query evaluation. Combining operators. Logical query optimization. By Marina Barsky Winter 2016, University of Toronto

CSE 444, Winter 2011, Midterm Examination 9 February 2011

CSE 344 Final Examination

Relational Model and Relational Algebra

CSE 344 Midterm Nov 1st, 2017, 1:30-2:20

RELATIONAL ALGEBRA. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

CSE414 Midterm Exam Spring 2018

Querying Data with Transact SQL

Introduction to SQL Part 2 by Michael Hahsler Based on slides for CS145 Introduction to Databases (Stanford)

Relational Query Languages: Relational Algebra. Juliana Freire

ARTICLE RELATIONAL ALGEBRA

CMPS182 Midterm Examination. Name: Question 1: Question 2: Question 3: Question 4: Question 5: Question 6:

Polls on Piazza. Open for 2 days Outline today: Next time: "witnesses" (traditionally students find this topic the most difficult)

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

[18 marks] Consider the following schema for tracking customers ratings of books for a bookstore website.

2.2.2.Relational Database concept

CS 474, Spring 2016 Midterm Exam #2

INFSCI 2710 Database Management Solution to Final Exam

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

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

McGill April 2009 Final Examination Database Systems COMP 421

CSE Midterm - Spring 2017 Solutions

Querying Data with Transact-SQL

Exam II Computer Programming 420 Dr. St. John Lehman College City University of New York 20 November 2001

Introduction Relational Algebra Operations

Relational Model, Relational Algebra, and SQL

Examination examples

IMPORTANT: Circle the last two letters of your class account:

Chapter 2: Intro to Relational Model

Exam. Question: Total Points: Score:

Chapter 2 The relational Model of data. Relational algebra

L22: The Relational Model (continued) CS3200 Database design (sp18 s2) 4/5/2018

CSE 530 Midterm Exam

CMP-3440 Database Systems

CSE 344 Midterm Exam

COMP3311 Database Systems

Databases. Jörg Endrullis. VU University Amsterdam

Midterm Review. Winter Lecture 13

Relational Database: The Relational Data Model; Operations on Database Relations

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Relational Algebra and SQL. Basic Operations Algebra of Bags

EECS 647: Introduction to Database Systems

CSC 261/461 Database Systems Lecture 13. Fall 2017

Relational Algebra. Procedural language Six basic operators

CSE 544 Principles of Database Management Systems

SQL. The Basics Advanced Manipulation Constraints Authorization 1. 1

CSE 344 JANUARY 19 TH SUBQUERIES 2 AND RELATIONAL ALGEBRA

Fundamentals of Database Systems

Querying Data with Transact-SQL

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Optimization Overview

RELATION AND RELATIONAL OPERATIONS

CS145 Midterm Examination

Informationslogistik Unit 5: Data Integrity & Functional Dependency

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

RELATIONAL DATA MODEL: Relational Algebra

CSE-1520R Test #2. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Querying Data with Transact-SQL (20761)

Exam 1 CS x265 Spring 2018 Name: KEY. I will not use notes, other exams, or any source other than my own brain on this exam:

Web Science & Technologies University of Koblenz Landau, Germany. Relational Data Model

L07: SQL: Advanced & Practice. CS3200 Database design (sp18 s2) 1/11/2018

Querying Microsoft SQL Server 2014

CSE 344 Final Examination

L Information Systems for Engineers. Final exam. ETH Zurich, Autumn Semester 2017 Friday

Relational Algebra for sets Introduction to relational algebra for bags

Carnegie Mellon University Department of Computer Science /615 - Database Applications C. Faloutsos & A. Pavlo, Spring 2014.

Lecture 16. The Relational Model

(a) Explain how physical data dependencies can increase the cost of maintaining an information

Announcements. Agenda. Database/Relation/Tuple. Schema. Discussion. CSE 444: Database Internals

CSCB20 Week 2. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Relational Algebra. Algebra of Bags

Faloutsos - Pavlo CMU SCS /615

Overview. Carnegie Mellon Univ. School of Computer Science /615 - DB Applications. Concepts - reminder. History

Querying Data with Transact-SQL

Transcription:

14 March 2013 CSE-3421M Test #2 w/ answers p. 1 of 16 CSE-3421M Test #2 Queries Family Name: Given Name: Student#: CS&E Account: Instructor: Parke Godfrey Exam Duration: 75 minutes Term: Winter 2013 Answer the following questions to the best of your knowledge. Your answers may be brief, but be precise and be careful. The exam is closed-book and closed-notes. Calculators, etc., are fine to use. Write any assumptions you need to make along with your answers, whenever necessary. There are four major questions, each with parts. Points for each question and sub-question are as indicated. In total, the test is out of 50 points. In schemas, the underlined attributes denote a table s key. Attributes that are in italics are not nullable. Foreign keys are indicated by FK. If you need additional space for an answer, just indicate clearly where you are continuing. Marking Box 1. /15 2. /15 3. /10 4. /10 Total /50

14 March 2013 CSE-3421M Test #2 w/ answers p. 2 of 16 1. (15 points) SQL. San Carlos Municipal Airport. [Exercise] Congratulations! You have just made the major leagues by landing a job with Amazing.com, the famous Internet bookstore. Customer(cno, cname, address) Author(ano, aname, birth, country) Book(isbn, title, year, publisher, language) Wrote(ano, isbn) FK (ano) refs Author FK (isbn) refs Book Stock(isbn, from, qnty, price) FK (isbn) refs Book Purchase(cno, isbn, when, from, qnty) FK (cno) refs Customer FK (isbn, from) refs Stock Payment(cno, when, amount) FK (cno) refs Customer Figure 1: Amazing.com Schema. The basic schema of the main database at Amazing.com is shown in Figure 1. The underlined attributes indicate a table s primary key (and are hence not nullable). Attributes marked in italics are not nullable. Foreign keys are indicated by FK. The attribute price in Stock tells how much a customer pays for that book. The attribute from in Purchase indicates where the stock was pulled from. a. (6 points) Write an SQL query to report customers by cno, name, and address who have ever bought (Purchase) a book authored by an author whose nationality (country) is Canadian. Do not report duplicate rows. select distinct C.cno, C.cname, C.address from Customer C, Purchase P, Wrote W, Author A where C.cno = P.cno and W.isbn = P.isbn and W.ano = A.ano and A.country = Canada ;

14 March 2013 CSE-3421M Test #2 w/ answers p. 3 of 16 b. (6 points) Write an SQL query to report customers by cno, name, and address who have ever bought (Purchase) a book that has two or more authors for more than $200. Do not report duplicate rows. with Multi (isbn) as ( select W.isbn from Wrote W group by W.isbn having count(*) > 1 ) select distinct C.cno, C.cname, C.address from Customer C, Purchase P, Multi M, Stock S where C.cno = P.cno and P.isbn = M.isbn and P.isbn = S.isbn and P.from = S.from and S.price > 200;

14 March 2013 CSE-3421M Test #2 w/ answers p. 4 of 16 c. (3 points) Consider the SQL query select cname, cno, aname, ano from Customer, Author except select cname, cno, aname, ano from ( select cno, ano, isbn from Customer, Wrote except select P.cno, W.ano, W.isbn from Purchase P, Wrote W where P.isbn = W.isbn ) as X, Customer C, Author A where X.cno = C.cno and X.ano = A.ano; State in plain, concise English what the query does. You get zero credit if you use database terms in your answer! (E.g., Well, the query first joins two tables, taking the projection of... does not count!) Which customers have bought every book of an author?

14 March 2013 CSE-3421M Test #2 w/ answers p. 5 of 16 2. (15 points) General. All the beautiful choices! [Multiple Choice] Choose one best answer for each of the following. Each is worth one point. There is no negative penalty for a wrong answer. a. The SQL ORDER BY clause 1 A. can be used to order the results of any sub-query. B. can only be used as the last clause of a query to order the tuples of the answer. C. may affect the results of a join. D. may only be used when GROUP BY is used. E. is required in any SQL query. b. The SQL statement DELETE FROM R; A. will remove all the tuples from R. B. will remove no tuples from R. C. may remove just some tuples from R. D. is syntactically incorrect because it is missing a WHERE clause. E. Not enough information to determine. c. In a real relational database system, if you try to join (natural join) tables R and S and R is empty (that is, it has no tuples), A. the system reports an error. B. the answer set is an empty table. C. the answer set is the same as table S. D. the answer set consists of just one row. E. an answer set is returned; however, the results are system dependent. For Questions 2d and 2e, consider a table T with attributes A and B. d. Which of the following SQL queries is illegal? A. select * from T; B. select count(*) from T; C. select count(*) from T group by A; D. select count(*), max (B) from T group by A; E. select max(count(*)) from T group by A; e. Which of the following SQL queries is illegal? A. select A from T; B. select A, count(*) from T; C. select A, count(*) from T group by A; D. select A, count(*) from T group by A, B; E. select A, B, count(*) from T group by A, B; 1 Accepted A too for this. By the SQL standards, only B is correct; but most database engines allow order by in sub-queries now, regardless.

14 March 2013 CSE-3421M Test #2 w/ answers p. 6 of 16 f. In relational algebra, the intersection operator ( ) is not logically redundant if we only have additionally A. join ( ) B. crossproduct ( ), select (σ), and project (π) C. difference ( ) and union ( ) D. crossproduct ( ) and difference ( ) E. crossproduct ( ) and union ( ) g. R S is equivalent to A. R (R S) B. R (S R) C. (R S) R D. (( R) ( S)) E. There is not enough information to answer this. h. Consider the schema R(A,B) FK (B) refs S S(A,B) FK (A) refs R Note that none of the attributes are nullable. Which of the following is guaranteed to produce as many as, or more, tuples than each of the others? A. R S B. π A (R) S C. R π B (S) D. π A (R) π B (S) E. There is not enough information to answer this. i. Consider the schema from Question 2h again. Which of the following is guaranteed to produce as many as, or more, tuples than each of the others? A. R S B. R S C. R S D. R S E. There is not enough information to answer this. j. Say that A is the primary key of table R, and that B is another, numeric attribute of R. Let r be the number of tuples in table R. (π A (σ B<5 (R))) (π A (σ B 5 (R))) How many tuples will result from the above RA expression? A. 0 B. 1 C. 1 2r D. r E. 2r

14 March 2013 CSE-3421M Test #2 w/ answers p. 7 of 16 k. Consider the following schema. create table T ( c integer primary key, d integer); create table S ( b integer primary key, c integer references T(c) on delete cascade); create table R ( a integer primary key, b integer references S(b) on delete set null); Let tables R, S, and T contain r, s, and t tuples, respectively. Let n be the number of tuples in the result of the following query: select * from R, S, T where R.B = S.B and S.C = T.C; What is the lowest bound on n that you can say with certainty? A. n r B. n t C. n r s t D. n min(r,s,t) E. It is impossible to establish a bound with the information provided.

14 March 2013 CSE-3421M Test #2 w/ answers p. 8 of 16 For Questions 2l to 2n, one of the choices is not like the others. That is, one of the choices could evaluate to a different answer than the others do. Choose the one that may evaluate differently. l. Consider the relations R(A, B), S(B, C), and T(C, A). A. π A,B ((R S) T) B. π A,B (R (T S)) C. R π A,B (S T) D. π A,B (R S) π A,B (S T) E. π A,B (R T) π A,B (R S) m. Consider the schema R(A,B,C), S(A,D), and T(D,B,E). A. π B ((σ C=5 (R) σ E=7 (T)) S) B. π B (π A,B,D (σ (C=5) (E=7) (R T)) (S π B (T))) C. π B ((σ C=5 (R S)) (σ E=7 (S T))) D. π B (σ C=5 (R S)) π B (σ E=7 (S T)) E. π B (π A,B (σ C=5 (R)) (S π D,B (σ E=7 (T)))) n. Consider the schema R(A,B), and S(B,C). (There are no FKs!) 2 A. π A,C (R S) B. { A,C B( A,B R B,C S)} C. { A,C B( A,B R B,C S)} D. select distinct R.A, S.C from R, S where R.B = S.B; E. select distinct R.A, S.C from R, S s1 where R.B in (select s2.b from S s2 where s1.c = s2.c); o. Consider the schema R(A,B), S(B,C), and T(C,A). (There are no FKs!) A. { A B,C( A, B R B, C S C, A T)} B. { A B( A, B R C( B, C S C, A T))} C. { A B( A, B R C( C, A T B, C S))} D. { A B( A, B R C( B, C S A2( C, A2 T A A2)))} E. { A C( C, A T B( B, C S A2( A2, B R A A2)))} 2 Gave everyone credit for Questions 2n and 2o, regardless, because of typos (fixed here) in the test as given. They are good questions, though!

14 March 2013 CSE-3421M Test #2 w/ answers p. 9 of 16 3. (10 points) Relational Algebra & Calculus. The area under what?! [Short Answer] R A B 1 2 3 2 5 6 7 8 9 8 S B C 6 2 2 4 8 1 8 3 2 5 Three tables: R, S, & T. T A C 7 1 1 2 9 3 5 4 3 5 a. (3 points) Show the results of R S. A B C 1 2 4 1 2 5 3 2 4 3 2 5 5 6 2 7 8 1 7 8 3 9 8 1 9 8 3 b. (3 points) Show the results of R (S T). A B C 3 2 5 7 8 1 9 8 3

14 March 2013 CSE-3421M Test #2 w/ answers p. 10 of 16 c. (2 points) Consider any three tables J, K, and L. Is it possible that J (K L) and (J K) L might evaluate to different tables? If so, construct a small example that demonstrates this. If not, briefly explain why. No. Join is associative. d. (2 points) Which is SQL based upon, the domain relational calculus or the tuple relational calculus? Briefly justify your answer. Tuple-relational calculus. Variables (in SQL, range variables) are over a domain of tuples, not over domains of individual attributes as in the domain-relational calculus (which is standard in predicate-calculus notation).

14 March 2013 CSE-3421M Test #2 w/ answers p. 11 of 16 Extra space.

14 March 2013 CSE-3421M Test #2 w/ answers p. 12 of 16 4. (10 points) Aggregation. Groups for a thousand, Alex. [Exercise] Person(sin, name, gender, job, studio, title, year) FK (studio) references Studio, FK (title, year) references Movie Category(cat) Movie(title, year, length, director, studio, cat) FK (director) references Person (sin), FK (studio) references Studio, FK (cat) references Category Cast(sin, title, year, character) FK (sin) references Person, FK (title, year) references Movie Studio(studio, city) Figure 2: Movies and Casts. Write SQL queries for the following with respect to the schema in Figure 2. a. (3 points) For each director (someone who has directed at least one movie), report the person s sin, name, gender, and the number of movies that he or she has directed. Call this last column #movies. select P.sin, P.name, P.gender, count(*) as #movies from Person P, Movie M where P.sin = M.director group by P.sin, P.name, P.gender

14 March 2013 CSE-3421M Test #2 w/ answers p. 13 of 16 b. (4 points) For each actor (someone who has been cast in a movie) and movie category (cat), report the sin, name, gender, and title of the actor and the number of movies of that category in which he or she has been cast. Call this last column #movies. Only count a given actor or actress once per movie; that a person may have played several characters in the movie does not count multiple times. with InMovie (cid, title, year) as ( select distinct cid, title, year from Cast ) select P.sin, P.name, P.gender, P.title, M.cat, count(*) as #movies from Person P, InMovie C, Movie M where P.sin = C.sin and C.title = M.title and C.year = M.year group by P.sin, P.name, P.gender, P.title, M.cat;

14 March 2013 CSE-3421M Test #2 w/ answers p. 14 of 16 Extra space.

14 March 2013 CSE-3421M Test #2 w/ answers p. 15 of 16 c. (3 points) For each movie, report how many male actors and how many female actresses gender = M for male and gender = F for female were cast in the movie in columns male and female, respectively. Again, only count a given actor or actress once per movie; that a person may have played several characters in the movie does not count multiple times. The column male or female should report 0 (zero) if there were no actors or actresses, respectively, in the movie. with Counts (title, year, male, female) as ( select title, year, 0, 0 from Movie union select C.title, C.year, count(distinct C.sin), 0 from Cast C, Person P where C.sin = P.sin and P.gender = M union select C.title, C.year, 0, count(distinct C.sin) from Cast C, Person P where C.sin = P.sin and P.gender = F ) select C.title, C.year, max(c.male) as male, max(c.female) as female from Counts C group by C.title, C.year;

14 March 2013 CSE-3421M Test #2 w/ answers p. 16 of 16 Extra space. Happy Pi Day! Relax. Breathe. Turn in your test. Go home.