Proposed solutions Project Assignment #1 (SQL)

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "Proposed solutions Project Assignment #1 (SQL)"

Transcription

1 Proposed solutions Project Assignment #1 (SQL) (a) Database creation: create table sailor (sname CHAR(30), rating INTEGER) create table boat (bname CHAR (30), color CHAR (15), rating INTEGER) create table reservation (sname CHAR(30), bname CHAR (30), rday CHAR(15), starting INTEGER, ending INTEGER) (b) Instance upload: insert into sailor values ( Brutus, 1); insert into sailor values ( Andy, 8); insert into sailor values ( Horatio, 7); insert into sailor values ( Rusty, 8); insert into sailor values ( Bob, 1); insert into boat values ( SpeedQueen, white, 9); insert into boat values ( InterLake, red, 8); insert into boat values ( Marine, blue, 7); insert into boat values ( Bay, red, 3); values ( Andy, Interlake, Monday, 10, 14); values ( Andy, Marine, Saturday, 14, 16); values ( Andy, Bay, Wednesday, 8, 12); values ( Rusty, Bay, Sunday, 9, 12); values ( Rusty, Interlake, Wednesday, 13, 20); values ( Rusty, Interlake, Monday, 9, 11); values ( Andy, Bay, Wednesday, 9, 10); values ( Horatio, Marine, Tuesday, 16, 19); 1

2 (c) Instance listing: select * from sailor; select* from boat; select * ; (d) SQL queries: 1., bname from sailor, boat where sailor.rating >= boat.rating 2., count(bname) as number from sailor, boat where sailor.rating >= boat.rating group by sname) union, 0 as number from sailor from sailor, boat where sailor.rating boat.rating)) Note that Brutus and Bob are not qualified to sail any boat, so their count should be shown as zero. 3. (i) one using the MIN aggregate function, and from sailor where rating in (select MIN(rating) from sailor) 2

3 (ii) another without using MIN. select s.sname from sailor s from sailor where rating < s.rating) 4., boat where reservation.bname = boat.bname and boat.color <> red ) Without the assumption that each boat has only one color, we would write: where bname not in (select bname from boat where color = red )) 5. select s.sname from sailor s r, boat b where r.sname = s.sname and r.bname = b.bname and color = red ) Note that Brutus is one of these since he has no reservations. 3

4 6. (i) with NOT IN tests;, boat b where b.color = red and sname not in where bname = b.bname )) (ii) with NOT EXISTS tests; select r.sname r from boat b where b.color = red and not exists where sname = r.sname and reservation.bname = b.bname )) (iii) with COUNT aggregate functions. select r.sname r where (select count(*) from boat b where b.color = red and ( select count(*) where sname = r.sname and bname = b.bname) = 0 ) = 0 4

5 alternatively, select r.sname r, boat b where r.bname = b.bname and b.color = red group by r.sname having count (distinct r.bname) = (select count (distinct bname) from boat where color = red ) 7. create view unique as select distinct sailor.sname, rating, bname from sailor, reservation where sailor.sname = reservation.sname; select bname, avgrating = avg(rating * 1.0) from unique group by bname The reason for multiplying rating with 1.0 in avg(rating * 1.0) is to convert the rating to a real so that the average is not truncated to an integer. The role of the view unique is to provide the list of boats and sailors with their ratings who have reserved the boatwithout duplicates. This is to avoid counting the same sailor multiple times if the sailor has multiple reservations of the same boat. For example, the following alternative does not avoid this problem: select bname, avgrating = avg(rating) from sailor, reservation where sailor.sname = reservation.sname group by bname 5

