Lectures 6: Introduction to SQL 5

Slides:



Advertisements
Similar presentations
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
Advertisements

1 Lecture 03: Advanced SQL. 2 Outline Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views Reading:
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2008.
1 Lecture 03: SQL Friday, January 7, Administrivia Have you logged in IISQLSRV yet ? HAVE YOU CHANGED YOUR PASSWORD ? Homework 1 is now posted.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #12 M.P. Johnson Stern School of Business, NYU Spring, 2005.
1 Lecture 3: More SQL Friday, January 9, Agenda Homework #1 on the web site today. Sign up for the mailing list! Next Friday: –In class ‘activity’
CSE544: SQL Monday 3/27 and Wednesday 3/29, 2006.
Lecture 3: Introduction to SQL September 29, 2014.
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
1 Lecture 4: More SQL Monday, January 13th, 2003.
Computer Science 101 Web Access to Databases SQL – Extended Form.
SQL (almost end) April 26 th, Agenda HAVING clause Views Modifying views Reusing views.
SQL April 22 th, Agenda Union, intersections Sub-queries Modifying the database Views Modifying views Reusing views.
1 SQL cont.. 2 Outline Unions, intersections, differences (6.2.5, 6.4.2) Subqueries (6.3) Aggregations (6.4.3 – 6.4.6) Hint for reading the textbook:
IM433-Industrial Data Systems Management Lecture 5: SQL.
More SQL: Complex Queries, Triggers, Views, and Schema Modification UMM AL QURA UNIVERSITY College of Computer Dr. Ali Al Najjar 1.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
SQL SQL Review. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
1 SQL Additional Notes. 2  1 Group and Aggregation*  2 Execution Order*  3 Join*  4 Find the maximum  5 Line Format SQL Additional Notes *partially.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
Aggregation SELECT Sum(price) FROM Product WHERE manufacturer=“Toyota” SQL supports several aggregation operations: SUM, MIN, MAX, AVG, COUNT Except COUNT,
COMP 430 Intro. to Database Systems Grouping & Aggregation Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!
1 Lecture 03: SQL Monday, January 9, Project t/Default.aspxhttp://iisqlsrv.cs.washington.edu/444/Projec.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
SQL.
Lectures 2&3: Introduction to SQL
Cours 7: Advanced SQL.
Lecture 04: SQL Monday, January 10, 2005.
Chapter 3 Introduction to SQL(3)
Databases : More about SQL
Database Systems Subqueries, Aggregation
Concept of Aggregation in SQL
Cse 344 April 4th – Subqueries.
Basic SQL Lecture 6 Fall
Monday 3/27 and Wednesday 3/29, 2006
Class 2 Relational Data Languages
SQL : Query Language Part II CS3431.
CS 405G: Introduction to Database Systems
Cse 344 January 12th –joins.
SQL-2 Week 9-11.
Introduction to Database Systems CSE 444 Lecture 03: SQL
January 17th – Subqueries
Lecture 4: Advanced SQL – Part II
Introduction to SQL Wenhao Zhang October 5, 2018.
SQL – Entire Select.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Introduction to Database Systems CSE 444 Lecture 03: SQL
CSE544 SQL Wednesday, March 31, 2004.
SQL Introduction Standard language for querying and manipulating data
CMPT 354: Database System I
Lecture 12: SQL Friday, October 20, 2000.
Lectures 3: Introduction to SQL Part II
Lectures 7: Introduction to SQL 6
Lectures 3: Introduction to SQL 2
Lectures 5: Introduction to SQL 4
Lecture 4: SQL Thursday, January 11, 2001.
Lecture 3 Monday, April 8, 2002.
Class 2 Relational Data Languages
Section 4 - Sorting/Functions
Lecture 4: SQL Wednesday, April 10, 2002.
Lecture 03: SQL Friday, October 3, 2003.
Lectures 2: Introduction to SQL 1
Lecture 04: SQL Monday, October 6, 2003.
Lecture 14: SQL Wednesday, October 31, 2001.
Instructor: Zhe He Department of Computer Science
Presentation transcript:

Lectures 6: Introduction to SQL 5 Lecture and activity contents are based on what Prof Chris Ré used in his CS 145 in the fall 2016 term with permission.

What you will learn about in this section Aggregation operators GROUP BY GROUP BY: with HAVING, semantics ACTIVITY: Fancy SQL Pt. I

