CMPT 354 Database Systems. Simon Fraser University Fall Instructor: Oliver Schulte

Similar documents
CMPT 354 Database Systems. Simon Fraser University Summer Instructor: Oliver Schulte

CMPT 354 Database Systems. Simon Fraser University Fall Instructor: Oliver Schulte

CMPT 354 Database Systems. Simon Fraser University Fall Instructor: Oliver Schulte. Assignment 3b: Application Development, Chapters 6 and 7.

University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao

DATABASE MANAGEMENT SYSTEMS

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

Assignment 3: SQL SELECT Queries

Babu Banarasi Das National Institute of Technology and Management

COMP7640 Assignment 2

DR B.R.AMBEDKAR UNIVERSITY B.Sc.(Computer Science): III Year THEORY PAPER IV (Elective 4) PHP, MySQL and Apache

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

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

Database Systems ( 資料庫系統 )

Database Management Systems (COP 5725) Homework 3

Assignment #2. CS Spring 2015 Due on Friday, June 19, 2015, 9 AM For instructions on how to submit your assignment check the course website.

Unit Assessment Guide

CSCI315 Database Design and Implementation Singapore Assignment 2 11 January 2018

Assignment Grading Rubric

Question 1 (a) 10 marks

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

CS450 - Database Concepts Fall 2015

QUIZ 1 REVIEW SESSION DATABASE MANAGEMENT SYSTEMS

192 Chapter 14. TotalCost=3 (1, , 000) = 6, 000

Examination examples

Introduction to SQL Server 2005/2008 and Transact SQL

Business Analytics Nanodegree Syllabus

Database Management Systems MIT Introduction By S. Sabraz Nawaz

Database Management Systems,

RDBMS- Day 4. Grouped results Relational algebra Joins Sub queries. In today s session we will discuss about the concept of sub queries.

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

Database Design and Management - BADM 352 Fall 2009 Syllabus and Schedule

COMP Instructor: Dimitris Papadias WWW page:

Fundamentals of Database Systems

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

CMU - SCS / Database Applications Spring 2013, C. Faloutsos Homework 1: E.R. + Formal Q.L. Deadline: 1:30pm on Tuesday, 2/5/2013

SQL: Queries, Programming, Triggers

COSC 122 Computer Fluency. Databases. Dr. Ramon Lawrence University of British Columbia Okanagan

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

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

Final Examination Computer Science 420 Dr. St. John Lehman College City University of New York 21 May 2002

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Database Systems: Fall 2009 Quiz I Solutions

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

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

CS 245 Midterm Exam Solution Winter 2015

ITT Technical Institute. IT203 Database Development Onsite Course SYLLABUS

Exam Datawarehousing INFOH419 July 2013

QUERY-BY-EXAMPLE (QBE)

Sample Question Paper

Integrity constraints, relationships. CS634 Lecture 2

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

INSRUCTION SHEET Video 1 Using Windows Movie Maker. Star Wars Video Mix

CMPT 354 Database Systems I. Spring 2012 Instructor: Hassan Khosravi

Examples of Physical Query Plan Alternatives. Selected Material from Chapters 12, 14 and 15

Entity-Relationship Modelling. Entities Attributes Relationships Mapping Cardinality Keys Reduction of an E-R Diagram to Tables

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

High Level Database Models

Outer Join, More on SQL Constraints

CMPT 354: Database System I. Lecture 5. Relational Algebra

CS 582 Database Management Systems II

2.2.2.Relational Database concept

Database Management Systems Paper Solution

Relational Algebra Part I. CS 377: Database Systems

UNIVERSITY OF THE PACIFIC COURSE ApPROVAL FORM REVISION. Revised catalog description (attach additional sheet if necessary).

CSE 344 Midterm Exam

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

CS 348 Introduction to Database Management Assignment 2

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

MSIS-DL 317 Syllabus. Faisal Akkawi, Ph.D. Introduction to Databases Fall 09

Administration Naive DBMS CMPT 454 Topics. John Edgar 2

RELATIONAL DATA MODEL: Relational Algebra

Intermediate SQL ( )

Submitting an online T# request is simple and easy. I will go through the basics on how to enter a T# on the next slides.

Introduction to SQL Server 2005/2008 and Transact SQL

LAB 3 Part 1 ADBC - Interactive SQL Queries-Advanced

Homework 1: Relational Algebra and SQL (due February 10 th, 2016, 4:00pm, in class hard-copy please)

Math 155. Measures of Central Tendency Section 3.1

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Visit for more.

CSE 21 Spring 2016 Homework 5. Instructions

Queen s University Faculty of Arts and Science School of Computing CISC 432* / 836* Advanced Database Systems

Database Management Systems

MWF 9:00-9:50AM & 12:00-12:50PM (ET)

Oracle Hyperion Profitability and Cost Management

Lecture #8 (Still More Relational Theory...!)

Course and Contact Information. Course Description. Course Objectives