6 (e) select x.bname, x.rday, x.sname as sname1, x.starting as start1, x.ending as end1, y.sname as sname2, y.starting as start2, y.ending as end2 x, reservation y where x.bname = y.bname and x.rday = y.rday and ((x.starting = y.starting and x.ending = y.ending and x.sname < y.sname) or (x.starting = y.starting and x.ending > y.ending) or (x.starting < y.starting and y.starting < x.ending)) Note that this lists only one tuple for every pair of conflicting reservations x, y. Indeed, suppose first the time intervals of the reservations are the same. Then the sailors of the two reservations should be different (otherwise x and y would be the same reservation). The double listing is avoided by requiring that x.sname < y.sname (this compares the strings). If the starting time of the two reservations are the same and the intervals are not equal then only one of them ends first, so requiring x.ending > y.ending avoids again the double listing. Finally, if the reservations have different starting times then only one starts first, so x.starting < y.starting avoids double listing. (f) Updates in SQL. 1. update boat set color = xxx where color = blue ; update boat set color = blue where color = red ; update boat set color = red where color = xxx 2. delete from sailor s from boat where s.rating boat.rating) 6

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

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves Tutorial 5: SQL By Chaofa Gao Tables used in this note: Sailors(sid: integer, sname: string, rating: integer, age: real); Boats(bid: integer, bname: string, color: string); Reserves(sid: integer, bid:

More information

sid sname rating age sid bid day bid bname Color

sid sname rating age sid bid day bid bname Color SQL - 2 Week 6-2 Sailors sid sname rating age 22 Dustin 7 45.0 29 Brutus 1 33.0 31 Lubber 8 55.5 32 Andy 8 25.5 58 Rusty 10 35.0 64 Horatio 7 35.0 71 Zorba 10 16.0 74 Horatio 9 35.0 85 Art 3 25.5 95 Bob

More information

Database Systems. Course Administration

Database Systems. Course Administration Database Systems ( ) September 27, 200 Lecture # By Hao-hua Chu ( ) 1 Course Administration Assignment #1 is due today. Assignment #2 is out on the home webpage. It is due one week from today. Next week

More information

Basic form of SQL Queries

Basic form of SQL Queries SQL - 1 Week 6-1 Basic form of SQL Queries SELECT FROM WHERE target-list relation-list qualification target-list A list of attributes of output relations in relation-list relation-list A list of relation

More information

SQL. UVic C SC 370. Dr. Daniel M. German Department of Computer Science. June 3, 2004 Version: SQL (1.1.2) CSC 370

SQL. UVic C SC 370. Dr. Daniel M. German Department of Computer Science. June 3, 2004 Version: SQL (1.1.2) CSC 370 SQL UVic C SC 370 Dr. Daniel M. German Department of Computer Science June 3, 2004 Version: 1.1.2 5 1 SQL (1.1.2) CSC 370 dmgerman@uvic.ca Overview A review of SQL Basic Select statements UNION, INTERSECT,

More information

Relational Algebra and SQL

Relational Algebra and SQL Relational Algebra and SQL Johannes Gehrke johannes@cs.cornell.edu http://www.cs.cornell.edu/johannes Slides from Database Management Systems, 3 rd Edition, Ramakrishnan and Gehrke. Database Management

More information

SQL: The Query Language Part 2. Example Instances. Conceptual Evaluation. sid bid day /10/ /12/96. Queries With GROUP BY

SQL: The Query Language Part 2. Example Instances. Conceptual Evaluation. sid bid day /10/ /12/96. Queries With GROUP BY SQL: The Query Language Part Lecture 10 R & G - Chapter 5 The important thing is not to stop questioning. Albert Einstein Reserves Example Instances Sailors Boats Queries With GROUP BY To generate values

More information

SQL: Queries, Programming, Triggers. Instructor: Alessandra La Spina

SQL: Queries, Programming, Triggers. Instructor: Alessandra La Spina SQL: Queries, Programming, Triggers Instructor: Alessandra La Spina laspina@dm.unipi.it SQL Data Definition Language (DDL): subset of SQL that supports creation, deletion, and modification of definitions

More information

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

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables SQL: Queries, Programming, Triggers Chapter 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Example Instances We will use these instances of the Sailors and Reserves relations in our

More information

SQL: Queries, Programming, Triggers

SQL: Queries, Programming, Triggers SQL: Queries, Programming, Triggers Chapter 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 R1 Example Instances We will use these instances of the Sailors and Reserves relations in

More information

Chapter 5. SQL: Queries, Constraints, Triggers

Chapter 5. SQL: Queries, Constraints, Triggers Chapter 5 SQL: Queries, Constraints, Triggers 1 Overview: aspects of SQL DML: Data Management Language. Pose queries (Ch. 5) and insert, delete, modify rows (Ch. 3) DDL: Data Definition Language. Creation,

More information

SQL: Queries, Constraints, Triggers

SQL: Queries, Constraints, Triggers SQL: Queries, Constraints, Triggers Chapter 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Example Instances We will use these instances of the Sailors and Reserves relations in our

More information

SQL: Queries, Programming, Triggers

SQL: Queries, Programming, Triggers SQL: Queries, Programming, Triggers CSC343 Introduction to Databases - A. Vaisman 1 R1 Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for

More information

Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS INTRODUCTION

Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS INTRODUCTION Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS 1 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will be noted when necessary). - We shall cover - query capabilities

