Lecture 4: More SQL and Relational Algebra



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

Relational Database: Additional Operations on Relations; SQL

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

Relational Algebra and SQL

Chapter 5. SQL: Queries, Constraints, Triggers

SQL: Queries, Programming, Triggers

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

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

Relational Databases

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

SQL: Queries, Programming, Triggers

Introduction to database design

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

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

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Advance DBMS. Structured Query Language (SQL)

IT2305 Database Systems I (Compulsory)

Databases 2011 The Relational Model and SQL

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

1 Structured Query Language: Again. 2 Joining Tables

SQL SELECT Query: Intermediate

Oracle SQL. Course Summary. Duration. Objectives

Introduction to Microsoft Jet SQL

Database Management Systems. Chapter 1

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

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

In this Lecture SQL SELECT. Example Tables. SQL SELECT Overview. WHERE Clauses. DISTINCT and ALL SQL SELECT. For more information

Query-by-Example (QBE)

Information Systems SQL. Nikolaj Popov

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

IT2304: Database Systems 1 (DBS 1)

Relational Algebra. Basic Operations Algebra of Bags

3. Relational Model and Relational Algebra

Instant SQL Programming

Introduction to SQL for Data Scientists

Introduction to SQL: Data Retrieving

Advanced SQL. Lecture 3. Outline. Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database: SQL and PL/SQL Fundamentals NEW

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

ICAB4136B Use structured query language to create database structures and manipulate data

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

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

Oracle Database: SQL and PL/SQL Fundamentals

Quiz 2: Database Systems I Instructor: Hassan Khosravi Spring 2012 CMPT 354

Oracle Database: SQL and PL/SQL Fundamentals

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Introduction to Querying & Reporting with SQL Server

SQL Nested & Complex Queries. CS 377: Database Systems

SQL: QUERIES, CONSTRAINTS, TRIGGERS

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

Oracle Database 12c: Introduction to SQL Ed 1.1

CS 338 Join, Aggregate and Group SQL Queries

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

CSC 443 Data Base Management Systems. Basic SQL

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Effective Use of SQL in SAS Programming

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

Financial Data Access with SQL, Excel & VBA

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

Programming with SQL

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

PSU SQL: Introduction. SQL: Introduction. Relational Databases. Activity 1 Examining Tables and Diagrams

Chapter 6 The database Language SQL as a tutorial

DATABASE DESIGN AND IMPLEMENTATION II SAULT COLLEGE OF APPLIED ARTS AND TECHNOLOGY SAULT STE. MARIE, ONTARIO. Sault College

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

DBMS / Business Intelligence, SQL Server

The Relational Algebra

Netezza SQL Class Outline

Structured Query Language (SQL)

WRITING EFFICIENT SQL. By Selene Bainum

Performing Queries Using PROC SQL (1)

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Database Administration with MySQL

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

CS143 Notes: Views & Authorization

MOC 20461C: Querying Microsoft SQL Server. Course Overview

SQL (structured query language)

Where? Originating Table Employees Departments

Advanced Query for Query Developers

Chapter 13: Query Processing. Basic Steps in Query Processing

Big Data and Analytics by Seema Acharya and Subhashini Chellappan Copyright 2015, WILEY INDIA PVT. LTD. Introduction to Pig

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

unisys OS 2200 Relational Data Management System (RDMS 2200) and IPF SQL Interface End Use Guide imagine it. done. ClearPath OS

Outline. SAS-seminar Proc SQL, the pass-through facility. What is SQL? What is a database? What is Proc SQL? What is SQL and what is a database

Lecture 6: Query optimization, query tuning. Rasmus Pagh

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

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

3.GETTING STARTED WITH ORACLE8i

SQL JOINs and VIEWs. Chapter 3.3 V3.0. Napier University Dr Gordon Russell

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

1.204 Lecture 3. SQL: Basics, Joins SQL

WORKING WITH SUBQUERY IN THE SQL PROCEDURE

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX


MapReduce examples. CSE 344 section 8 worksheet. May 19, 2011

Querying Microsoft SQL Server 2012

SQL NULL s, Constraints, Triggers

Oracle Database: SQL and PL/SQL Fundamentals NEW

