Lectures 7: Introduction to SQL 6

Slides:



Advertisements
Similar presentations
1 Lecture 4: Advanced SQL. 2 INTERSECT and EXCEPT: (missing from MySQL) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM.
Advertisements

1 Lecture 12: SQL Friday, October 26, Outline Simple Queries in SQL (5.1) Queries with more than one relation (5.2) Subqueries (5.3) Duplicates.
1 Lecture 03: Advanced SQL. 2 Outline Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views Reading:
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 05: SQL Wednesday, October 8, Outline Outer joins (6.3.8) Database Modifications (6.5) Defining Relation Schema in SQL (6.6) Indexes.
1 INTERSECT and EXCEPT: (may no be in MySQL) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A,
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #10 M.P. Johnson Stern School of Business, NYU Spring, 2008.
1 Lecture 02: Basic SQL. 2 Outline Data in SQL Simple Queries in SQL Queries with more than one relation Reading: Chapter 3, “Simple Queries” from SQL.
End of SQL XML April 22 th, Null Values If x=Null then 4*(3-x)/7 is still NULL If x=Null then x=“Joe” is UNKNOWN Three boolean values: –FALSE =
1 Information Systems Chapter 6 Database Queries.
CSE544: SQL Monday 3/27 and Wednesday 3/29, 2006.
1 Lecture 4: More SQL Monday, January 13th, 2003.
1 Lecture 7: End of Normal Forms Outerjoins, Schema Creation and Views Wednesday, January 28th, 2004.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
1 Lecture 04: SQL Wednesday, January 11, Outline Two Examples Nulls (6.1.6) Outer joins (6.3.8) Database Modifications (6.5)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
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.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
NULL VALUES CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  Dealing with null values Three-valued logic Effects in WHERE clauses IS NULL Effects.
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.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
1 Introduction to Database Systems CSE 444 Lecture 04: SQL April 7, 2008.
1 Lecture 5: Outerjoins, Schema Creation and Views Wednesday, January 15th, 2003.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Lectures 2&3: Introduction to SQL
Rob Gleasure robgleasure.com
Lecture 05: SQL Wednesday, January 12, 2005.
Cours 7: Advanced SQL.
Lecture 04: SQL Monday, January 10, 2005.
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Chapter 4: Intermediate SQL Joins
The Best Of Collection (Master Tracks), Vol. 1
Monday 3/27 and Wednesday 3/29, 2006
Introduction to Database Systems CSE 444 Lecture 04: SQL
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
Cse 344 May 14th – Entities.
More SQL Nested and Union queries, and more
Cse 344 January 12th –joins.
March 30th – intro to joins
Cse 344 January 10th –joins.
January 19th – Subqueries 2 and relational algebra
Lecture 4: Advanced SQL – Part II
Lecture 06: SQL Systems Aspects
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CSE544 SQL Wednesday, March 31, 2004.
Rob Gleasure robgleasure.com
Lecture 05 Views, Constraints
SQL: Structured Query Language
Lecture 9: The E/R Model II
Lectures 3: Introduction to SQL Part II
Lectures 3: Introduction to SQL 2
Lectures 5: Introduction to SQL 4
Lecture 4: SQL Thursday, January 11, 2001.
Lecture 10: The E/R Model III
Lectures 6: Introduction to SQL 5
CSC 453 Database Systems Lecture
Lecture 06: SQL Monday, October 11, 2004.
Lecture 4: SQL Wednesday, April 10, 2002.
Rob Gleasure robgleasure.com
Geo-Databases: lecture 4 Complex Queries in SQL
Lectures 2: Introduction to SQL 1
CSE544 SQL Monday, April 5, 2004.
Lecture 04: SQL Monday, October 6, 2003.
Lecture 05: SQL Wednesday, October 9, 2002.
Presentation transcript:

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

3. Advanced SQL-izing

What you will learn about in this section Quantifiers NULLs Outer Joins ACTIVITY: Fancy SQL Pt. II

Quantifiers Product(name, price, company) Company(name, city) Find all companies that make some products with price < 100 SELECT DISTINCT Company.cname FROM Company, Product WHERE Company.cname = Product.manuf AND Product.price < 100 An existential quantifier is a logical quantifier (roughly) of the form “there exists” Existential: easy ! 

Quantifiers Find all companies with products all having price < 100 Product(name, price, company) Company(name, city) Equivalent SELECT DISTINCT Company.cname FROM Company WHERE Company.cname NOT IN( SELECT Product.manuf FROM Product WHERE Product.price >= 100) Find all companies that make only products with price < 100 A universal quantifier is of the form “for all” Universal: hard ! 

NULLS in SQL Whenever we don’t have a value, we can put a NULL Can mean many things: Value does not exists Value exists but is unknown Value not applicable Etc. The schema specifies for each attribute if can be null (nullable attribute) or not How does SQL cope with tables that have NULLs?

Null Values For numerical operations, NULL -> NULL: If x = NULL then 4*(3-x)/7 is still NULL For boolean operations, in SQL there are three values: FALSE = 0 UNKNOWN = 0.5 TRUE = 1 If x= NULL then the comparison x=“Joe” results in UNKNOWN

Null Values C1 AND C2 = min(C1, C2) C1 OR C2 = max(C1, C2) NOT C1 = 1 – C1 SELECT * FROM Person WHERE (age < 25) AND (height > 6 AND weight > 190) Won’t return e.g. (age=20 height=NULL weight=200)! Rule in SQL: include only tuples that yield TRUE (1.0)

Null Values Unexpected behavior: SELECT * FROM Person WHERE age < 25 OR age >= 25 Some Persons are not included !

Null Values Can test for NULL explicitly: x IS NULL x IS NOT NULL SELECT * FROM Person WHERE age < 25 OR age >= 25 OR age IS NULL Now it includes all Persons!

RECAP: Inner Joins By default, joins in SQL are “inner joins”: Product(name, category) Purchase(prodName, store) SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Both equivalent: Both INNER JOINS! SELECT Product.name, Purchase.store FROM Product, Purchase WHERE Product.name = Purchase.prodName

Inner Joins + NULLS = Lost data? By default, joins in SQL are “inner joins”: Product(name, category) Purchase(prodName, store) SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName SELECT Product.name, Purchase.store FROM Product, Purchase WHERE Product.name = Purchase.prodName However: Products that never sold (with no Purchase tuple) will be lost!

Outer Joins An outer join returns tuples from the joined relations that don’t have a corresponding tuple in the other relations I.e. If we join relations A and B on a.X = b.X, and there is an entry in A with X=5, but none in B with X=5… A LEFT OUTER JOIN will return a tuple (a, NULL)! Left outer joins in SQL: SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName Now we’ll get products even if they didn’t sell

INNER JOIN: Product Purchase SELECT Product.name, Purchase.store category Gizmo gadget Camera Photo OneClick prodName store Gizmo Wiz Camera Ritz name store Gizmo Wiz Camera Ritz SELECT Product.name, Purchase.store FROM Product INNER JOIN Purchase ON Product.name = Purchase.prodName Note: another equivalent way to write an INNER JOIN!

LEFT OUTER JOIN: Product Purchase SELECT Product.name, Purchase.store category Gizmo gadget Camera Photo OneClick prodName store Gizmo Wiz Camera Ritz name store Gizmo Wiz Camera Ritz OneClick NULL SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName

Other Outer Joins Left outer join: Right outer join: Full 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 the both left and right tuples even if there’s no match

Activity-3-3.ipynb

Lecture 2 & 3 > SUMMARY Summary SQL is a rich programming language that handles the way data is processed declaratively