SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName

Similar documents
Introduction to Database Systems CSE 414

Introduction to Data Management CSE 344

Announcements. Outline UNIQUE. (Inner) joins. (Inner) Joins. Database Systems CSE 414. WQ1 is posted to gradebook double check scores

Principles of Database Systems CSE 544. Lecture #2 SQL The Complete Story

Database Systems CSE 414

Introduction to Data Management CSE 344

CSE 544 Principles of Database Management Systems

Subqueries. 1. Subqueries in SELECT. 1. Subqueries in SELECT. Including Empty Groups. Simple Aggregations. Introduction to Database Systems CSE 414

Announcements. Joins in SQL. Joins in SQL. (Inner) joins. Joins in SQL. Introduction to Database Systems CSE 414

Lecture 03: SQL. Friday, April 2 nd, Dan Suciu Spring

L07: SQL: Advanced & Practice. CS3200 Database design (sp18 s2) 1/11/2018

SQL. Assit.Prof Dr. Anantakul Intarapadung

CSE 344 JANUARY 19 TH SUBQUERIES 2 AND RELATIONAL ALGEBRA

Announcements. Subqueries. Lecture Goals. 1. Subqueries in SELECT. Database Systems CSE 414. HW1 is due today 11pm. WQ1 is due tomorrow 11pm

Introduction to Database Systems CSE 444

Database Systems CSE 414

CSE 544 Principles of Database Management Systems

Lecture 4: Advanced SQL Part II

CSC 261/461 Database Systems Lecture 5. Fall 2017

SQL. Structured Query Language

Principles of Database Systems CSE 544. Lecture #2 SQL, Relational Algebra, Relational Calculus

Introduction to Database Systems CSE 414. Lecture 8: SQL Wrap-up

Introduction to SQL Part 2 by Michael Hahsler Based on slides for CS145 Introduction to Databases (Stanford)

Introduction to Database Systems CSE 414

Announcements. Multi-column Keys. Multi-column Keys (3) Multi-column Keys. Multi-column Keys (2) Introduction to Data Management CSE 414

Querying Data with Transact SQL

Introduction to Database Systems CSE 414

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

Announcements. Agenda. Database/Relation/Tuple. Discussion. Schema. CSE 444: Database Internals. Room change: Lab 1 part 1 is due on Monday

CMPT 354: Database System I. Lecture 4. SQL Advanced

Introduction to Database Systems CSE 344

Announcements. Multi-column Keys. Multi-column Keys. Multi-column Keys (3) Multi-column Keys (2) Introduction to Data Management CSE 414

CSE 344 Introduction to Data Management. Section 2: More SQL

Introduction to Data Management CSE 344

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Introduction to Data Management CSE 414

Midterm Review. Winter Lecture 13

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

CSE 544 Principles of Database Management Systems. Alvin Cheung Fall 2015 Lecture 8 - Data Warehousing and Column Stores

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

Please pick up your name card

Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

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

Processing of Very Large Data

CS 2451 Database Systems: More SQL

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

Lecture 3 Additional Slides. CSE 344, Winter 2014 Sudeepa Roy

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Announcements. Agenda. Database/Relation/Tuple. Schema. Discussion. CSE 444: Database Internals

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

Lecture 04: SQL. Monday, April 2, 2007

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Agenda. Discussion. Database/Relation/Tuple. Schema. Instance. CSE 444: Database Internals. Review Relational Model

Introduction to Data Management CSE 344

Advanced Data Management Technologies

Databases (MariaDB/MySQL) CS401, Fall 2015

Keys, SQL, and Views CMPSCI 645

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Nested Queries. Dr Paolo Guagliardo. Aggregate results in WHERE The right way. Fall 2018

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Database Usage (and Construction)

Data Warehousing and Decision Support

CSE 414 Midterm. April 28, Name: Question Points Score Total 101. Do not open the test until instructed to do so.

SQL: Recursion. Announcements (October 3) A motivating example. CPS 116 Introduction to Database Systems. Homework #2 graded

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Database Applications (15-415)

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

In-class activities: Sep 25, 2017

Polls on Piazza. Open for 2 days Outline today: Next time: "witnesses" (traditionally students find this topic the most difficult)

Database Systems CSE 414

Teradata SQL Features Overview Version

Lecture 04: SQL. Wednesday, October 4, 2006

CMPT 354: Database System I. Lecture 3. SQL Basics

Advanced Data Management Technologies

1) Introduction to SQL

Lecture 6 - More SQL

Avoiding Sorting and Grouping In Processing Queries

(a) (14 points) Find all people who are friends with Clara Oswin Oswald both on Facebook and Twitter since Return their names.

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

CS 327E Lecture 11. Shirley Cohen. March 2, 2016

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

Querying Data with Transact-SQL

T-SQL Training: T-SQL for SQL Server for Developers

Compiere 3.8 GA Quick Info Widgets 07/15/2012

Data Science 100 Databases Part 2 (The SQL) Previously. How do you interact with a database? 2/22/18. Database Management Systems

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

SQL Server Administration Class 4 of 4. Activant Prophet 21. Basic Data Manipulation