Aggregation SQL supports several aggregation operations: SELECT AVG(price) FROM Product WHERE maker = “Toyota” SELECT COUNT(*) FROM Product WHERE year > 1995 SQL supports several aggregation operations: SUM, COUNT, MIN, MAX, AVG Except COUNT, all aggregations apply to a single attribute

Aggregation: COUNT We probably want: COUNT applies to duplicates, unless otherwise stated SELECT COUNT(category) FROM Product WHERE year > 1995 Note: Same as COUNT(*). Why? We probably want: SELECT COUNT(DISTINCT category) FROM Product WHERE year > 1995

More Examples What do these mean? Purchase(product, date, price, quantity) SELECT SUM(price * quantity) FROM Purchase What do these mean? SELECT SUM(price * quantity) FROM Purchase WHERE product = ‘bagel’

Simple Aggregations Purchase 50 (= 1*20 + 1.50*20) Product Date Price Quantity bagel 10/21 1 20 banana 10/3 0.5 10 10/10 10/25 1.50 SELECT SUM(price * quantity) FROM Purchase WHERE product = ‘bagel’ 50 (= 1*20 + 1.50*20)

Grouping and Aggregation Purchase(product, date, price, quantity) SELECT product, SUM(price * quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product Find total sales after 10/1/2005 per product. Let’s see what this means…

Grouping and Aggregation Semantics of the query: 1. Compute the FROM and WHERE clauses 2. Group by the attributes in the GROUP BY 3. Compute the SELECT clause: grouped attributes and aggregates

1. Compute the FROM and WHERE clauses SELECT product, SUM(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product Product Date Price Quantity Bagel 10/21 1 20 10/25 1.50 Banana 10/3 0.5 10 10/10 FROM

2. Group by the attributes in the GROUP BY SELECT product, SUM(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product Product Date Price Quantity Bagel 10/21 1 20 10/25 1.50 Banana 10/3 0.5 10 10/10 Product Date Price Quantity Bagel 10/21 1 20 10/25 1.50 Banana 10/3 0.5 10 10/10 GROUP BY

3. Compute the SELECT clause: grouped attributes and aggregates SELECT product, SUM(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product Product Date Price Quantity Bagel 10/21 1 20 10/25 1.50 Banana 10/3 0.5 10 10/10 Product TotalSales Bagel 50 Banana 15 SELECT

HAVING Clause SELECT product, SUM(price*quantity) FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product HAVING SUM(quantity) > 100 Same query as before, except that we consider only products that have more than 100 buyers HAVING clauses contains conditions on aggregates Whereas WHERE clauses condition on individual tuples…

General form of Grouping and Aggregation SELECT S FROM R1,…,Rn WHERE C1 GROUP BY a1,…,ak HAVING C2 Why? S = Can ONLY contain attributes a1,…,ak and/or aggregates over other attributes C1 = is any condition on the attributes in R1,…,Rn C2 = is any condition on the aggregate expressions

General form of Grouping and Aggregation SELECT S FROM R1,…,Rn WHERE C1 GROUP BY a1,…,ak HAVING C2 Evaluation steps: Evaluate FROM-WHERE: apply condition C1 on the attributes in R1,…,Rn GROUP BY the attributes a1,…,ak Apply condition C2 to each group (may have aggregates) Compute aggregates in S and return the result

Group-by v.s. Nested Query Author(login, name) Wrote(login, url) Find authors who wrote ³ 10 documents: Attempt 1: with nested queries SELECT DISTINCT Author.name FROM Author WHERE COUNT( SELECT Wrote.url FROM Wrote WHERE Author.login = Wrote.login) > 10 This is SQL by a novice

Group-by v.s. Nested Query Find all authors who wrote at least 10 documents: Attempt 2: SQL style (with GROUP BY) SELECT Author.name FROM Author, Wrote WHERE Author.login = Wrote.login GROUP BY Author.name HAVING COUNT(Wrote.url) > 10 This is SQL by an expert No need for DISTINCT: automatically from GROUP BY

Group-by vs. Nested Query Which way is more efficient? Attempt #1- With nested: How many times do we do a SFW query over all of the Wrote relations? Attempt #2- With group-by: How about when written this way? With GROUP BY can be much more efficient!

Activity-3-1.ipynb Activity-3-2.ipynb