Assignment 3: SQL Queries. Questions 1. The following relations keep track of airline flight information:



Similar documents
SQL: QUERIES, CONSTRAINTS, TRIGGERS

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

There are five fields or columns, with names and types as shown above.

DATABASE MANAGEMENT SYSTEMS SOLUTIONS MANUAL. Raghu Ramakrishnan et al. University of Wisconsin Madison, WI, USA

DATABASE MANAGEMENT SYSTEMS SOLUTIONS MANUAL. Raghu Ramakrishnan et al. University of Wisconsin Madison, WI, USA

CMPT 354 Assignment 1 Key Instructor: G. Louie

Ch.5 Database Security. Ch.5 Database Security Review

DATABASE MANAGEMENT SYSTEMS SOLUTIONS MANUAL THIRD EDITION

Computer Security: Principles and Practice

Using Temporary Tables to Improve Performance for SQL Data Services

Object-Based Databases

Displaying Data from Multiple Tables

Exercise 1: Relational Model

CS 338 Join, Aggregate and Group SQL Queries

Aggregating Data Using Group Functions

Displaying Data from Multiple Tables. Chapter 4

6 QUERY-BY-EXAMPLE (QBE)

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Objectives of SQL. Terminology for Relational Model. Introduction to SQL

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Query optimization. DBMS Architecture. Query optimizer. Query optimizer.

CIS 631 Database Management Systems Sample Final Exam

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

From Databases to Natural Language: The Unusual Direction

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

VIEWS virtual relation data duplication consistency problems

Intermediate SQL C H A P T E R4. Practice Exercises. 4.1 Write the following queries in SQL:

Relational Database: Additional Operations on Relations; SQL

1. SELECT DISTINCT f.fname FROM Faculty f, Class c WHERE f.fid = c.fid AND c.room = LWSN1106

Objectives of Lecture 1. Labs and TAs. Class and Office Hours. CMPUT 391: Introduction. Introduction

How To Calculate The Cost Of Atc Zero At Chicago Airport

Relational Algebra and SQL

Performing Queries Using PROC SQL (1)

Introduction to SQL C H A P T E R3. Exercises

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Physical Design. Phases of database design. Physical design: Inputs.

Introduction to tuple calculus Tore Risch

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

SQL: Queries, Programming, Triggers

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

SQL Nested & Complex Queries. CS 377: Database Systems

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Data Integrity in Travel Management Reporting

Data Modeling. Database Systems: The Complete Book Ch ,

SQL: Queries, Programming, Triggers

Chapter 1 Overview of the SQL Procedure

Chapter 5. SQL: Queries, Constraints, Triggers

SQL Basics. Introduction to Standard Query Language

New York University Computer Science Department Courant Institute of Mathematical Sciences

Introduction to Microsoft Jet SQL

Mini User's Guide for SQL*Plus T. J. Teorey

SDMX technical standards Data validation and other major enhancements

EIM ETL Validation Script Guidelines

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Part 4: Database Language - SQL

SQL Server 2008 Core Skills. Gary Young 2011

SQL SELECT Query: Intermediate

>

When a variable is assigned as a Process Initialization variable its value is provided at the beginning of the process.

1 Structured Query Language: Again. 2 Joining Tables

White Paper. PiLab Technology Performance Test, Deep Queries. pilab.pl

Introduction to SQL: Data Retrieving

Relational Algebra 2. Chapter 5.2 V3.0. Napier University Dr Gordon Russell

Subqueries Chapter 6

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Objectives of Lecture 1. Class and Office Hours. Labs and TAs. CMPUT 391: Introduction. Introduction

b. Examine the following histories. Draw their serialization graph and identify which of them is serializable given reasons.

Mathematically Modeling Aircraft Fuel Consumption

Natural Language Interfaces (NLI s)

B2.2-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

Aviation Administration

Organization of Records in Blocks

Enhanced-ER Data Model

Database CIS 340. lab#6. I.Arwa Najdi

Ron Shaham. Expert Witness in Islamic Courts : Medicine and Crafts in the Service of Law. : University of Chicago Press,. p 38

. Perspectives on the Economics of Aging. : University of Chicago Press,. p 3 Copyright University of

May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

Building Queries in Microsoft Access 2007

Retrieval: Multiple Tables and Aggregation

