Database Systems CSE 414

Similar documents
Database Management Systems. Chapter 5

Using SQL in MS Access

Comp115: Databases. Relational Algebra

SQL Aggregate Queries

SQL language: set operators

CS 500: Fundamentals of Databases. Midterm review. Julia Stoyanovich

CS 461: Database Systems. Relational Algebra. supplementary material: Database Management Systems Sec. 4.1, 4.2 class notes

Real-World Performance Training Hands-On Exercise

Redis Graph. A graph database built on top of redis

TECHNICAL NOTE HOW TO USE LOOPERS. Kalipso_TechDocs_Loopers. Revision: 1.0. Kalipso version: Date: 16/02/2017.

Exam Name: db2 udb v7.1 family fundamentals

Homework 2: Relational Algebra and SQL Due at 5pm on Wednesday, July 20, 2016 NO LATE SUBMISSIONS WILL BE ACCEPTED

RESERVATION.Sid and RESERVATION.Bid are foreign keys referring to SAILOR.Sid and BOAT.Bid, respectively.

Couples, Relations and Functions

Shearwater Cloud Desktop Release Notes

Healthcare Analytics Anticoagulation Time in Therapeutic Range Calculation Documentation September 4, 2015

Reasoning for Ontology Engineering and Usage

CSIT5300: Advanced Database Systems