SQL Continued! Outerjoins, Aggregations, Grouping, Data Modification

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 2: Relations and SQL. September 5, Lecturer: Rasmus Pagh

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Homework Assignment 2. Due Date: October 21th, :30pm (noon) CS425 - Database Organization Results

SQL Structured Query Language Introduction

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2006 Lecture 3 - Relational Model

Lectures 2&3: Introduction to SQL

Data Science 100. Databases Part 2 (The SQL) Slides by: Joseph E. Gonzalez & Joseph Hellerstein,

SQL: Data Querying. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 4

Transcription:

Announcements Introduction to Data Management CSE 344 Lectures 5: More SQL aggregates Homework 2 has been released Web quiz 2 is also open Both due next week 1 2 Outline Outer joins (6.3.8, review) More aggregations (6.4.3 6.4.6) Prod 3 4 Prod Prod 5 6 1

Prod Prod 7 8 Prod Prod 9 10 FROM LEFT OUTER JOIN ON FROM LEFT OUTER JOIN ON Prod Prod NULL 11 12 2

FROM FULL OUTER JOIN ON Prod Phone Foo 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 NULL NULL Foo 13 14 (pid,product,price,quantity,month) Grouping and Aggregation Grouping and Aggregation 1. Compute the FROM and WHERE clauses. 2. Group by the attributes in the GROUPBY FROM 3. Compute the SELECT clause: grouped attributes and aggregates. FWGS 15 16 (pid,product,price,quantity,month) Grouping and Aggregation (pid,product,price,quantity,month) Ordering Results FROM SELECT product, price*quantity as rev FROM FROM ORDER BY rev desc 17 18 3

(pid,product,price,quantity,month) Ordering Results (pid,product,price,quantity,month) HAVING Clause FROM ORDER BY rev desc FWGOS 19 Same query as earlier, except that we consider only products that had at least 30 sales. SELECT product, sum(price*quantity) FROM WHERE price > 1 HAVING sum(quantity) > 30 HAVING clause contains conditions on aggregates. 20 (pid,product,price,quantity,month) (pid,product,price,quantity,month) SELECT month, sum(price*quantity), 21 22 (pid,product,price,quantity,month) (pid,product,price,quantity,month) SELECT FROM month, sum(price*quantity), SELECT month, sum(price*quantity), FROM GROUP BY month 23 24 4

(pid,product,price,quantity,month) SELECT month, sum(price*quantity), FROM GROUP BY month HAVING sum(quantity) < 10 (pid,product,price,quantity,month) SELECT month, sum(price*quantity), FROM GROUP BY month HAVING sum(quantity) < 10 ORDER BY sum(quantity) 25 26 WHERE vs HAVING WHERE condition is applied to individual rows The rows may or may not contribute to the aggregate No aggregates allowed here HAVING condition is applied to the entire group Entire group is returned, or not at all May use aggregate functions in the group Aggregates and Joins create table (pid int primary key, pname varchar(15), manufacturer varchar(15)); insert into product values(1,'bagel,'sunshine Co.'); insert into product values(2,'banana,'busyhands'); insert into product values(3,'gizmo,'works'); insert into product values(4,','busyhands'); insert into product values(5,'power,'powerworks'); 27 28 (pid,product,price,quantity,month) (pid,pname,manufacturer) Aggregate + Join Example SELECT x.manufacturer, count(*) FROM x, y WHERE x.pname = y.product GROUP BY x.manufacturer What do these query mean? SELECT x.manufacturer, y.month, count(*) FROM x, y WHERE x.pname = y.product GROUP BY x.manufacturer, y.month 29 General form of Grouping and Aggregation SELECT S FROM R 1,,R n WHERE C1 GROUP BY a 1,,a k HAVING C2 S = may contain attributes a 1,,a k and/or any aggregates but NO OTHER ATTRIBUTES C1 = is any condition on the attributes in R 1,,R n C2 = is any condition on aggregate expressions and on attributes a 1,,a k Why? 30 5

Semantics of SQL With Group-By SELECT S FROM R 1,,R n WHERE C1 GROUP BY a 1,,a k HAVING C2 Evaluation steps: 1. Evaluate FROM-WHERE using Nested Loop Semantics 2. Group by the attributes a 1,,a k 3. Apply condition C2 to each group (may have aggregates) 4. Compute aggregates in S and return the result 31 Semantics of SQL With Group-By SELECT S FROM R 1,,R n WHERE C1 GROUP BY a 1,,a k HAVING C2 Evaluation steps: 1. Evaluate FROM-WHERE using Nested Loop Semantics 2. Group by the attributes a 1,,a k 3. Apply condition C2 to each group (may have aggregates) 4. Compute aggregates in S and return the result FWGHOS 32 Empty Groups In the result of a group by query, there is one row per group in the result No group can be empty! In particular, count(*) is never 0 SELECT x.manufacturer, count(*) FROM x, y WHERE x.pname = y.product GROUP BY x.manufacturer What if there are no purchases for a manufacturer Empty Group Solution: Outer Join SELECT x.manufacturer, count(y.pid) FROM x LEFT OUTER JOIN y ON x.pname = y.product GROUP BY x.manufacturer 33 34 6