Transcription:

CPSC 421 Database Management Systems Lecture 4: More SQL and Relational Algebra * Some material adapted from R. Ramakrishnan, L. Delcambre, and B. Ludaescher Today s Agenda Go over last week s quiz New assignments! More on SQL queries and relational algebra CPSC 421, 2009 2 1

More SQL Query Constructs SELECT FROM WHERE (SELECT FROM WHERE ) UNION (SELECT FROM WHERE ) ORDER BY GROUP BY HAVING 1. Extensions include SUM, COUNT, MIN, MAX, AVG, etc. 2. Extensions include various kinds of JOINs 3. AddiYonal comparators, e.g., EXISTS, IN, ANY 4. Operators that take two or more complete SQL queries as arguments, e.g., UNION and INTERSECT 5. Several addiyonal clauses, e.g., ORDER BY, GROUP BY, and HAVING CPSC 421, 2009 3 More SQL Query Constructs SELECT FROM WHERE 1. Extensions include SUM, COUNT, MIN, MAX, AVG, etc. CPSC 421, 2009 4 2

Sample Database We ll use this database in our examples Customer(Number, Name, Address, CRating, CAmount, CBalance, Salesperson) Salesperson(Number, Name, Address, Office) Foreign key: Customer.Salesperson Salesperson.Number CPSC 421, 2009 5 SQL SELECT Aggregate operators: COUNT, SUM, MIN, MAX, and AVG SELECT MIN(Cbalance), MAX(Cbalance), AVG(Cbalance) FROM Customer; SELECT MIN(Cbalance), MAX(Cbalance), AVG(Cbalance) FROM Customer WHERE age > 35; If one aggregate operator appears in the SELECT clause, then ALL of the entries in the select clause must be an aggregate operator Unless the query includes a GROUP BY clause (covered later) CPSC 421, 2009 6 3

Applying Aggregates SELECT Name, Phone, AVG(Age) FROM Student WHERE Major = CS ; This SQL query is not well-formed, and not allowed What would/should the query answer be? Teacher ID Name 1 Joe 2 Mary 3 Arun 4 John Phone 123 456 789 999 Age 24 28 32 18 Major CS CS CS English CPSC 421, 2009 7 SQL SELECT What is the difference between these two queries? SELECT COUNT(Name) SELECT DISTINCT Name FROM Customer FROM Customer When will these two queries return the same answer? that is, for what kind of DB instance would these return the same answer CPSC 421, 2009 8 4

SQL SELECT What is the implication of using DISTINCT when computing the SUM or AVG of an attribute? SUM(DISTINCT(age)) vs. SUM(age) What is the implication of using DISTINCT when computing the MIN or MAX of an atribute? MIN(DISTINCT(age)) vs. MIN(age) CPSC 421, 2009 9 SQL SELECT What is the implication of using DISTINCT when computing the SUM or AVG of an attribute? SUM(DISTINCT(age)) vs. SUM(age) The SUM or AVG will be computed only on disynct values What is the implication of using DISTINCT when computing the MIN or MAX of an atribute? MIN(DISTINCT(age)) vs. MIN(age) No difference: the answer does not depend on whether or not duplicates are removed CPSC 421, 2009 10 5

SQL SELECT The SELECT clause list can also include simple arithmetic expressions using +, -, *, and / SELECT (CAmount CBalance) AS AvailableCredit, Name FROM Customer WHERE CAmount > 0 This query computes the available credit for those Customers that have CAmount > 0 CPSC 421, 2009 11 More SQL Query Constructs SELECT FROM WHERE 2. Extensions include various kinds of JOINs CPSC 421, 2009 12 6

There are a number of join types that can be expressed in the WHERE clause inner join (the regular join) cross join natural join left outer join right outer join full outer join CPSC 421, 2009 13 There are a number of join types that can be expressed in the WHERE clause inner join (the regular join) cross join natural join left outer join These can be expressed using basic SELECT FROM WHERE queries they act as syntacyc sugar right outer join full outer join CPSC 421, 2009 14 7