sname rating 8 .forward Relational Algebra Operator Precedence Sample Query 0 Example Schema bid 103 sname( ( Sailors) Relational Algebra Queries

Homework 4 PLAYERS, TEAMS, MATCHES, PENALTIES, COMMITTEE_MEMBERS

Solution: Homework 2: Relational Algebra Due at 5pm on Monday, February 5, 2018

The FTC Gas Model For Balancing Landfill Gas Extraction

Algebra 3.5 day 1.notebook. September 10, Bellwork

Lecture 10: Generation

Oracle 11g Secure Files Overview Inderpal S. Johal. Inderpal S. Johal, Data Softech Inc.

LING 364: Introduction to Formal Semantics. Lecture 19 March 28th

The Cooperative Cleaners Case Study: Modelling and Analysis in Real-Time ABS

GSIS Video Director s Report ASCII File Layout 2013 Season

Iteration: while, for, do while, Reading Input with Sentinels and User-defined Functions

Ch 12 Homework. Name: Homework problems are from the Serway & Vuille 10 th edition. Follow the instructions and show your work clearly. 1.

Proform Software Upgrade v1.9.1 Release Notes Welcome to release of the Proform Form Book and System Builder.

Implementation of the Different Detector Concepts in Mokka Simulation Framework

Multivariable Predictive Control and its Application on the Solvent Dehydration Tower

COMP 406 Lecture 05. Artificial. Fiona Yan Liu Department of Computing The Hong Kong Polytechnic University

A physicist, an engineer and a programmer were in a car driving over a steep alpine pass when the brakes failed. The car was getting faster and

Cascade control. Cascade control. 2. Cascade control - details

Exercise 2-2. Second-Order Interacting Processes EXERCISE OBJECTIVE DISCUSSION OUTLINE. The actual setup DISCUSSION

Introduction to Alloy

Summary and Comparison of the Data Used by the TTO and CRS Reelection Analyses

Excel Solver Case: Beach Town Lifeguard Scheduling

Hydrostatics Physics Lab XI

Discovering Denial Constraints

CS Lecture 5. Vidroha debroy. Material adapted courtesy of Prof. Xiangnan Kong and Prof. Carolina Ruiz at Worcester Polytechnic Institute

PLA 2.1. Release Notes PLA 2.1 (build 604) December 21, 2015

Chapter 10 Gases. Characteristics of Gases. Pressure. The Gas Laws. The Ideal-Gas Equation. Applications of the Ideal-Gas Equation

{ } Plan#for#Today# CS#133:#Databases# RelaKonal#Calculus# Rel.#Alg.#Compound#Operator:# Division# A B = x y B( x, y A)

AIC TENNIS BY LAWS 2018

Agilent Dimension Software for ELSD User Manual

Parsimonious Linear Fingerprinting for Time Series

Sales Create Outbound Delivery from Sales Order S01- Lean Baseline Package

DISTILLATION PRESSURE CONTROL TROUBLESHOOTING THE HIDDEN PITTFALLS OF OVERDESIGN

CSE 3401: Intro to AI & LP Uninformed Search II

Swim Search: An Online Sports Management Information Retrieval System

REPORT. Operators and investigation by AIB Denmark

Carrier Gases in Capillary GC

RESERVATION.Sid and RESERVATION.Bid are foreign keys referring to SAILOR.Sid and BOAT.Bid, respectively.

ECE 697B (667) Spring 2003

OIL & GAS. 20th APPLICATION REPORT. SOLUTIONS for FLUID MOVEMENT, MEASUREMENT & CONTAINMENT. Q&A: OPEC Responds to SHALE BOOM

Another window pops up where you can input some parameters (called System Preferences) for your team as shown

AUSTRALIAN SAILING LTD

Participation of Women in the Joint Statistical Meetings: Beginning in 1996, the American Statistical Association s Committee on Women in

In [2]: #connect connect connect let python meet SQL. connection = sqlite3.connect('database.sqlite')

(51) Int Cl.: A61B 1/00 ( )

UNIVERSITY OF WATERLOO

Title Specification of Water dew temperature of biomethane injected into below 7 bar Gas Distribution Systems

Instructors: Randy H. Katz David A. PaGerson hgp://inst.eecs.berkeley.edu/~cs61c/fa10. Fall Lecture #39. Agenda

Gaia ARI. ARI s Gaia Workshop. Grégory Mantelet. 20 th June 2018 CDS / ARI

If you need to reinstall FastBreak Pro you will need to do a complete reinstallation and then install the update.

ROUTE 52 ALLENTOWN. Port Authority of Allegheny County

h01: Perkovic Ch1 (Introduction to CS), 2.1, 2.2 (Expr, Vars, Assignment, Strings), 2.3 (Lists, Tuples)

December 20, 2014 ATHLETE GUIDE.

STICTION: THE HIDDEN MENACE

RS Tera Pro World Challenge Trophy 2019 RS Tera Sport World Challenge Trophy 2019

CS472 Foundations of Artificial Intelligence. Final Exam December 19, :30pm

Consumption of Primary, Outer, Closure and Multi-pack Packaging

RF-LAMBDA LEADER OF RF BROADBAND SOLUTIONS

#60Tests#60Minutes #60Tests#60Minutes

Investigating Natural Gas Production and Consumption with Web GIS

ST Shimano Total Integration. Technical Service Instructions. General Safety Information SI-6CT0B

Section 8: Model-View-Controller

This e-book version is for an individual. No corporation, training classes, training institutes are allowed to use this e-book.

CT PET-2018 Part - B Phd COMPUTER APPLICATION Sample Question Paper

Data wrangling. Chapter A grammar for data wrangling select() and filter()

Sharing the How (and not the What )

Cirral Length and Wave Exposure in the barnacle, Semibalanus cariosus. By: Jennifer Maciejeski

Black Box testing Exercises. Lecturer: Giuseppe Santucci

PRESSURE Student: Group:

( ) ( ) *( A ) APPLICATION DATA. Procidia Control Solutions Coarse/Fine Control. Split-Range Control. AD Rev 2 April 2012

Blocking Presupposition by Causal and Identity Inferences. Henk Zeevat SFB991,Heinrich Heine University Düsseldorf & ILLC, Amsterdam University

Work Health and Safety Management Plan

EXPERIMENT 12 GAS LAWS ( BOYLE S AND GAY-LUSSAC S LAW)

CSE 3402: Intro to Artificial Intelligence Uninformed Search II

Natural Bridges Field Trip Activity

Long Red, Short Green, I Got Skipped, and Why Do I Always Have To Stop Who Runs These Signals and How Do I Get Ahold Of Them?

Validation of 12.5 km Resolution Coastal Winds. Barry Vanhoff, COAS/OSU Funding by NASA/NOAA

Acknowledgement: Author is indebted to Dr. Jennifer Kaplan, Dr. Parthanil Roy and Dr Ashoke Sinha for allowing him to use/edit many of their slides.

Living with limits: growth, resources and climate change Martin Wolf, Associate Editor & Chief Economics Commentator, Financial Times

Administration and IT Assignment Assessment Task: Sounds on the Shore

Best Practice Guidance for Assessing the Financial Performance of Fishing Gear: Industry-led gear trials

by Robert Gifford and Jorge Aranda University of Victoria, British Columbia, Canada

Transcription:

Database Systems CSE 414 Lectures 4: Joins & Aggregation (Ch. 6.1-6.4) CSE 414 - Spring 2017 1

Announcements WQ1 is posted to gradebook double check scores WQ2 is out due next Sunday HW1 is due Tuesday (tomorrow), 11pm HW2 is coming out on Wednesday Should now have seats for all registered CSE 414 - Spring 2017 2

Outline Inner joins (6.2, review) Outer joins (6.3.8) Aggregations (6.4.3 6.4.6) CSE 414 - Spring 2017 3

UNIQUE PRIMARY KEY adds implicit NOT NULL constraint while UNIQUE does not you would have to add this explicitly for UNIQUE: CREATE TABLE Company( name VARCHAR(20) NOT NULL, UNIQUE (name)); You almost always want to do this (in real schemas) SQL Server behaves badly with NULL & UNIQUE otherwise, think through NULL for every query you can remove the NOT NULL constraint later CSE 414 - Spring 2017 4

(Inner) Joins SELECT a1, a2,, an FROM R1, R2,, Rm WHERE Cond for t1 in R1: (Nested loop for t2 in R2: semantics)... for tm in Rm: if Cond(t1.a1, t1.a2, ): output(t1.a1, t1.a2,, tm.an) CSE 414 - Spring 2017 5

(Inner) joins Company(cname, country) (pname, price, category, manufacturer) manufacturer is foreign key SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname CSE 414 - Spring 2017 6

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname pname category manufacturer gadget Works Photo Hitachi OneClick Photo Hitachi Company cname Works Canon Hitachi country USA Japan Japan CSE 414 - Spring 2017 7

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname pname category manufacturer gadget Works Photo Hitachi OneClick Photo Hitachi Company cname Works Canon Hitachi country USA Japan Japan pname category manufacturer cname country gadget Works Works USA 8

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname pname category manufacturer gadget Works Photo Hitachi OneClick Photo Hitachi Company cname Works Canon Hitachi country USA Japan Japan Not output since country!= USA (also cname!= manufacturer) CSE 414 - Spring 2017 9

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname pname category manufacturer gadget Works Photo Hitachi OneClick Photo Hitachi Company cname Works Canon Hitachi country USA Japan Japan Not output since country!= USA CSE 414 - Spring 2017 10

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname pname category manufacturer gadget Works Photo Hitachi OneClick Photo Hitachi Company cname Works Canon Hitachi country USA Japan Japan Not output since category!= gadget (and ) CSE 414 - Spring 2017 11

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname pname category manufacturer gadget Works Photo Hitachi OneClick Photo Hitachi Company cname Works Canon Hitachi country USA Japan Japan Not output since category!= gadget CSE 414 - Spring 2017 12

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname pname category manufacturer gadget Works Photo Hitachi OneClick Photo Hitachi Company cname Works Canon Hitachi country USA Japan Japan Not output since category!= gadget CSE 414 - Spring 2017 13

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname pname category manufacturer gadget Works Photo Hitachi OneClick Photo Hitachi Company cname Works Canon Hitachi country USA Japan Japan Not output since category!= gadget (with any Company) CSE 414 - Spring 2017 14

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname pname category manufacturer gadget Works Photo Hitachi OneClick Photo Hitachi Company cname Works Canon Hitachi country USA Japan Japan restrict to category = gadget CSE 414 - Spring 2017 15

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname (where category = gadget ) pname category manufacturer gadget Works Company cname Works Canon Hitachi country USA Japan Japan CSE 414 - Spring 2017 restrict to country = USA 16

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname (where category = gadget ) Company (where country = USA ) pname category manufacturer gadget Works cname Works country USA Now only one combination to consider (Query optimizers do this too.) CSE 414 - Spring 2017 17

(Inner) joins SELECT DISTINCT cname FROM, Company WHERE country = USA AND category = gadget AND manufacturer = cname Alternative syntax: SELECT DISTINCT cname FROM JOIN Company ON country = USA AND category = gadget AND manufacturer = cname Emphasizes that the predicate is part of the join. CSE 414 - Spring 2017 18

Self-Joins and Tuple Variables Ex: find companies that manufacture both products in the gadgets category and in the photo category Just joining Company with is insufficient: need to join Company with with FROM Company,, When a relation occurs twice in the FROM clause we call it a self-join; in that case every column name in is ambiguous (why?) are you referring to the tuple in the 2 nd or 3 rd loop? CSE 414 - Spring 2017 19

Name Conflicts we used cname / pname to avoid this problem When a name is ambiguous, qualify it: WHERE Company.name =.name AND For self-join, we need to distinguish tables: FROM x, y, Company These new names are called tuple variables can think of as name for the variable of each loop can also write Company AS C etc. can make SQL query shorter: C.name vs Company.name CSE 414 - Spring 2017 20

Self-joins SELECT DISTINCT z.cname FROM x, y, Company z WHERE z.country = USA AND x.category = gadget AND y.category = photo AND x.manufacturer = cname AND y.manufacturer = cname; Company pname category manufacturer cname country gadget Works SingleTouch photo Hitachi MultiTouch photo Works Works Hitachi USA Japan

Self-joins SELECT DISTINCT z.cname FROM x, y, Company z WHERE z.country = USA AND x.category = gadget AND y.category = photo AND x.manufacturer = cname AND y.manufacturer = cname; Company x pname category manufacturer cname country gadget Works SingleTouch photo Hitachi MultiTouch photo Works Works Hitachi USA Japan

Self-joins SELECT DISTINCT z.cname FROM x, y, Company z WHERE z.country = USA AND x.category = gadget AND y.category = photo AND x.manufacturer = cname AND y.manufacturer = cname; Company x pname category manufacturer y gadget Works cname Works country USA SingleTouch photo Hitachi MultiTouch photo Works Hitachi Japan

Self-joins SELECT DISTINCT z.cname FROM x, y, Company z WHERE z.country = USA AND x.category = gadget AND y.category = photo AND x.manufacturer = cname AND y.manufacturer = cname; Company x pname category manufacturer y gadget Works cname Works country USA z SingleTouch photo Hitachi Hitachi Japan MultiTouch photo Works restrict to country = USA Not output since y.category!= photo

Self-joins SELECT DISTINCT z.cname FROM x, y, Company z WHERE z.country = USA AND x.category = gadget AND y.category = photo AND x.manufacturer = cname AND y.manufacturer = cname; Company x pname category manufacturer cname country z y gadget Works SingleTouch photo Hitachi Works Hitachi USA Japan MultiTouch photo Works Not output since y.manufacturer!= cname

Self-joins SELECT DISTINCT z.cname FROM x, y, Company z WHERE z.country = USA AND x.category = gadget AND y.category = photo AND x.manufacturer = cname AND y.manufacturer = cname; Company x pname category manufacturer cname country z y gadget Works SingleTouch photo Hitachi MultiTouch photo Works Works Hitachi USA Japan x.pname x.category x.manufacturer y.pname y.category y.manufacturer z.cname z.country gadget Works MultiTouch Photo Works Works USA

Outer joins (name, category) Purchase(prodName, store) -- prodname is foreign key SELECT.name,, Purchase.store FROM, Purchase WHERE.name = Purchase.prodName Or equivalently: SELECT.name,, Purchase.store FROM JOIN Purchase ON.name = Purchase.prodName But some s may not be not listed. Why? 27

Outer joins (name, category) Purchase(prodName, store) -- prodname is foreign key If we want to include products that never sold, then we need an outer join : SELECT.name,, Purchase.store FROM LEFT OUTER JOIN Purchase ON.name = Purchase.prodName CSE 414 - Spring 2017 28

SELECT.name, Purchase.store FROM JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo CSE 414 - Spring 2016 29

SELECT.name, Purchase.store FROM JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo CSE 414 - Spring 2016 30

SELECT.name, Purchase.store FROM JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name Store CSE 414 - Spring 2016 31

SELECT.name, Purchase.store FROM JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name Store CSE 414 - Spring 2016 32

SELECT.name, Purchase.store FROM JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name Store CSE 414 - Spring 2016 33

SELECT.name, Purchase.store FROM JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name Store CSE 414 - Spring 2016 34

SELECT.name, Purchase.store FROM JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name Store Ritz CSE 414 - Spring 2016 35

SELECT.name, Purchase.store FROM JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name Store Ritz CSE 414 - Spring 2016 36

SELECT.name, Purchase.store FROM JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name Store Ritz CSE 414 - Spring 2016 37

SELECT.name, Purchase.store FROM LEFT OUTER JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name Store Ritz CSE 414 - Spring 2016 38

SELECT.name, Purchase.store FROM LEFT OUTER JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name OneClick Store Ritz NULL CSE 414 - Spring 2016 39

SELECT.name, Purchase.store FROM RIGHT OUTER JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name Store Phone Foo Ritz NULL Foo 40

SELECT.name, Purchase.store FROM FULL OUTER JOIN Purchase ON.name = Purchase.prodName Purchase Name Category ProdName Store gadget Photo Ritz OneClick Photo Name Store Phone Foo Ritz OneClick NULL NULL Foo 41

Outer Joins Left outer join: Include the left tuple even if there s no match Right outer join: Include the right tuple even if there s no match Full outer join: Include both left and right tuples even if there s no match (Also something called a UNION JOIN, though it s rarely used.) (Actually, all of these used much more rarely than inner joins.) CSE 414 - Spring 2016 42

Outer Joins Example See lec04-sql-outer-joins.sql CSE 414 - Spring 2016 43

>sqlite3 lecture04 Aggregation in SQL sqlite> create table Purchase( pid int primary key, product text, price float, quantity int, month varchar(15)); Other DBMSs have other ways of importing data sqlite> -- download data.txt sqlite>.import lec04-data.txt Purchase CSE 414 - Spring 2017 44

Comment about SQLite One cannot load NULL values such that they are actually loaded as null values So we need to use two steps: Load null values using some type of special value Update the special values to actual null values update Purchase set price = null where price = null CSE 414 - Spring 2017 45

Simple Aggregations Five basic aggregate operations in SQL select count(*) from Purchase select sum(quantity) from Purchase select avg(price) from Purchase select max(quantity) from Purchase select min(quantity) from Purchase Except count, all aggregations apply to a single value CSE 414 - Spring 2017 46

Aggregates and NULL Values Null values are not used in aggregates insert into Purchase values(12, 'gadget', NULL, NULL, 'april') Let s try the following select count(*) from Purchase select count(quantity) from Purchase select sum(quantity) from Purchase select sum(quantity) from Purchase where quantity is not null; 47

Aggregates and NULL Values Null values are not used in aggregates insert into Purchase values(12, 'gadget', NULL, NULL, 'april') Let s try the following select count(*) from Purchase select count(quantity) from Purchase select sum(quantity) from Purchase select sum(quantity) from Purchase where quantity is not null; 48

Counting Duplicates COUNT applies to duplicates, unless otherwise stated: SELECT Count(product) FROM Purchase WHERE price > 4.99 same as Count(*) if no nulls We probably want: SELECT Count(DISTINCT product) FROM Purchase WHERE price> 4.99 CSE 414 - Spring 2017 49

More Examples SELECT Sum(price * quantity) FROM Purchase SELECT Sum(price * quantity) FROM Purchase WHERE product = bagel What do they mean? CSE 414 - Spring 2017 50

Purchase Simple Aggregations Price Quantity Bagel 3 20 Bagel 1.50 20 Banana 0.5 50 Banana 2 10 Banana 4 10 SELECT Sum(price * quantity) FROM Purchase WHERE product = Bagel CSE 414 - Spring 2017 90 (= 60+30) 51

Purchase Simple Aggregations Price Quantity Bagel 3 20 Bagel 1.50 20 Banana 0.5 50 Banana 2 10 Banana 4 10 SELECT Sum(price * quantity) FROM Purchase WHERE product = Bagel CSE 414 - Spring 2017 90 (= 60+30) 52

More Examples How can we find the average revenue per sale? SELECT sum(price * quantity) / count(*) FROM Purchase WHERE product = bagel How can we find the average price of a bagel sold? SELECT sum(price * quantity) / sum(quantity) FROM Purchase WHERE product = bagel CSE 414 - Spring 2017 53

More Examples SELECT sum(price * quantity) / count(*) FROM Purchase WHERE product = bagel SELECT sum(price * quantity) / sum(quantity) FROM Purchase WHERE product = bagel What happens if there are NULLs in price or quantity? Moral: disallow NULLs unless you need to handle them 54