Introduction to Database Systems CSE 444

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

Introduction to Database Systems CSE 414

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

Lecture 2: Introduction to SQL

Database Systems CSE 414

SQL. Assit.Prof Dr. Anantakul Intarapadung

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

SQL. Structured Query Language

CSE 344 JANUARY 19 TH SUBQUERIES 2 AND RELATIONAL ALGEBRA

CSE 544 Principles of Database Management Systems

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

Big Data Processing Technologies. Chentao Wu Associate Professor Dept. of Computer Science and Engineering

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

Lecture 3: SQL Part II

Database design and implementation CMPSCI 645. Lecture 04: SQL and Datalog

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

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

CSE 544 Principles of Database Management Systems

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

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

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

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

Introduction to Data Management CSE 414

DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

INF 212 ANALYSIS OF PROG. LANGS SQL AND SPREADSHEETS. Instructors: Crista Lopes Copyright Instructors.

Lectures 2&3: Introduction to SQL

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

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

Before we talk about Big Data. Lets talk about not-so-big data

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

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

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

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

Introduction to Databases CSE 414. Lecture 2: Data Models

Introduction to Data Management CSE 344

CSE 344 JANUARY 8 TH SQLITE AND JOINS

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

Introduction to Relational Databases

Introduction to Data Management CSE 344. Lecture 2: Data Models

Database Systems CSE 414

Principles of Database Systems CSE 544p. Lecture #1 September 28, 2011

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

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

COMP 430 Intro. to Database Systems

Introduction to Data Management CSE 344. Lecture 12: Relational Calculus

CS 564 Midterm Review

MTAT Introduction to Databases

Keys, SQL, and Views CMPSCI 645

Introduction to Data Management CSE 344

COMP 430 Intro. to Database Systems

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

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

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

Introduction to Database Systems CSE 414

Data Informatics. Seon Ho Kim, Ph.D.

CompSci 516: Database Systems

Announcements. Using Electronics in Class. Review. Staff Instructor: Alvin Cheung Office hour on Wednesdays, 1-2pm. Class Overview

CSE344 Midterm Exam Fall 2016

Introduction to Data Management CSE 344. Lecture 14: Relational Calculus

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

The Relational Data Model

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

CSC 261/461 Database Systems Lecture 7

Chapter 6 The database Language SQL as a tutorial

CSC 261/461 Database Systems Lecture 5. Fall 2017

SQLite, Firefox, and our small IMDB movie database. CS3200 Database design (sp18 s2) Version 1/17/2018

Lecture 5 Design Theory and Normalization

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

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

CSC 261/461 Database Systems Lecture 6. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

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

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

EXTENDED RELATIONAL ALGEBRA OUTERJOINS, GROUPING/AGGREGATION INSERT/DELETE/UPDATE

SQL Continued! Outerjoins, Aggregations, Grouping, Data Modification

Please pick up your name card

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

Midterm Review. Winter Lecture 13

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

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

Introduction to Database Systems. The Relational Data Model

SQL: Data Manipulation Language

Querying Data with Transact SQL

Introduction to Database Systems. The Relational Data Model. Werner Nutt

More SQL. Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update

Lecture 04: SQL. Monday, April 2, 2007

Chapter 6 The database Language SQL as a tutorial

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

SQL: Data Definition Language

Introduction to SQL. Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

8. Relational Calculus (Part II)

CSE 544 Data Models. Lecture #3. CSE544 - Spring,

Lecture 04: SQL. Wednesday, October 4, 2006

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Database Systems CSE 414

CSE 344 JULY 30 TH DB DESIGN (CH 4)

The Entity-Relationship Model (ER Model) - Part 1

CPSC 504 Background (aka, all you need to know about databases for this course in two lectures) Rachel Pottinger January 8 and 12, 2015

Introduction to Data Management CSE 344

Transcription:

Introduction to Database Systems CSE 444 Lecture 2: SQL

Announcements Project 1 & Hw 1 are posted on class website Project 1 (SQL) due in two weeks Homework 1 (E/R models etc) due in three weeks Remember: 3me goes by very fast! Start early! On the course website you will find Recommended readings from the book PDF of lecture notes (~morning of class) Other 1

Outline Data in SQL Simple Queries in SQL (6.1) Queries with more than one rela3on (6.2) Subqueries (6.3) 2

Structured Query Language (SQL) Data Defini3on Language (DDL) Create/alter/delete tables and their a"ributes Following lectures... Data Manipula3on Language (DML) Query one or more tables discussed next! Insert/delete/modify tuples in tables 3

Tables in SQL Attribute names Product Table name Key PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Tuple / row Attribute 4

Data Types in SQL Atomic types Character strings: CHAR(20), VARCHAR(50) Can be of fixed or variable length Numbers: INT, BIGINT, SMALLINT, FLOAT Others: MONEY, DATETIME, Record (aka tuple) Has atomic a"ributes Table (aka rela3on) A set of tuples Book Sec. 2.3.2 5

Simple SQL Query SELECT * FROM Product WHERE category='gadgets' Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Selection PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks 6