There are a number of join types that can be expressed in the WHERE clause inner join (the regular join) cross join natural join left outer join right outer join These can be expressed using basic SELECT FROM WHERE queries they act as syntacyc sugar These are new operators full outer join CPSC 421, 2009 15 These two queries are equivalent SELECT C.Name, S.Name FROM Customer C JOIN Salesperson S ON C.Salesperson = S.Number WHERE C.CreditRating < 6; SELECT C.Name, S.Name FROM Customer C, Salesperson S WHERE C.Salesperson = S.Number AND C.CreditRating < 6; CPSC 421, 2009 16 8

SQL and equivalent relational algebra queries SELECT C.Name, S.Name FROM Customer C JOIN Salesperson S ON C.Salesperson = S.Number WHERE C.CreditRating < 6; C.Name, S.Name ( C.CreditRating < 6 (Customer C.Salesperson = S.Number Salesperson)) SELECT C.Name, S.Name FROM Customer C, Salesperson S WHERE C.Salesperson = S.Number AND C.CreditRating < 6; C.Name, S.Name ( C.CreditRating < 6 C.Salesperson = S.Number (Customer X Salesperson)) CPSC 421, 2009 17 JOIN with USING clause when attributes in the 2 tables have the same name Course(CNumber, CName, Description) Teacher(TNumber, TName, Phone) Offering(CNumber, TNumber, Time, Days, Room) These two queries are equivalent SELECT C.CNumber, C.CName, Room FROM Course C JOIN Offering USING(CNumber); SELECT C.CNumber, C.Name, Room FROM Course C JOIN Offering O ON C.CNumber=O.CNumber; USING clause doesn t need (and can t have) a correlation name CPSC 421, 2009 18 9

Basic Join INNER JOIN For the INNER JOIN SELECT C.Name, S.Name FROM Customer C INNER JOIN Salesperson S ON C.Salesperson = S.Number; the query answer includes all matches but excludes: Customer rows that do not have a Salesperson Salesperson rows that are not assigned to any customers CPSC 421, 2009 19 Basic Join INNER JOIN The keyword INNER is optional This INNER JOIN query SELECT C.Name, S.Name FROM Customer C INNER JOIN Salesperson S ON C.Salesperson = S.Number; is the same as SELECT C.Name, S.Name FROM Customer C JOIN Salesperson S ON C.Salesperson = S.Number; CPSC 421, 2009 20 10

A CROSS JOIN is a cross product These queries are equivalent SELECT * FROM Customer, Salesperson SELECT * FROM Customer CROSS JOIN Salesperson; CPSC 421, 2009 21 SQL SELECT Equi-Join vs. Natural Join Equi-join: Account Number=Account Deposit When the join is based on equality we always have two identical attributes (columns) in the answer Number 102 102 104 105 Owner W. Wei W. Wei M. Jones H. MarYn Balance 2000 2000 1000 10000 Type checking checking checking checking Account 102 102 104 105 TransId 1 2 3 4 Date 10/22/09 10/29/09 10/29/09 11/2/09 Amount 500 200 1000 10000 The natural join eliminates the duplicate column for joins based on equality CPSC 421, 2009 22 11

Natural join requires attributes with the same name in the two relations For the previous example, we would need to rename Account to Number to use natural join If you don t specify which attributes to join on, natural join will join on all attributes with the same name CPSC 421, 2009 23 NATURAL JOIN is like a macro that joins tables with an equality condition for all attributes with the same name Consider the following database Course(CNumber, CName, Description) Teacher(TNumber, TName, Phone) Offering(CNumber, TNumber, Time, Days, Room) CPSC 421, 2009 24 12

NATURAL JOIN drops columns automatically With any join based on equality, there will always be pairs of identical columns (one for each column joined) The NATURAL JOIN eliminates one of the duplicate columns CPSC 421, 2009 25 List the course and teacher name for all course offerings This query can be expressed with the NATURAL JOIN or with an INNER JOIN These two queries are equivalent SELECT CName, TName FROM Course C, Offering O, Teaching T WHERE C.CNumber = O.CNumber AND O.TNumber = T.Tnumber SELECT CName, TName FROM Course NATURAL JOIN Offering NATURAL JOIN Teacher; This works because the join attributes have the same attribute names CPSC 421, 2009 26 13