More information

CS275 Intro to Databases SQL SQL 10/18/2012. SQL Chap.5. Structured Query Language (SQL) Select Insert Update Delete

CS275 Intro to Databases SQL SQL 10/18/2012. SQL Chap.5. Structured Query Language (SQL) Select Insert Update Delete CS275 Intro to Databases SQL Chap.5 SQL Structured Query Language (SQL) Widely used SQL:2011 is the latest standard Select Insert Update Delete SQL 1 Select Basic form [DISTINCT] select list FROM from

More information

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

SQL: Queries, Constraints, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. A Note on Range Variables. Example of Conceptual Evaluation SQL: Queries, Constraints, Triggers Chapter 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 R1 Example Instances We will use these instances of the Sailors and Reserves relations in

More information

Basic SQL Query SQL. SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Slightly Modified Query

Basic SQL Query SQL. SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Slightly Modified Query Basic SQL Query SQL SELECT FROM [WHERE [DISTINCT] target-list relation-list condition] SELECT S.Name WHERE S.Age > 25 SELECT DISTINCT S.Name WHERE S.Age > 25 Default is that duplicates are not eliminated!

More information

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

Example Instances. SQL: Queries, Constraints, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. A Note on Range Variables SQL: Queries, Constraints, Triggers Chapter 5 1 Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for the Reserves relation contained only

More information

Relational Calculus. Chapter Comp 521 Files and Databases Spring

Relational Calculus. Chapter Comp 521 Files and Databases Spring Relational Calculus Chapter 4.3-4.5 Comp 521 Files and Databases Spring 2010 1 Relational Calculus Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus has

More information

CSCI-GA Database Systems. Mohamed Zahran (aka Z)

CSCI-GA Database Systems. Mohamed Zahran (aka Z) CSCI-GA.2433-001 Database Systems Lecture 5: SQL I Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com SQL? Structured query language Originally developed by IBM It is now de facto standard.

More information

Comp 5311 Database Management Systems. 4b. Structured Query Language Exercises 3

Comp 5311 Database Management Systems. 4b. Structured Query Language Exercises 3 Comp 5311 Database Management Systems 4b. Structured Query Language Exercises 3 1 Sailors (sid, sname, rating, age), Reserves (sid, bid, date), Boats (bid, bname, color) Aggregate Operators Find the number

More information

Relational Algebra and Calculus

Relational Algebra and Calculus Relational Algebra and Calculus UVic C SC 370 Daniel M German Relational Algebra and Calculus (1.2.0) CSC 370 8/5/2005 22:36 p.1/29 Overview The mathematical foundation of query languages such as SQL Relational

More information

The Database Language SQL (iii)

The Database Language SQL (iii) ICS 321 Spring 2012 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 03/05/2012 Lipyeow Lim -- University of Hawaii at Manoa

More information

Overview. Key Features. Some Facts. DB2 Objects. Data Storage Structure: Tablespace

Overview. Key Features. Some Facts. DB2 Objects. Data Storage Structure: Tablespace Overview IBM DB2 UDB Background Key Features User Tools DB2 advantages/disadvantages Pricing Future of DB2 References Database Management Systems 1 Database Management Systems 2 Some Facts DB2 UDB suports

More information

QUERIES, PROGRAMMING, TRIGGERS

QUERIES, PROGRAMMING, TRIGGERS 5 SQL: QUERIES, PROGRAMMING, TRIGGERS What men or gods are these? What maidens loth? What mad pursuit? What struggle to escape? What pipes and timbrels? What wild ecstasy? What is the average salary in

More information

SQL: Queries, Programming, Triggers

SQL: Queries, Programming, Triggers SQL: Queries, Programming, Triggers Chapter 5 Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Example Instances We will use these instances of the Sailors and Reserves relations in our examples.

More information

CSCI-GA Database Systems. Mohamed Zahran (aka Z)

CSCI-GA Database Systems. Mohamed Zahran (aka Z) CSCI-GA.2433-001 Database Systems Lecture 6: SQL II Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Aggregate Operators Powerful class of constructs to calculate aggregate values. Useful

More information

Databases - Relational Algebra II. (GF Royle, N Spadaccini ) Databases - Relational Algebra II 1 / 28