Adding and Subtracting Positive and Negative Numbers

Data Stream Management and Complex Event Processing in Esper. INF5100, Autumn 2010 Jarle Søberg

The Integration of Secure Programming Education into the IDE

A Dynamic Programming Approach for 4D Flight Route Optimization

Modeling Time in REA/REAL Databases: Planning and Availability

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

10CS54: DATABASE MANAGEMENT SYSTEM

Data Structure: Relational Model. Programming Interface: JDBC/ODBC. SQL Queries: The Basic From

Appendix A Practices and Solutions

Cloud and Big Data Summer School, Stockholm, Aug Jeffrey D. Ullman

CHAPTER 8: SQL-99: SCHEMA DEFINITION, BASIC CONSTRAINTS, AND QUERIES

EASA ATPL Pilot Training and Boeing / Airbus Type Rating Program in USA

Optimization in Airline Scheduling: Challenges and Successes

IT2304: Database Systems 1 (DBS 1)

GRANT APPLICATION - READING & LEARNING CENTER / COMPUTER LAB

Semantic Errors in SQL Queries: A Quite Complete List

Financial Data Access with SQL, Excel & VBA

IT2305 Database Systems I (Compulsory)

), 35% use extra unleaded gas ( A

Transcription:

Database Systems Instructors: Hao-Hua Chu Winston Hsu Fall Semester, 2007 Assignment 3: SQL Queries Questions 1. The following relations keep track of airline flight information: Flightsflno: integer, from: string, to: string, distance: integer, departs: time, arrives: time, price: integer) Aircraftaid: integer, aname: string, cruisingrange: integer) Certifiedeid: integer, aid: integer) Employeeseid: integer, ename: string, salary: integer) Note that the Employees relation describes pilots and other kinds of employees aswell; every pilot is certified for some aircraft, and only pilots are certified to fly.write each of the following queries in SQL. a. Find the names of aircraft such that all pilots certified to operate them earn more than $80,000. b. For each pilot who is certified for more than three aircraft, find the eid and the maximum cruisingrange of the aircraft for which she or he is certified. c. Find the names of pilots whose salary is less than the price of the cheapest route from Los Angeles to Honolulu. d. For all aircraft with cruisingrange over 1000 miles, find the name of the aircraft and the average salary of all pilots certified for this aircraft. e. Find the names of pilots certified for some Boeing aircraft. f. Find the aids of all aircraft that can be used on routes from Los Angeles to Chicago. g. Identify the routes that can be piloted by every pilot who makes more than $100,000. h. Print the enames of pilots who can operate planes with cruisingrange greater than 3000 miles but are not certified on any Boeing aircraft. i. A customer wants to travel from Madison to New York with no more than two changes of flight. List the choice of departure times from Madison if the customer wants to arrive in New York by 6 p.m. j. Compute the difference between the average salary of a pilot and the average salary of all employees including pilots). k. Print the name and salary of every nonpilot whose salary is more than the average salary for pilots.

l. Print the names of employees who are certified only on aircrafts with cruising range longer than 1000 miles. a. m. Print the names of employees who are certified only on aircrafts with cruising range longer Aircraft than 1000 miles, but on at least two such aircrafts. n. Print the A.Aid A A.aname names of employees who are certified only on aircrafts with cruising range longer than 1000 miles Certified and who are certified on some Boeing aircraft. C.eid C.aid = C, E.eid Employees * E Ans: b. C.eid, Employees E1 IN Certified E1.eid = E.eid E1.salary < 80000 )) C.aid = A.aid C, Aircraft A.cruisingrange) A NOT EXISTS c. C.eid Employees *) > 3 MAX E.salary < E E.ename F.price) HAVING COUNT d. Flights F complication Observe that F.from = Los Angeles F.to = Honolulu ) Temp.name, MIN aid by using is the an key intermediate Temp.AvgSalary for A.aid, Aircraft, relation but the Temp: question asks for aircraft names; we deal with this Aircraft A.aname A, Certified name, C, Employees E.salary) E AvgSalary e. A.aid, E.ename = A.aname C.aid ) C.eid Temp = E.eid A.cruisingrange > 1000 AVG f. Employees E.eid A.aid E, Certified C, Aircraft A GROUP Aircraft = C.eid C.aid = A.aid A.aname Boeing% BY A.cruisingrange A > Flights F.from F.distance) = F Los Angeles F.to = Chicago ) LIKE MIN