Natural Join can be risky What if we had different attribute names? Course(CNumber, Name, Description) Teacher(TNumber, Name, Phone) Offering(CNumber, TNumber, Time, Days, Room) SELECT CName, TName FROM Course NATURAL JOIN Offering NATURAL JOIN Teacher; What else could lead to a problem for natural join? CPSC 421, 2009 27 INNER JOIN vs. OUTER JOIN For the INNER JOIN SELECT C.Name, S.Name FROM Customer INNER JOIN Salesperson ON C.Salesperson = S.Number the query answer does not include a customer that does not have a salesperson a salesperson that is not assigned to any customers CPSC 421, 2009 28 14

Customer without a salesperson Salesperson without a customer Customer Number Name 1 Smith 2 Jones 3 Wei Address 123 X St 222 Y St 111 Z St CRaYng 700 700 700 CAmount 10000 8000 11000 CBalance 9000 3750 9000 Salesperson 55 65 NULL Salesperson Number Name 55 Miller 65 Rojas 75 MarYn Address 555 A St 555 A St 777 B St Office 100 101 200 CPSC 421, 2009 29 An INNER (regular) JOIN includes only those customers that have salespersons (only the matches) SELECT C.Name, S.Name FROM Customer INNER JOIN Salesperson ON C.Salesperson = S.Number A LEFT OUTER JOIN will include all matches plus all customers that do not have a Salesperson A RIGHT OUTER JOIN will include all matches plus all salespersons that are not assigned to any customers A FULL OUTER JOIN will include all of these CPSC 421, 2009 30 15

INNER JOIN on C.Salesperson = S.Number gives: 1 Smith 123 X St 700 10000 9000 55 55 Miller 555 A St 100 2 Jones 222 Y St 700 8000 3750 65 65 Rojas 555 A St 101 LEFT OUTER JOIN on C.Salesperson = S.Number gives us: 1 Smith 123 X St 700 10000 9000 55 55 Miller 555 A St 100 2 Jones 222 Y St 700 8000 3750 65 65 Rojas 555 A St 101 3 Wei 111 Z St 700 11000 9000 NULL NULL NULL NULL NULL Customer Number Name 1 Smith 2 Jones 3 Wei Salesperson Number Name 55 Miller 65 Rojas 75 MarYn Address 123 X st 222 Y st 111 Z st Address 555 A St 555 A St 777 B St CRaYng 700 700 700 Office 100 101 200 CAmount 10000 8000 11000 CBalance 9000 3750 9000 Salesperson 55 65 NULL CPSC 421, 2009 31 INNER JOIN on C.Salesperson = S.Number gives: 1 Smith 123 X St 700 10000 9000 55 55 Miller 555 A St 100 2 Jones 222 Y St 700 8000 3750 65 65 Rojas 555 A St 101 RIGHT OUTER JOIN on C.Salesperson = S.Number gives us: 1 Smith 123 X St 700 10000 9000 55 55 Miller 555 A St 100 2 Jones 222 Y St 700 8000 3750 65 65 Rojas 555 A St 101 NULL NULL NULL NULL NULL NULL NULL 75 MarYn 777 B St 200 Customer Number Name 1 Smith 2 Jones 3 Wei Salesperson Number Name 55 Miller 65 Rojas 75 MarYn Address 123 X st 222 Y st 111 Z st Address 555 A St 555 A St 777 B St CRaYng 700 700 700 Office 100 101 200 CAmount 10000 8000 11000 CBalance 9000 3750 9000 Salesperson 55 65 NULL CPSC 421, 2009 32 16

FULL OUTER JOIN on C.Salesperson = S.Number gives us: 1 Smith 123 X St 700 10000 9000 55 55 Miller 555 A St 100 2 Jones 222 Y St 700 8000 3750 65 65 Rojas 555 A St 101 3 Wei 111 Z St 700 11000 9000 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 75 MarYn 777 B St 200 Customer Number Name 1 Smith 2 Jones 3 Wei Salesperson Number Name 55 Miller 65 Rojas 75 MarYn Address 123 X st 222 Y st 111 Z st Address 555 A St 555 A St 777 B St CRaYng 700 700 700 Office 100 101 200 CAmount 10000 8000 11000 CBalance 9000 3750 9000 Salesperson 55 65 NULL CPSC 421, 2009 33 You can put a complete query expression in the FROM clause (a form of subquery ) SELECT FROM Employee E, (SELECT FROM WHERE ) WHERE The parentheses are important here CPSC 421, 2009 34 17