On line Application Manual for Visiting Student & Student Exchange Program 1

FY17 Trip Request Form

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

Unit I. By Prof.Sushila Aghav MIT

CSC 261/461 Database Systems Lecture 19

AG & SG SIDDHARTHA COLLEGE OF ARTS AND SCIENCES - VUYYURU.

MET CS 669 Database Design and Implementation for Business Term Project: Online DVD Rental Business

ITT Technical Institute. PT2520T Database Concepts Onsite Course SYLLABUS

Relational Model & Algebra. Announcements (Tue. Sep. 3) Relational data model. CompSci 316 Introduction to Database Systems

Step 0 How to begin and what you need to do before you start?

ER modeling. Lecture 4

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

CS698F Advanced Data Management. Instructor: Medha Atre. Aug 04, 2017 CS698F Adv Data Mgmt 1

Transcription:

Assignment 2: Relational Queries, SQL. CMPT 354 Database Systems Simon Fraser University Fall 2016 Instructor: Oliver Schulte Instructions: Check the instructions in the syllabus. The university policy on academic dishonesty and plagiarism (cheating) will be taken very seriously in this course. Everything submitted should be your own writing or coding. You must not let other students copy your work. On your assignment, put down your name, the number of the assignment and the number of the course. Spelling and grammar count. Group Work: Discussions of the assignment is okay, for example to understand the concepts involved. If you work in a group, put down the name of all members of your group. There should be no group submissions. Each group member should write up their own solution to show their own understanding. For the due date please see our course management server https://courses.cs.sfu.ca. Instructions for what to submit and how to obtain the required databases are posted appear at the end of this file. The assignment asks you to write SQL equivalents for relational algebra queries. Since in the course we study SQL before relational algebra, you may want to start with the SQL queries first (parts IV and V). Then you should be able to make use of your SQL query logic to design the relational algebra queries.

Part I. Relational Algebra. Consider the following relational schema. An employee can work in more than one department; the pct_time field of the Works relation shows the percentage of time that a given employee works in a given department. Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pct_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) Write the following queries in relational algebra. In another part you will write SQL equivalents for the relational algebra queries. 1. Print the names and ages of each employee who works in both the Hardware department and the Software department. 2. Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in. To illustrate, if the employee works in 3 departments, whose budgets are 1000, 2000 and 3000 respectively, then the maximum budget of all of the departments that he/she works in is 3000. 3. Find the managerids of managers who manage only departments with budgets greater than $1 million. 4. Find the enames of managers who manage the departments with the largest budgets. To illustrate, suppose there are three departments like this: department 1 with budget $1 million, department 2 with budget $2 million, department 3 with budget $2 million. Then departments 2 and 3 have the largest budgets. Write this query without using aggregate functions. (The text allows aggregate functions in relational algebra but this is not standard.) Total Marks: 100 Marking Criteria: Technical Correctness 70% Notational Correctness 20% Presentation 10%

Part II. Relational Algebra. Consider the following relational schema. Flight(number:integer, airline: string, from: string, to: string) CanAirport(code: string, location: string) USAirport(code: string, location: string) The from and to fields in the Flight table point to the code fields in the airport table. For example, a typical entry in the Flight table might be 56, AIRC, YEG, YYV and a typical entry in the CanAirport table might be YEG, Edmonton. Write a query in relational algebra for the following requests. a. Find all airlines that fly to all Canadian airports. b. Find all airlines that fly to a Canadian airport but not to any US Airport. c. Find the locations of all airports with at least one flight to New York. Total Mark: 70 Marking Criteria: Technical Correctness 70% Notational Correctness 20% Presentation 10%

Part III. Relational Algebra. Introduction In this assignment we use a Star Wars Trilogy database for science fiction fans. Your task will be to create queries that answer some of the often asked questions. The schema is as follows. (You may also want to look at the database.) Tables Characters: contains information about the character's Name (primary key), Race (if known), Homeworld (if known) and Affiliation (rebels/empire/neutral/free-lancer). Planets: contains information about the planet's Name (primary key), its Type (gas/swamp/forest/handmade/ice/desert), and its Affiliation (rebels/empire/neutral) TimeTable: contains CharacterName, PlanetName, Movie in which the character visited the planet and the time of arrival and departure from the planet. Movie 1 represents The Star Wars, Movie 2 represents Empire Strikes Back, and Movie 3 represents Return of the Jedi. Each movie has been divided into 10 time chunks and these chunks are used to define time of arrival and departure. So if Darth Vader visited Bespin (Cloud City) in Empire Strikes Back from the middle of the movie till its end, the record of it will look like this: CharacterName PlanetName Movie Time of Arrival Time of Departure Darth Vader Bespin 2 5 10 Write the following queries in relational algebra. 1. Find all characters that have been on all neutral planets. 2. Find names of the planets visited by humans who are affiliated with the empire. 3. On which planets and in which movies has Luke been at the same time on the planet as Darth Vader? 4. Find humans that visited desert planets and droids that visited swampy planets. List the movies when it happened and the names of the characters. Total Marks: 100 Marking Criteria: Technical Correctness 70% Notational Correctness 20% Presentation 10%