Simple SQL Query Product SELECT pname, price, manufacturer FROM Product WHERE price > 100 PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Selection & Projection PName Price Manufacturer SingleTouch $149.99 Canon MultiTouch $203.99 Hitachi 7

Details SQL is case insensi3ve SELECT = Select = select Product = product but 'Sea"le' 'sea"le' (in general) Constants must use single quotes 'abc' - yes "abc" - no 8

Eliminating Duplicates Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks PowerGizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi SELECT category FROM Product Set vs. Bag semantics Category Gadgets Gadgets Photography Household SELECT DISTINCT category FROM Product Category Gadgets Photography Household 9

Ordering the Results SELECT pname, price, manufacturer FROM Product WHERE category='gadgets' and price > 10 ORDER BY price, pname Ties in price attribute broken by pname attribute Ordering is ascending by default. Descending:... ORDER BY price, pname DESC 10

Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi SELECT DISTINCT category FROM Product ORDER BY category SELECT category FROM Product ORDER BY pname?? SELECT DISTINCT category FROM Product ORDER BY pname? 11

Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi SELECT DISTINCT category FROM Product ORDER BY category SELECT category FROM Product ORDER BY pname SELECT DISTINCT category FROM Product ORDER BY pname * Error actually happens during "seman3c" analysis of query. Category Gadgets Household Photography Category Gadgets Household Gadgets Photography Syntax error* 12 x

Keys and Foreign Keys Key Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Foreign key Key Company CName StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 Japan 13

Joins Product (pname, price, category, manufacturer) Company (cname, stockprice, country) Q: Find all products under $200 manufactured in Japan; return their names and prices! SELECT pname, price FROM Product, Company WHERE manufacturer=cname and country='japan' and price <= 200 Join b/w Product and Company 14

Joins Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Company CName StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 Japan SELECT pname, price FROM Product, Company WHERE manufacturer=cname and country='japan' and price <= 200 PName Price SingleTouch $149.99 15