Relational Algebra Eight standard operators: project select union intersect difference cross product join divide renaming CPSC 421, 2009 35 Relational Algebra Eight standard operators: project select union intersect difference cross product join divide renaming These 4 you have seen already CPSC 421, 2009 36 18

Relational Algebra Eight standard operators: project select union intersect difference cross product join divide renaming These 3 are from set theory CPSC 421, 2009 37 Relational Algebra union intersect difference These operators can only be used with relations that are union-compatible CPSC 421, 2009 38 19

Relational Algebra Two relations are union-compatible if They have the same arity (the same number of attributes) The corresponding attribute have the same domains These relations are union compatible Checking(CNum: int, COwner: string, CBalance: float) Savings(SNum: int, SOwner: string, SBalance: float) Union, intersection, and different all require unioncompatible relations CPSC 421, 2009 39 Relational Algebra union CheckingAccount SavingsAccount CheckingAccount CNum COwner 101 Smith 102 Wei 104 Jones 105 MarYn SavingsAccount SNum SOwner 103 Smith CBalance 1000 2000 1000 10000 SBalance 5000 CNum 101 102 104 105 103 COwner Smith Wei Jones MarYn Smith CBalance 1000 2000 1000 10000 5000 Note that a(ributes are from the first rela2on in the query CPSC 421, 2009 40 20

Relational Algebra intersection CheckingAccount SavingsAccount What is the answer to this query? COwner (CheckingAccount) SOwner (SavingsAccount) What is the answer to this query? Relational Algebra intersection CheckingAccount SavingsAccount What is the answer to this query? It is empty no tuples appear in both relations COwner (CheckingAccount) SOwner (SavingsAccount) What is the answer to this query? Smith the only owner in SavingsAcount 21

Relational Algebra difference CheckingAccount SavingsAccount Find all tuples that are in the CheckingAccount relation but are not in the SavingsAccount relation COwner (CheckingAccount) SOwner (SavingsAccount) Everyone in CheckingAccount except Smith More SQL Query Constructs SELECT FROM WHERE (SELECT FROM WHERE ) UNION (SELECT FROM WHERE ) 4. Operators that take two or more complete SQL queries as arguments, e.g., UNION and INTERSECT CPSC 421, 2009 44 22

SQL UNION (SELECT C.Name FROM Customer C WHERE C.Name LIKE B% ) UNION (SELECT S.Name FROM Salesperson S WHERE S.Name LIKE B% ); Two complete queries with the UNION operator in between CPSC 421, 2009 45 SQL INTERSECTION (SELECT C.Name FROM Customer C) INTERSECT (SELECT S.Name FROM Salesperson S); Two complete queries with the INTERSECT operator in between CPSC 421, 2009 46 23

SQL EXCEPT (i.e., DIFFERENCE) (SELECT C.Name FROM Customer C) EXCEPT (SELECT S.Name FROM Salesperson S); Two complete queries with the INTERSECT operator in between MySQL doesn t support EXCEPT (difference) Inconvenient, but can be simulated using other operators CPSC 421, 2009 47 SQL ALL Dealing with bags in UNION, INTERSECT, EXCEPT UNION vs UNION ALL INTERSECT vs INTERSECT ALL EXCEPT vs EXCEPT ALL If you don t specify ALL, the answer is computed on sets: Eliminate duplicates from first operand Eliminate duplicates from second operand Compute operation Eliminate duplicates from answer Why do this and not just apply operator and eliminate duplicates from the result? CPSC 421, 2009 48 24

For Next Tuesday Reading Ch 4: Intro, 4.1, 4.2 Ch 5: 5.5 On reserve in the library Be sure to know: Relational algebra, basic aggregates, and joins Homework Homework 2 due next Thursday Tuesday we ll do group by, having, order by (you ll need this for homework 2) First part of project assigned start early! CPSC 421, 2009 49 25