Databases - Relational Algebra II. (GF Royle, N Spadaccini ) Databases - Relational Algebra II 1 / 28 Databases - Relational Algebra II (GF Royle, N Spadaccini 2006-2010) Databases - Relational Algebra II 1 / 28 This lecture This lecture continues queries in relational algebra. (GF Royle, N Spadaccini

More information

Introduction to SQL and the Relational Model

Introduction to SQL and the Relational Model Introduction to SQL and the Relational Model Data Boot Camp! May 20, 2014 Michael Cafarella 5/20/14 1 Relational Databases The most common kind is a relational database The software is called a Relational

More information

Database Systems I. SQL Queries

Database Systems I. SQL Queries Database Systems I SQL Queries CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Introduction We now introduce SQL, the standard query language for relational DBS. As RA, an SQL query takes

More information

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Example Instances. Relational Algebra

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Example Instances. Relational Algebra Relational Algebra Lecture 9 R &G, Chapter 4 By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental

More information

Relational Algebra. Instructor: Alessandra La Spina

Relational Algebra. Instructor: Alessandra La Spina Relational Algebra Instructor: Alessandra La Spina laspina@dm.unipi.it Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple,

More information

GROUP BY and HAVING. SELECT MIN (S.age) FROM Sailors S WHERE S.rating = i. For i = 1, 2,..., 10:

GROUP BY and HAVING. SELECT MIN (S.age) FROM Sailors S WHERE S.rating = i. For i = 1, 2,..., 10: SQL - 4 Week 9 GROUP BY and HAVING So far, we ve applied aggregate operators to all (qualifying) tuples. Sometimes, we want to apply them to each of several groups of tuples. Consider: Find the age of

More information

MIS Database Systems SQL: Queries, Constraints, Triggers

MIS Database Systems SQL: Queries, Constraints, Triggers MIS 335 - Database Systems SQL: Queries, Constraints, Triggers http://www.mis.boun.edu.tr/durahim/ Ahmet Onur Durahim Learning Objectives Basics of SQL Conceptual Evaluation of SQL Queries Range Variables,

More information

Query-by-Example (QBE)

Query-by-Example (QBE) Query-by-Example (QBE) Module 3, Lecture 6 Example is the school of mankind, and they will learn at no other. -- Edmund Burke (1729-1797) Database Management Systems, R. Ramakrishnan 1 QBE: Intro A GUI

More information

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1 Relational Algebra Module 3, Lecture 1 Database Management Systems, R. Ramakrishnan 1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model

More information

2. Find all sailor id s of sailors who have a rating of at least 8 or reserved boat 103.

2. Find all sailor id s of sailors who have a rating of at least 8 or reserved boat 103. Exercises on SQL Try to solve as many problems as possible. You are not expected to complete this in one tutorial. Go over these exercises before you attend your tutorial. For each exercise, try it first

More information

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries COMP 5138 Relational Database Management Systems Week 5 : Basic COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-1 Today s Agenda Overview Basic Queries Joins Queries Aggregate Functions

More information

Relational Algebra. Relational Query Languages. Formal Relational Query Languages. Chapter 4, Part A

Relational Algebra. Relational Query Languages. Formal Relational Query Languages. Chapter 4, Part A Relational Algebra Chapter 4, Part A Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database.

More information

Relational Algebra. CS430/630 Lecture 2. Slides based on Database Management Systems 3 rd ed, Ramakrishnan and Gehrke

Relational Algebra. CS430/630 Lecture 2. Slides based on Database Management Systems 3 rd ed, Ramakrishnan and Gehrke Relational Algebra CS430/630 Lecture 2 Slides based on Database Management Systems 3 rd ed, Ramakrishnan and Gehrke Relational Query Languages Query languages: Allow manipulation and retrieval of data

More information

Lecture 9 09/14/15. CMPSC431W: Database Management Systems. Instructor: Yu- San Lin

Lecture 9 09/14/15. CMPSC431W: Database Management Systems. Instructor: Yu- San Lin CMPSC431W: Database Management Systems Lecture 9 09/14/15 Instructor: Yu- San Lin yusan@psu.edu Course Website: hcp://www.cse.psu.edu/~yul189/cmpsc431w Slides based on McGraw- Hill & Dr. Wang- Chien Lee

More information

Relational Algebra. Chapter 4, Part A

Relational Algebra. Chapter 4, Part A Relational Algebra Chapter 4, Part A 1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful QLs: Strong formal

More information

Query Evaluation & Optimization

Query Evaluation & Optimization Query Evaluation & Optimization Chapter 16 1 Cost-based Query Sub-System Queries Select * From Blah B Where B.blah = blah Query Parser Query Optimizer Plan Generator Plan Cost Estimator Catalog Manager

More information

Relational Query Optimization 2

Relational Query Optimization 2 Relational Query Optimization 2 R&G - Chapter 14 For ease and speed in doing a thing do not give the work lasting solidity or exactness of beauty. Plutarch, Life of Pericles Query Optimization Query can

More information

Query Optimization. From Chapter 15. Database Management Systems, R. Ramakrishnan and Johannes Gehrke

Query Optimization. From Chapter 15. Database Management Systems, R. Ramakrishnan and Johannes Gehrke Query Optimization From Chapter 15 Schema for Examples Sailors (sid: integer, sname: string, rating: integer, age: real) Reserves (sid: integer, bid: integer, day: dates, rname: string) Reserves: Each

More information

Relational Algebra. Relational Query Languages

Relational Algebra. Relational Query Languages Relational Algebra Chapter 4 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational

More information

Rika Yunitarini M.Kautsar Sophan

Rika Yunitarini M.Kautsar Sophan SQL Part 2 Rika Yunitarini M.Kautsar Sophan Intro One of the most powerful features of SQL is nested queries. A nested query is a query that has another query embedded within it; the embedded query is

More information

Relational Algebra. Yanlei Diao UMass Amherst 9/22/09. Slides Courtesy of R. Ramakrishnan and J. Gehrke

Relational Algebra. Yanlei Diao UMass Amherst 9/22/09. Slides Courtesy of R. Ramakrishnan and J. Gehrke Relational Algebra Yanlei Diao UMass Amherst 9/22/09 Slides Courtesy of R. Ramakrishnan and J. Gehrke 1 Outline Conceptual Design: ER model Logical Design: ER to relational model Querying and manipulating

More information

Introduction to Microsoft Jet SQL

Introduction to Microsoft Jet SQL Introduction to Microsoft Jet SQL Microsoft Jet SQL is a relational database language based on the SQL 1989 standard of the American Standards Institute (ANSI). Microsoft Jet SQL contains two kinds of

More information

CS 220 Relational Algebra 2/3/2015

CS 220 Relational Algebra 2/3/2015 CS 220 Relational Algebra 2/3/2015 Relational Query Languages Query = retrieval program Language examples: Theoretical: 1. Relational Algebra 2. Relational Calculus a. tuple relational calculus (TRC) b.

More information

SQL. Basic format of the select command select [distinct] target_list from tuple_variable_list where qualification [order by target_list_subset];

SQL. Basic format of the select command select [distinct] target_list from tuple_variable_list where qualification [order by target_list_subset]; SQL Basic format of the select command select [distinct] target_list from tuple_variable_list where qualification [order by target_list_subset]; Simple query examples use this relational schema: sailors(sid,

More information

Database Security. Chapter 21

Database Security. Chapter 21 Database Security Chapter 21 Introduction to DB Security Secrecy: Users should not be able to see things they are not supposed to. E.g., A student can t see other students grades. Integrity: Users should

More information

Rika Yunitarini M.Kautsar Sophan

Rika Yunitarini M.Kautsar Sophan SQL Part 1 Rika Yunitarini M.Kautsar Sophan Intro Structured Query Language (SQL) is the most widely used commercial relational database language. Intro The SQL language has several aspects to it: 1. The

More information

Relational Database: Additional Operations on Relations; SQL

Relational Database: Additional Operations on Relations; SQL Relational Database: Additional Operations on Relations; SQL Greg Plaxton Theory in Programming Practice, Fall 2005 Department of Computer Science University of Texas at Austin Overview The course packet

More information

Overview of Query Evaluation. Overview of Query Evaluation

Overview of Query Evaluation. Overview of Query Evaluation Overview of Query Evaluation Chapter 12 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Overview of Query Evaluation Plan: Tree of R.A. ops, with choice of alg for each op. Each operator

More information

SQL Queries and Subqueries Zaki Malik September Sept 04, 2008

SQL Queries and Subqueries Zaki Malik September Sept 04, 2008 SQL Queries and Subqueries Zaki Malik September 04, 2008 Basic SQL Query SELECT [DISTINCT] target list FROM relation list WHERE qualification; Relation list: A list of relation names (possibly with range

More information

Database Systems I. SQL Constraints and Triggers

Database Systems I. SQL Constraints and Triggers Database Systems I SQL Constraints and Triggers CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Integrity Constraints An integrity constraint (IC) describes conditions that every legal instance

More information

Null Values. SQL Constraints

Null Values. SQL Constraints Null Values. SQL Constraints CS430/630 Lecture 10 Slides based on Database Management Systems 3 rd ed, Ramakrishnan and Gehrke Null Values Field values in a tuple may sometimes be unknown: e.g., a rating

More information

Table Alteration. Reminder. Altering Tables (cont.) Altering Tables. Altering Tables (cont.) We have covered: Today we ll t alk about :

Table Alteration. Reminder. Altering Tables (cont.) Altering Tables. Altering Tables (cont.) We have covered: Today we ll t alk about : Reminder We have covered: Creating tables Converting ER diagrams to table definitions Today we ll t alk about : Altering tables Inserting and deleting data from tables Querying tables Table Alteration

More information

The Relational Model of Data (i)

The Relational Model of Data (i) ICS 321 Spring 2013 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/14/2013 Lipyeow Lim -- University of Hawaii at Manoa

More information

Relational Query Languages. Relational Algebra. Formal Relational Query Languages. Preliminaries. Example Instances. Relational Algebra

Relational Query Languages. Relational Algebra. Formal Relational Query Languages. Preliminaries. Example Instances. Relational Algebra Relational Query Languages Relational Algebra Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful QLs: Strong formal foundation based on

More information

INFO/CS 330: Applied Database Systems

INFO/CS 330: Applied Database Systems INFO/CS 330: Applied Database Systems Introduction to Database Security Johannes Gehrke johannes@cs.cornell.edu http://www.cs.cornell.edu/johannes Introduction to DB Security Secrecy:Users should not be

More information

Query Optimization Example. Steps Query op5miza5on steps: 12/13/09

Query Optimization Example. Steps Query op5miza5on steps: 12/13/09 Query Optimization Example (sid, sname, ra5ng, age) (bid, bname, color) (sid, bid, day, rname) Query: SELECT S.sid, S.sname, S.age FROM S, B, R WHERE B.bid = R.rid AND B.bid = R.bid AND B.color = Red AND

More information

Relational Algebra and Calculus. Relational Query Languages

Relational Algebra and Calculus. Relational Query Languages Relational Algebra and Calculus Chapter 4 Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database.

More information

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

CMU - SCS 15-415/15-615 Database Applications Spring 2013, C. Faloutsos Homework 1: E.R. + Formal Q.L. Deadline: 1:30pm on Tuesday, 2/5/2013 CMU - SCS 15-415/15-615 Database Applications Spring 2013, C. Faloutsos Homework 1: E.R. + Formal Q.L. Deadline: 1:30pm on Tuesday, 2/5/2013 Reminders - IMPORTANT: Like all homeworks, it has to be done

More information

RELATIONAL ALGEBRA ALGEBRA

RELATIONAL ALGEBRA ALGEBRA RELATIONAL ALGEBRA (Chapter 2) 1 RELATIONAL ALGEBRA INTRODUCTION Assume the following two relations: Emp (SS#, name, age, salary, dno) Dept (dno, dname, floor, mgrss#) Relational algebra is a procedural

More information

SQL: The Query Language Part 3

SQL: The Query Language Part 3 SQL: The Query Language Part 3 R &G - Chapter 5 Lecture 13 It is not every question that deserves an answer. Publius Syrus. 42 B. C. Schedule Today: Embedded SQL, Cursors, Stored Procedures Dynamic SQL,

More information

SQL: QUERIES, CONSTRAINTS, TRIGGERS

SQL: QUERIES, CONSTRAINTS, TRIGGERS 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS Online material is available for all exercises in this chapter on the book s webpage at http://www.cs.wisc.edu/~dbbook This includes scripts to create tables for each

More information

Still More Operators: Outer Join

Still More Operators: Outer Join Still More Operators: Outer Join outer join is an extension of the join operation to deal with missing information three types: left outer join, right outer join, and full outer join left outer join computes

More information

1st January 2014 Wednesday

1st January 2014 Wednesday 1st January 2014 Wednesday January 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 February _ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

More information

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

Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division Answer Key UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division CS186 Fall 2003 Eben Haber Midterm Midterm Exam: Introduction to Database Systems This exam has

More information

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

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Outline More Complex SQL Retrieval Queries

More information

CSE132A Solutions HW 1

CSE132A Solutions HW 1 CSE132A Solutions HW 1 Problem 2. [60pts] Consider the following schema: Suppliers(sid:integer, sname: string, address: string) Parts(pid:integer, pname: string, color: string) Catalog(sid:integer, pid:

More information

ISYS 365 SQL Refresher. Week 01

ISYS 365 SQL Refresher. Week 01 ISYS 365 SQL Refresher Week 01 Agenda SQL Overview SQL DDL CREATE, DROP, ALTER SQL DML SELECT, INSERT, UPDATE, DELETE Subqueries, Joins, Unions, Intersections and Difference SQL Overview What is SQL? Structured

More information

6 QUERY-BY-EXAMPLE (QBE)

6 QUERY-BY-EXAMPLE (QBE) 6 QUERY-BY-EXAMPLE (QBE) Example is always more efficacious than precept. Samuel Johnson 6.1 INTRODUCTION Query-by-Example (QBE) is another language for querying (and, like SQL, for creating and modifying)

More information

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

1. SELECT DISTINCT f.fname FROM Faculty f, Class c WHERE f.fid = c.fid AND c.room = LWSN1106 Database schema: Department(deptid, dname, location) Student(snum, sname, deptid, slevel, age) Faculty(fid, fname, deptid) Class(cname, time, room, fid) Enrolled(snum, cname) SQL queries: 1. Get the names

More information

D B M G. SQL language: basics. Set operators. The UNION operator The INTERSECT operator The EXCEPT operator. SQL language: set operators

D B M G. SQL language: basics. Set operators. The UNION operator The INTERSECT operator The EXCEPT operator. SQL language: set operators SQL language: basics Set operators Set operators The UNION operator The INTERSECT operator The EXCEPT operator 2013 Politecnico di Torino 1 Set operators The UNION operator The UNION operator Set union

More information

Θεµελίωση Βάσεων εδοµένων

Θεµελίωση Βάσεων εδοµένων Θεµελίωση Βάσεων εδοµένων Βασίλης Βασσάλος 1 What do we need to produce good software? Good programmers, software engineers, project managers, business experts, etc. (People) Good software development

More information

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations Relational Algebra R & G, Chater 4 By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced roblems, and, in effect, increases the mental ower of the

More information

Security and Authorization. Introduction to DB Security. Access Controls. Chapter 21

Security and Authorization. Introduction to DB Security. Access Controls. Chapter 21 Security and Authorization Chapter 21 Database Management Systems, 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to DB Security Secrecy: Users should not be able to see things they are not supposed

More information

Overview of Query Evaluation

Overview of Query Evaluation Overview of Query Evaluation Chapter 12 Comp 521 Files and Databases Fall 2010 1 Overview of Query Evaluation Query: SELECT sname FROM Reserves R, Sailors S WHERE R.sid=S.sid AND R.bid = 100 AND S.rating

More information

CS 542: Distributed Database Systems

CS 542: Distributed Database Systems CS 542: Distributed Database Systems Relational Database Overview Prof. Chris Clifton 14 January 2009 Based on 3 Key Features Simple data structures: 2-dimensional tables physical data independence Solid

More information

Relational Query Optimization. Chapter 15

Relational Query Optimization. Chapter 15 Relational Query Optimization Chapter 15 Highlights of System R Optimizer Impact: Most widely used currently; works well for < 10 joins. Cost estimation: Approximate art at best. Statistics, maintained

More information

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan More on SQL Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan SELECT A1, A2,, Am FROM R1, R2,, Rn WHERE C1, C2,, Ck Interpreting a Query

More information

SQL. Short introduction

SQL. Short introduction SQL Short introduction 1 Overview SQL, which stands for Structured Query Language, is used to communicate with a database. Through SQL one can create, manipulate, query and delete tables and contents.

More information

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

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL COS 597A: Principles of Database and Information Systems SQL: Overview and highlights The SQL Query Language Structured Query Language Developed by IBM (system R) in the 1970s Need for a standard since

More information

Information Systems. Relational Databases. Nikolaj Popov

Information Systems. Relational Databases. Nikolaj Popov Information Systems Relational Databases Nikolaj Popov Research Institute for Symbolic Computation Johannes Kepler University of Linz, Austria popov@risc.uni-linz.ac.at Integrity Constraints Integrity

More information

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

UNIT 6. Structured Query Language (SQL) Text: Chapter 5 UNIT 6 Structured Query Language (SQL) Text: Chapter 5 Learning Goals Given a database (a set of tables ) you will be able to express a query in SQL, involving set operators, subqueries and aggregations

More information

Homework 2: Query Processing/Optimization, Transactions/Recovery (due October 6 th, 2014, 2:30pm, in class hard-copy please)

Homework 2: Query Processing/Optimization, Transactions/Recovery (due October 6 th, 2014, 2:30pm, in class hard-copy please) Virginia Tech. Computer Science CS 5614 (Big) Data Management Systems Fall 2014, Prakash Homework 2: Query Processing/Optimization, Transactions/Recovery (due October 6 th, 2014, 2:30pm, in class hard-copy

More information

Database Programming. Week 10-2. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Database Programming. Week 10-2. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford Database Programming Week 10-2 *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford SQL in Real Programs We have seen only how SQL is used at the generic query

More information

Kathleen Durant PhD Lecture 19 CS 3200 Northeastern University. Slide Content Courtesy of R. Ramakrishnan, J. Gehrke, and J.

Kathleen Durant PhD Lecture 19 CS 3200 Northeastern University. Slide Content Courtesy of R. Ramakrishnan, J. Gehrke, and J. Relational Query Optimization Kathleen Durant PhD Lecture 19 CS 3200 Northeastern University 1 Slide Content Courtesy of R. Ramakrishnan, J. Gehrke, and J. Hellerstein Overview of Query Evaluation Query

More information

SQL Tables, Keys, Views, Indexes

SQL Tables, Keys, Views, Indexes CS145 Lecture Notes #8 SQL Tables, Keys, Views, Indexes Creating & Dropping Tables Basic syntax: CREATE TABLE ( DROP TABLE ;,,..., ); Types available: INT or INTEGER REAL or FLOAT CHAR( ), VARCHAR( ) DATE,

More information

Programming in postgresql with PL/pgSQL. Procedural Language extension to postgresql

Programming in postgresql with PL/pgSQL. Procedural Language extension to postgresql Programming in postgresql with PL/pgSQL Procedural Language extension to postgresql 1 Why a Programming Language? Some calculations cannot be made within a query (examples?) Two options: Write a program

More information

Answer all the following

Answer all the following SKYUPS MEDIA DBMS IMPTANT QUESTIONS WITH MODEL PAPER 1 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Data Base Management System Model Paper 1 (R13) II CSE II Semester Duration: 3hrs Max Marks: 75 1.

More information

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

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu Introduction to SQL and SQL in R LISA Short Courses Xinran Hu 1 Laboratory for Interdisciplinary Statistical Analysis LISA helps VT researchers benefit from the use of Statistics Collaboration: Visit our

More information

Building Queries in Microsoft Access 2007

Building Queries in Microsoft Access 2007 Building Queries in Microsoft Access 2007 Description In this class we will explore the purpose, types and uses of Queries. Learn to design a query to retrieve specific data using criteria and operators.

More information

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain) ECS-165A WQ 11 66 4. SQL Contents Basic Queries in SQL (select statement) Set Operations on Relations Nested Queries Null Values Aggregate Functions and Grouping Data Definition Language Constructs Insert,

More information

Relational Model CENG 351 1

Relational Model CENG 351 1 Relational Model CENG 351 1 Relational Database: Definitions Relational database: a set of relations Relation: made up of 2 parts: Instance : a table, with rows and columns. #Rows = cardinality, #fields

More information

1. Write the query of Exercise 6.19 using TRC and DRC: Find the names of all brokers who have made money in all accounts assigned to them.

1. Write the query of Exercise 6.19 using TRC and DRC: Find the names of all brokers who have made money in all accounts assigned to them. 1. Write the query of Exercise 6.19 using TRC and DRC: Find the names of all brokers who have made money in all accounts assigned to them. TRC: DRC: {B.Name Broker(B) AND A Account (A.BrokerId = B.Id A.Gain

More information

Unique Visits. Returning Visits Total 1,538 1, Average Unique Visits

Unique Visits. Returning Visits Total 1,538 1, Average Unique Visits Side 1 af 9 30-10- 15:11:04 30 October Summary Log: No Limit Total 1,538 1,186 771 415 Average 5 4 3 1 Day Date Sunday 30th October 7 3 3 0 Saturday 29th October 2 1 0 1 Friday 28th October 2 2 1 1 Thursday

More information