Tuple Variables Person (pname, address, works_for) Company (cname, address which address? SELECT DISTINCT pname, address FROM Person, Company WHERE works_for = cname SELECT DISTINCT Person.pName, Company.address FROM Person, Company WHERE Person.works_for = Company.cName SELECT DISTINCT X.pName, Y.address FROM Person as X, Company as Y WHERE X.works_for = Y.cName "as" is optional 16 x

In Class Product (pname, price, category, manufacturer) Company (cname, stockprice, country) Q: Find all Chinese companies that manufacture products in the Toy category! SELECT cname FROM WHERE 17

In Class Product (pname, price, category, manufacturer) Company (cname, stockprice, country) Q: Find all Chinese companies that manufacture products in the Toy category! SELECT DISTINCT cname FROM Product P, Company WHERE country = 'China' and P.category = 'Toy' and P.manufacturer = cname 18

In Class Product (pname, price, category, manufacturer) Company (cname, stockprice, country) Q: Find all Chinese companies that manufacture products both in the Toy and Electronic categories. SELECT DISTINCT cname FROM WHERE 19

In Class Product (pname, price, category, manufacturer) Company (cname, stockprice, country) Q: Find all Chinese companies that manufacture products both in the Toy and Electronic categories. SELECT DISTINCT cname FROM Product P1, Product P2, Company WHERE country = 'China' and P1.category = 'Toy' and P2.category = 'Electronic' and P1.manufacturer = cname and P2.manufacturer = cname 20

Meaning (Semantics) of SQL Queries SELECT a 1, a 2,, a k FROM R 1 as x 1, R 2 as x 2,, R n as x n WHERE Conditions Conceptual evaluation strategy (nested for loops): Answer = {} for x 1 in R 1 do for x 2 in R 2 do.. for x n in R n do if Conditions then Answer = Answer {(a 1,,a k )} return Answer 21

Using the Formal Semantics What do these queries compute? R(a), S(a), T(a) SELECT DISTINCT R.a FROM R, S WHERE R.a=S.a Returns R S SELECT DISTINCT R.a FROM R, S, T WHERE R.a=S.a or R.a=T.a If S and T then returns R (S T) else returns 22

Joins Introduce Duplicates Product (pname, price, category, manufacturer) Company (cname, stockprice, country) Q: Find all Chinese companies that manufacture some product in the Gadgets category! SELECT country FROM Product, Company WHERE manufacturer = cname and category = 'Gadgets' 23

Joins Introduce Duplicates Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Company CName StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 Japan SELECT country FROM Product, Company WHERE manufacturer = cname and category = 'Gadgets' Country USA USA Remember to use DISTINCT 24

Subqueries A subquery is a SQL query nested inside a larger query Such inner- outer queries are called nested queries A subquery may occur in: A SELECT clause A FROM clause A WHERE clause Rule of thumb: avoid wri3ng nested queries when possible; keep in mind that some3mes it s impossible 25

1. Subqueries in SELECT Product (pname, price, cid) Company (cid, cname, city) Q: For each product return the city where it is manufactured! SELECT P.pname, ( SELECT C.city FROM Company C WHERE C.cid = P.cid) FROM Product P What happens if the subquery returns more than one city? Runtime error 26 x

1. Subqueries in SELECT Product (pname, price, cid) Company (cid, cname, city) Q: For each product return the city where it is manufactured! SELECT P.pname, ( SELECT C.city FROM Company C WHERE C.cid = P.cid) FROM Product P "unnesting the query" SELECT P.pname, C.city FROM Product P, Company C WHERE C.cid = P.cid Whenever possible, don't use nested queries 27 x

1. Subqueries in SELECT Product (pname, price, cid) Company (cid, cname, city) Q: Compute the number of products made by each company! SELECT C.cname, ( SELECT count (*) FROM Product P WHERE P.cid = C.cid) FROM Company C Better: we can unnest by using a GROUP BY (next lecture) 28 x

2. Subqueries in FROM Product (pname, price, cid) Company (cid, cname, city) Q: Find all products whose prices is > 20 and < 30! SELECT X.pname FROM ( SELECT * FROM Product as P WHERE price >20 ) as X WHERE X.price < 30 unnesting SELECT pname FROM Product WHERE price > 20 and price < 30 29

3. Subqueries in WHERE Product (pname, price, cid) Company (cid, cname, city) Existential quantifiers Q: Find all companies that make some products with price < 100! Using EXISTS: SELECT DISTINCT C.cname FROM Company C WHERE EXISTS ( SELECT * FROM Product P WHERE C.cid = P.cid and P.price < 100) 30

3. Subqueries in WHERE Product (pname, price, cid) Company (cid, cname, city) Existential quantifiers Q: Find all companies that make some products with price < 100! Using IN: SELECT DISTINCT C.cname FROM Company C WHERE C.cid IN ( SELECT P.cid FROM Product P WHERE P.price < 100) 31

3. Subqueries in WHERE Product (pname, price, cid) Company (cid, cname, city) Existential quantifiers Q: Find all companies that make some products with price < 100! Using ANY: SELECT DISTINCT C.cname FROM Company C WHERE 100 > ANY ( SELECT price FROM Product P WHERE P.cid = C.cid) 32

3. Subqueries in WHERE Product (pname, price, cid) Company (cid, cname, city) Existential quantifiers Q: Find all companies that make some products with price < 100! Now, let's unnest: SELECT DISTINCT C.cname FROM Company C, Product P WHERE C.cid = P.cid and P.price < 100 Existential quantifiers are easy! 33

3. Subqueries in WHERE Product (pname, price, cid) Company (cid, cname, city) Universal quantifiers Q: Find all companies that make only products with price < 100! same as: Q: Find all companies for which all products have price < 100! Universal quantifiers are more complicated! 34 x

3. Subqueries in WHERE 1. Find the other companies: i.e. they have some product 100! SELECT DISTINCT C.cname FROM Company C WHERE C.cid IN ( SELECT P.cid FROM Product P WHERE P.price >= 100) 2. Find all companies s.t. all their products have price < 100! SELECT DISTINCT C.cname FROM Company C WHERE C.cid NOT IN ( SELECT P.cid FROM Product P WHERE P.price >= 100) 35

3. Subqueries in WHERE Product (pname, price, cid) Company (cid, cname, city) Universal quantifiers Q: Find all companies that make only products with price < 100! Using NOT EXISTS: SELECT DISTINCT C.cname FROM Company C WHERE NOT EXISTS ( SELECT * FROM Product P WHERE C.cid = P.cid and P.price >= 100) 36

3. Subqueries in WHERE Product (pname, price, cid) Company (cid, cname, city) Universal quantifiers Q: Find all companies that make only products with price < 100! Using ALL: SELECT DISTINCT C.cname FROM Company C WHERE 100 > ALL ( SELECT price FROM Product P WHERE P.cid = C.cid) 37

Question for Database Fans & Friends How can we unnest the universal quan,fier query? 38

Queries that must be nested A query Q is monotone if: Whenever we add tuples to one or more of the tables the answer to the query cannot contain fewer tuples Fact: all unnested queries are monotone Proof: using the nested for loops seman3cs Fact: Query with universal quan3fier is not monotone Add one tuple viola3ng the condi3on. Then not "all"... Consequence: we cannot unnest a query with a universal quan3fier Same argument holds for queries with nega3on 39 x

The drinkers-bars-beers example Likes(drinker, beer) Frequents(drinker, bar) Serves(bar, beer) Challenge: write these in SQL Find drinkers that frequent some bar that serves some beer they like. x: y. z. Frequents(x, y) Serves(y,z) Likes(x,z) Find drinkers that frequent only bars that serves some beer they like. x: y. Frequents(x, y) ( z. Serves(y,z) Likes(x,z)) Find drinkers that frequent some bar that serves only beers they like. x: y. Frequents(x, y) z.(serves(y,z) Likes(x,z)) Find drinkers that frequent only bars that serves only beer they like. x: y. Frequents(x, y) z.(serves(y,z) Likes(x,z)) 40