g. h. i. Flights F F.from, F.to * Employees E.salary > 100000 E Aircraft A.cruisingrange A, Certified > F.distance C NOT EXISTS Employees E E.eid = C.eid A.aid = C.aid) ) Certified C.eid C A.cruisingrange Aircraft A.aid = A C.aid > 3000 Aircraft A1.aid ) A1 F.departs A1.aid = C.aid Flights A1.aname Boeing% )) F.flno F F0.flno Flights F0.from F0.flno Madison F0.to = New York Flights F0.arrives < 18:00 ) IN F0.from F0.to F0, = Madison Flights = F1.from F1.to F0.to = New <> New York York F0.flno F1.departs > F0.arrives Flights F1.arrives < 18:00 ) F0.from F0.to F0, = Madison Flights F1, Flights F2 F1.to F1.from F2.to F0.to F1.to F2.from F1.departs = F2.departs F2.arrives <> New < > 18:00 F0.arrives F1.arrives York )) * E.eid E.ename NOT EXISTS A.aid IN EXISTS NOT EXISTS UNION UNION LIKE

j. C.eid k. C.eid IN E.ename C1.eid m. E.eid, l. n. E.eid, *) E.eid, COUNT HAVING Temp1.avg - Temp2.avg Employees E.eid E.salary) E avg Certified C )) Temp1, E.ename, E.salary Employees E1.salary) ) Temp2 avg AVG IN E.salary Employees E.eid > E Certified C ) AVG Employees E1.eid E1.salary) NOT Employees Certified C1 )) AVG C.aid A.aid E, Certified E.eid C, Aircraft C.eid IN E.ename E.ename Employees A.cruisingrange 1000) C.aid A.aid E, Certified E.eid C, Aircraft C.eid HAVING EVERY E.ename E.ename Employees A.cruisingrange 1000) > 1 C.aid = E.ename A.aid E, Certified C, Aircraft A A.cruisingrange E.eid = > C.eid 1000) A.aname = Boeing ) HAVING EVERY EVERY ANY

2. Consider the following relational schema. An employee can work in more than one department; the pct_time filed of the Works relation shows the percentage of time that a given employee works in a given department. Empeid: integer, ename: string, age: integer, salary: real) Workseid: integer, did: integer, pct_time: integer) Deptdid: integer, dname: string, budget: real, managerid: integer) Write the following queries in SQL: a. b. Ans: a. Print the names and ages of each employee who works in both the Hardware department and the Software department. b. For each department with more than 20 full-time-equivalent employees i.e., where the part-time and full-time employees add up to at least that many fulltime employees), print the did together with the number of employees that work in that department. c. Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in. d. Find the managerids of managers who manage only departments with budgets greater than $1 million. e. Find the enames of managers who manage the departments with the largest budgets. f. If a manager manages more than one department, he or she controls the sum of all the budgets for those departments. Find the managerids of managers who control more than $5 million. g. Find the E.ename, managerids E.age of managers who control the largest amounts. h. Find the enames of managers who manage only departments with budgets larger than $1 million, but at least one department with budget less than $5 million. E.eid Emp E, Works W1, Works W2, Dept D1, Dept W.did, W1.eid W1.did D1.did D1.dname Hardware Works = W2.eid W2.did = D2.did D2.dname = Software 2000 < W W.eid) Works W1.did W1 = W.did time) ) COUNT HAVING SUM W1.pct

c. d. e. f. g. h. E.ename Emp E.salary E > D.budget Dept D, Works W Dept E.eid = W.eid D.did = W.did) 1000000 D < D.managerid D2.budget ALL E.ename D2.managerid = D.managerid ) E.eid Emp E D.managerid ALL Dept D D.managerid D.budget = D2.budget) Dept Dept D2)) 5000000 D IN < D2.budget) MAX tempd.managerid D2.managerid = D.managerid ) Dept D.managerid D D.budget) tempbudget SUM tempd E.ename tempd.tempbudget = tempd.tempbudget) Emp tempd) E.eid E, = D.managerid Dept D > 1000000) E.Eid, E.ename D.budget < 5000000) MAX ANY Dept Dept ) D.managerid, SUM D.budget HAVING EVERY