Part IV. SQL Queries (100 marks). Consider again the following relational schema. Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pct_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) Write the following queries in SQL. 1. Print the names and ages of each employee who works in both the Hardware department and the Software department. 2. Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in. To illustrate, if the employee works in 3 departments, whose budgets are 1000, 2000 and 3000 respectively, then the maximum budget of all of the departments that he/she works in is 3000. 3. Find the managerids of managers who manage only departments with budgets greater than $1 million. 4. Find the enames of managers who manage the departments with the largest budgets. Total Marks 100 Marking Criteria, Part V. Technical Correctness 80% Correctness of Results from Queries 10% Presentation, Style 10%

Part V. SQL Queries. Introduction In this part we use a Star Wars Trilogy database for science fiction fans. Your task will be to create SQL queries that answer some of the often asked questions. You may have to use brackets for column names with spaces. For example, to project the Time of Arrival column from the TimeTable relation, you can write SELECT [Time of Arrival] from TimeTable; Write SQL queries for computing the following queries (same as above except for 5). 1. Find all characters that have been on all neutral planets. 2. Find distinct names of the planets visited by humans affiliated with the empire. 3. On which planets and in which movies has Luke been at the same time on the planet as Darth Vader? 4. Find humans that visited desert planets and droids that visited swampy planets. List the movies when it happened and the names of the characters. 5. For each character and for each neutral planet, how much time total did the character spend on the planet? The column that contains the total times should be labelled TotalTime. Marking Criteria, Part IV. Total Marks: 100 Technical Correctness 80% Correctness of Results from Queries 10% Presentation, Style 10%

Installation and Submission Installing the Databases This assignment requires you to execute SQL queries on SQL Server. You will need the two databases Starwars and company. You have two options for importing these to SQL Server. Option 1: DIY. We have posted.bak files for the two databases on-line. You can import these to your personal SQL Server installation using the same procedure as with the AdventureWorks database. Option 2: Use the CSIL setup. The databases have been imported into the CSIL SQL Server instance. You have read access to them, which should be sufficient for this assignment. If you want to import the tables into your CSIL database, you can use the same procedure as with the AdventureWorks database. Writing Up Your Solutions Part I. Relational Algebra. Submit four relational algebra expressions. You can handwrite relational algebra expressions and scan them into a pdf. You will lose marks if your writing is hard to read. This method has the problem that handwriting is difficult to fix, if you want to revise your query. Experience shows that investing a bit of time into figuring out how to typeset RA expressions leads to better solutions. You can use keywords like UNION for, NATURAL JOIN for, but make sure it s clear what algebra operators your keywords refer to. Part II. Relational Algebra. Submit three relational algebra expressions. Part III. Relational Algebra. Submit four relational algebra expressions. All queries should be submitted together as a single pdf file called algebra.pdf. Part IV: SQL queries. Submit four queries. Each query should create a view with the right output. Please submit your solutions two ways: 1) as an executable.sql script, 2) with screenshots. (Submit both, not just one). Part V: SQL queries. Submit five queries. Each query should create a view with the right output. Please submit your solutions two ways: 1) as an executable.sql script, 2) with screenshots. (Submit both, not just one). Submit a single file called views.sql for your solution to both parts IV and V. For the screenshots, submit a single file sql-screen.pdf for both parts IV and V.

Instructions for writing up the SQL queries. The view for query 1 should be called QueryIV1, the view for query 2 should be called QueryIV2, etc. up to query 5. Put all the view definitions into a single sql script that executes without errors on SQL Server. So your script should look like this: /* begin script */ CREATE VIEW QueryIV1 AS CREATE VIEW QueryIV2 AS CREATE VIEW QueryIV3 AS CREATE VIEW QueryIV4 AS CREATE VIEW QueryV1 AS CREATE VIEW QueryV2 AS CREATE VIEW QueryV3 AS CREATE VIEW QueryV4 AS

CREATE VIEW QueryV5 AS /*end script */ Do not change the column names from the original database. This means that you can use the AS alias construct for table names, but not column names. For example: GOOD: SELECT [Time of Arrival] from TimeTable; BAD: SELECT [Time of Arrival] AS MyNewName from TimeTable; An exception is query 5, where you introduce a new type of column for the total time spent by each character on each planet. The column that contains the total times should be labelled TotalTime. The purpose for having all solutions use the same column names is that it makes it easier for us to check if your solutions are correct.

Instructions for input-output screenshots. Show the definition of each query, followed by a screenshot of the output table. So the file should look like this: CREATE VIEW QueryIV1 AS CREATE VIEW QueryIV2 AS CREATE VIEW QueryIV3 AS CREATE VIEW QueryIV4 AS

CREATE VIEW QueryIV5 AS.