M.P. Johnson, DBMS, Stern/NYU, Sp20041 C20.0046: Database Management Systems Lecture #11 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.

Slides:



Advertisements
Similar presentations
SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
Advertisements

Relational Algebra (end) SQL April 19 th, Complex Queries Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store,
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
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:
Subqueries Example Find the name of the producer of ‘Star Wars’.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #9 M.P. Johnson Stern School of Business, NYU Spring, 2008.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2008.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #10 Matthew P. Johnson Stern School of Business, NYU Spring,
Matthew P. Johnson, OCL4, CISDD CUNY, Sept OCL4 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY June, 2005.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY June, 2005.
Matthew P. Johnson, OCL2, CISDD CUNY, January OCL2 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY Fall, 2004.
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 #7 M.P. Johnson Stern School of Business, NYU Spring, 2008.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #12 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 8i: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY Fall, 2004.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #10 M.P. Johnson Stern School of Business, NYU Spring, 2008.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #13 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Matthew P. Johnson, OCL4, CISDD CUNY, Sept OCL4 Oracle 10g: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY June, 2005.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #15 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #5 Matthew P. Johnson CISDD, CUNY June, 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’
Union, Intersection, Difference (SELECT name FROM Person WHERE City=“Seattle”) UNION (SELECT name FROM Person, Purchase WHERE buyer=name AND store=“The.
Complex Queries (1) Product ( pname, price, category, maker)
Integrity Constraints An important functionality of a DBMS is to enable the specification of integrity constraints and to enforce them. Knowledge of integrity.
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
1 Database Systems Lecture #6 Yan Pan School of Software, SYSU 2011.
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:
1 Database Systems Lecture #7 Yan Pan School of Software, SYSU 2011.
1 Lecture 04: SQL Wednesday, January 11, Outline Two Examples Nulls (6.1.6) Outer joins (6.3.8) Database Modifications (6.5)
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
1 Introduction to Database Systems CSE 444 Lecture 02: SQL September 28, 2007.
1 Lecture 02: SQL Friday, September 30, Administrivia Homework 1 is out. Due: Wed., Oct. 12 Did you login on IISQLSRV ? Did you change your password.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Introduction to Database Systems CSE 444 Lecture 04: SQL April 7, 2008.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Lecture 05: SQL Wednesday, January 12, 2005.
Cours 7: Advanced SQL.
COP Introduction to Database Structures
Chap 5. The DB Language (SQL)
Basic SQL Lecture 6 Fall
Introduction to Database Systems CSE 444 Lecture 04: SQL
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
CSE544 SQL Wednesday, March 31, 2004.
SQL Introduction Standard language for querying and manipulating data
Lecture 12: SQL Friday, October 20, 2000.
Lectures 3: Introduction to SQL Part II
Lectures 7: Introduction to SQL 6
Introduction to Database Systems CSE 444 Lecture 02: SQL
Lectures 5: Introduction to SQL 4
Lecture 4: SQL Thursday, January 11, 2001.
Integrity Constraints
Lecture 3 Monday, April 8, 2002.
Lecture 03: SQL Friday, October 3, 2003.
Lecture 3: Relational Algebra and SQL
Lecture 04: SQL Monday, October 6, 2003.
Presentation transcript:

M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #11 Matthew P. Johnson Stern School of Business, NYU Spring, 2004

M.P. Johnson, DBMS, Stern/NYU, Sp Agenda Last time: Started SQL This time: More SQL Homework 2 is up

M.P. Johnson, DBMS, Stern/NYU, Sp Review Examples from sqlzoo.netsqlzoo.net SELECT L FROM R 1, …, R n WHERE C SELECT L FROM R 1, …, R n WHERE C  L (  C (R 1 x … R n )

M.P. Johnson, DBMS, Stern/NYU, Sp First Unintuitive SQLism SELECTR.A FROMR, S, T WHERER.A=S.A OR R.A=T.A Looking for R  (S  T) But what happens if T is empty? See transcript of this in Oracle on salestranscript

M.P. Johnson, DBMS, Stern/NYU, Sp More on escape chars SQL: no official default escape char In SQL*Plus: default escape char == ‘\’  Can set with  SQL> set escape X Other tools, DBMSs: your mileage may very SQL string literals put in ‘ ‘:  ‘mystring’ Single-quote literals escaped with single- quotes:  ‘George’’s string’

M.P. Johnson, DBMS, Stern/NYU, Sp More on single-quotes Bitstrings specified like regular strings but with B:  B’001’ Hex strings with X:  X’7ff’ Dates with DATE:  DATE ‘ ’ Timestamps with TIMESTAMP:  TIMESTAMP ‘ :00:00’

M.P. Johnson, DBMS, Stern/NYU, Sp Set/bag ops in SQL Orthodox SQL has set operators:  UNION, INTERSECT, EXCEPT And bag operators:  UNION ALL, INTERSECT ALL, EXCEPT ALL These operators are applied to queries: (SELECT name FROM Person WHERE City=“Seattle”) UNION (SELECT name FROM Person, Purchase WHERE buyer=name AND store=“The Bon”) (SELECT name FROM Person WHERE City=“Seattle”) UNION (SELECT name FROM Person, Purchase WHERE buyer=name AND store=“The Bon”)

M.P. Johnson, DBMS, Stern/NYU, Sp Set/bag ops in Oracle SQL Oracle SQL support uses MINUS rather than EXCEPT Oracle SQL supports bag op UNION ALL but not INTERSECT ALL or MINUS ALL See the Ullman page on more differencesUllman

M.P. Johnson, DBMS, Stern/NYU, Sp Disambiguation in Oracle SQL Can rename fields by  Select name as n …  Select name n … But not by  Select name=n… Can rename relations only by  … from tab t1, tab t2 Lesson: if you get errors, remove all =s, ASs

M.P. Johnson, DBMS, Stern/NYU, Sp Disambiguation in Oracle SQL Every selected field must be unambiguous For R(A,B),  Select A from R, R   Select R1.A from R R1, R R2 Consider: Why? * is shorthand for all fields, each must be unambiguous  Select * from R R1, R R2 SQL> Select * from R, R; Select * from R, R * ERROR at line 1: ORA-00918: column ambiguously defined SQL> Select * from R, R; Select * from R, R * ERROR at line 1: ORA-00918: column ambiguously defined

M.P. Johnson, DBMS, Stern/NYU, Sp R.A.  SQL People(ssn, name, street, city, state)  assume for clarity that cities are unique Q: Who lives on George’s street? In R.A.:  street=s2 AND city=c2 (  p2(s2,c2) (People) x  street,city (  name=“George” (People))) In SQL? The other way in R.A.: People   street,city (  name=“George” (People)) In SQL? Later on…

M.P. Johnson, DBMS, Stern/NYU, Sp R.A.  SQL Acc(name,ssn,balance) Q: Who has the largest balance? In R.A.:  name (Acc) -  a2.name (  a2.bal < Acc.bal (Acc x  a2 (Acc))) In SQL?

M.P. Johnson, DBMS, Stern/NYU, Sp Nulls in SQL If we don’t have a value, can put a NULL Null can mean several things:  Value does not exists  Value exists but is unknown  Value not applicable The schema specifies whether null is allowed for each attribute  not null if not allowed  Otherwise, null is allowed

M.P. Johnson, DBMS, Stern/NYU, Sp Null Values x = NULL  4*(3-x)/7 = NULL x = NULL  x + 3 – x = NULL x = NULL  3 + (x-x) = NULL x = NULL  x = ‘Joe’ is UNKNOWN In general: no row use null fields appear in the selection test will pass the test Pace Boole, SQL has three boolean values:  FALSE=0  TRUE=1  UNKNOWN=0.5

M.P. Johnson, DBMS, Stern/NYU, Sp Null values in boolean expressions C1 AND C2= min(C1, C2) C1 OR C2= max(C1, C2) NOT C1= 1 – C1 height > 6 = UNKNOWN  UNKNOWN OR weight > 190 = UNKOWN  (age < 25) AND UNKNOWN = UNKNOWN SELECT * FROM Person WHERE (age < 25) AND (height > 6 OR weight > 190) SELECT * FROM Person WHERE (age < 25) AND (height > 6 OR weight > 190) E.g. age=20 heigth=NULL weight=200

M.P. Johnson, DBMS, Stern/NYU, Sp Comparing null and non-nulls Unexpected behavior: Some Persons are not included! The “trichotomy law” does not hold! SELECT * FROM Person WHERE age = 25 SELECT * FROM Person WHERE age = 25

M.P. Johnson, DBMS, Stern/NYU, Sp Testing for null values Can test for NULL explicitly:  x IS NULL  x IS NOT NULL Now it includes all Persons SELECT * FROM Person WHERE age = 25 OR age IS NULL SELECT * FROM Person WHERE age = 25 OR age IS NULL

M.P. Johnson, DBMS, Stern/NYU, Sp Evaluation strategies for SQL queries Semantics of a SQL query defined in terms of the following conceptual evaluation strategy:  Compute the cross-product of relation-list in FROM clause  Discard resulting tuples if they fail WHERE clause  Delete attributes that are not in SELECT clause  If DISTINCT is specified, eliminate duplicate rows Often the least efficient way to compute a query! Optimizer finds better ways, but result is the same

M.P. Johnson, DBMS, Stern/NYU, Sp Subqueries (5.3) Powerful feature of SQL: one clause can contain other SQL queries!  So can FROM and HAVING clauses Several ways:  Selection  single constant (scalar) in WHERE  Selection  relation in WHERE  Selection  relation in FROM  Etc.

M.P. Johnson, DBMS, Stern/NYU, Sp Subquery motivation Consider standard multi-table example:  Purchase(prodname, buyerssn, etc.)  Person(name, ssn, etc.)  What did Conrad buy? As usual, need to AND on equality identifying ssn’s row and buyerssn’s row SELECT Purchase.prodname FROM Purchase, Person WHERE buyerssn = ssn AND name = ‘Conrad’

M.P. Johnson, DBMS, Stern/NYU, Sp Subquery motivation Purchase(prodname, buyerssn, etc.) Person(name, ssn, etc.) What did Conrad buy? Natural intuition: 1.Go find Conrad’s ssn 2.Then find purchases SELECT ssn FROM Person WHERE name = ‘Conrad’ SELECT Purchase.prodname FROM Purchase, Person WHERE buyerssn = Conrad’s-ssn

M.P. Johnson, DBMS, Stern/NYU, Sp Subqueries Subquery: copy in Conrad’s selection for his ssn: The subquery returns one value, so the = is valid If it returns more, we get a run-time error. SELECT Purchase.prodname FROM Purchase WHERE buyerssn = (SELECT ssn FROM Person WHERE name = ‘Conrad’) SELECT Purchase.prodname FROM Purchase WHERE buyerssn = (SELECT ssn FROM Person WHERE name = ‘Conrad’)

M.P. Johnson, DBMS, Stern/NYU, Sp Operators on selections Several new operators applied to (unary) selections: 1. EXISTS R 2. s > ALL R 3. s > ANY R > is just an example op Each expression can be negated with NOT

M.P. Johnson, DBMS, Stern/NYU, Sp Subqueries returning relations Q: Find companies Martha bought from Intuition:  Find Martha’s ssn  Find Martha’s products  Find those products’ companies SELECT Product.maker FROM Product WHERE Product.name IN (SELECT Purchase.product FROM Purchase WHERE Purchase.buyerssn = (SELECT ssn FROM Person WHERE name = ‘Martha’)) SELECT Product.maker FROM Product WHERE Product.name IN (SELECT Purchase.product FROM Purchase WHERE Purchase.buyerssn = (SELECT ssn FROM Person WHERE name = ‘Martha’))

M.P. Johnson, DBMS, Stern/NYU, Sp Subqueries returning relations Equivalent to: But are they really equivalent? Make both distinct to be sure SELECT Product.maker FROM Product, Purchase, People WHERE Product.name = Purchase.product AND Purchase.buyerssn = ssn AND name = ‘Martha’ SELECT Product.maker FROM Product, Purchase, People WHERE Product.name = Purchase.product AND Purchase.buyerssn = ssn AND name = ‘Martha’

M.P. Johnson, DBMS, Stern/NYU, Sp Subqueries returning relations SELECT name FROM Product WHERE price > ALL (SELECT price FROM Purchase WHERE maker=‘Gizmo-Works’) SELECT name FROM Product WHERE price > ALL (SELECT price FROM Purchase WHERE maker=‘Gizmo-Works’) Product (pname, price, category, maker) Find products that are more expensive than all Gizmo-Works products You can also use: s > ALL R s > ANY R EXISTS R

M.P. Johnson, DBMS, Stern/NYU, Sp Correlated Queries So far, subquery executed once; result used for higher query More complicated: correlated queries “[T]he subquery… [is] evaluated many times, once for each assignment of a value to some term in the subquery that comes from a tuple variable outside the subquery” (p286). Q: What does this mean? A: That subqueries refer to vars from outer qureries

M.P. Johnson, DBMS, Stern/NYU, Sp Correlated Queries Movie (title, year, director, length) Q: Find titles that are titles of multiple movies Note (1) scope of variables (2) this can still be expressed as single SFW SELECT DISTINCT title FROM Movie AS x WHERE year <> ANY (SELECT year FROM Movie WHERE title = x.title); SELECT DISTINCT title FROM Movie AS x WHERE year <> ANY (SELECT year FROM Movie WHERE title = x.title); correlation

M.P. Johnson, DBMS, Stern/NYU, Sp Complex Correlated Query Product (pname, price, category, maker, year) Find products (and their manufacturers) that are more expensive than all products made by the same manufacturer before 1972 Powerful, but much harder to optimize! SELECT DISTINCT pname, maker FROM Product AS x WHERE price > ALL (SELECT price FROM Product AS y WHERE x.maker = y.maker AND y.year < 1972); SELECT DISTINCT pname, maker FROM Product AS x WHERE price > ALL (SELECT price FROM Product AS y WHERE x.maker = y.maker AND y.year < 1972);

M.P. Johnson, DBMS, Stern/NYU, Sp FROM subqueries Recall Q: Which companies did Martha buy from? Before: found ssn, found products, found companies SELECT Product.maker FROM Product WHERE Product.name IN (SELECT Purchase.product FROM Purchase WHERE Purchase.buyerssn = (SELECT ssn FROM Person WHERE name = ‘Martha’)) SELECT Product.maker FROM Product WHERE Product.name IN (SELECT Purchase.product FROM Purchase WHERE Purchase.buyerssn = (SELECT ssn FROM Person WHERE name = ‘Martha’))

M.P. Johnson, DBMS, Stern/NYU, Sp FROM subqueries Motivation for another way: suppose we’re given Martha’s purchases Then could just cross with Products and select identified rows  Substitute (named) subquery for Martha’s purchases SELECT Product.maker FROM Product, (SELECT Purchase.product FROM Purchase WHERE Purchase.buyerssn = (SELECT ssn FROM Person WHERE name = ‘Martha’)) Marthas WHERE Product.name = Martha.product SELECT Product.maker FROM Product, (SELECT Purchase.product FROM Purchase WHERE Purchase.buyerssn = (SELECT ssn FROM Person WHERE name = ‘Martha’)) Marthas WHERE Product.name = Martha.product

M.P. Johnson, DBMS, Stern/NYU, Sp Existential/Universal Conditions Product (pname, price, company) Company(cname, city) Find all companies s.t. some of their products have price < 100 SELECT DISTINCT Company.cname FROM Company, Product WHERE Company.cname = Product.company and Produc.price < 100 SELECT DISTINCT Company.cname FROM Company, Product WHERE Company.cname = Product.company and Produc.price < 100 Existential: easy!

M.P. Johnson, DBMS, Stern/NYU, Sp Existential/Universal Conditions Product (pname, price, company) Company(cname, city) Find all companies s.t. all of their products have price < 100 Universal: hard!

M.P. Johnson, DBMS, Stern/NYU, Sp Existential/universal with IN 2. Find all companies s.t. all their products have price < Find the other companies: i.e. s.t. some product  100 SELECT DISTINCT Company.cname FROM Company WHERE Company.cname IN (SELECT Product.company FROM Product WHERE Produc.price >= 100 SELECT DISTINCT Company.cname FROM Company WHERE Company.cname IN (SELECT Product.company FROM Product WHERE Produc.price >= 100 SELECT DISTINCT Company.cname FROM Company WHERE Company.cname NOT IN (SELECT Product.company FROM Product WHERE Produc.price >= 100 SELECT DISTINCT Company.cname FROM Company WHERE Company.cname NOT IN (SELECT Product.company FROM Product WHERE Produc.price >= 100

M.P. Johnson, DBMS, Stern/NYU, Sp More on Set-Comparison Operators We’ve already seen IN R, NOT IN R. Can also use EXISTS R, NOT EXISTS R Also available: op ANY R, op ALL R Find sailors whose rating is greater than that of some sailor called Horatio: SELECT R.SID FROM Reserves R WHERE R.rating > ANY ( SELECT R2.rating FROM Reserves R2 WHERE R2.sname=‘Horatio’)

M.P. Johnson, DBMS, Stern/NYU, Sp Joins operations Variations:  Cross join (Cartesian product)  Join … On  Natural join  Outer join Apply to relations appearing in selections

M.P. Johnson, DBMS, Stern/NYU, Sp Cross join - example NameAddressGenderBirthdate Hanks123 Palm RdM01/01/60 Taylor456 Maple AvF02/02/40 Lucas789 Oak StM03/03/55 NameAddressNetworth Spielberg246 Palm Rd10M Taylor456 Maple Av20M Lucas789 Oak St30M MovieStar MovieExec

M.P. Johnson, DBMS, Stern/NYU, Sp Cross join – example Select * From MovieStar Cross Join MovieExec MovieS tar.nam e MovieStar.add ress MovieSta r. Gender MovieStar.Birthdate MovieEx ec. Name MovieExec.Ad dress MovieEx ec. Networth Hanks123 Palm RdM01/01/60Spielberg246 Palm Rd10M Hanks123 Palm RdM01/01/60Taylor456 Maple Av20M Hanks123 Palm RdM01/01/60Lucas789 Oak St30M Taylor456 Maple AvF02/02/40Spielberg246 Palm Rd10M Taylor456 Maple AvF02/02/40Taylor456 Maple Av20M Taylor456 Maple AvF02/02/40Lucas789 Oak St30M Lucas789 Oak StM03/03/55Spielberg246 Palm Rd10M Lucas789 Oak StM03/03/55Taylor456 Maple Av20M Lucas789 Oak StM03/03/55Lucas789 Oak St30M Row

M.P. Johnson, DBMS, Stern/NYU, Sp Join … On: example Select * From MovieStar Join MovieExec On MovieStar.Name <> MovieExec. Name MovieSt ar.name MovieStar.addr ess MovieSta r. Gender MovieStar. Birthdate MovieExe c. Name MovieExec.Add ress MovieEx ec. Networth Hanks123 Palm RdM01/01/60Spielber g 246 Palm Rd10M Hanks123 Palm RdM01/01/60Taylor456 Maple Av20M Hanks123 Palm RdM01/01/60Lucas789 Oak St30M Taylor456 Maple AvF02/02/40Spielber g 246 Palm Rd10M Taylor456 Maple AvF02/02/40Taylor456 Maple Av20M Taylor456 Maple AvF02/02/40Lucas789 Oak St30M Lucas789 Oak StM03/03/55Spielber g 246 Palm Rd10M Lucas789 Oak StM03/03/55Taylor456 Maple Av20M Lucas789 Oak StM03/03/55Lucas789 Oak St30M Row

M.P. Johnson, DBMS, Stern/NYU, Sp Natural Joins MovieStar(name, address, gender, birthdate) MovieExec(name, address, networth) Natural Join: MovieStar Natural Join MovieExec; Results in: list of individuals who are movie- stars as well as executives: (Name, address, gender, birthdate, networth)

M.P. Johnson, DBMS, Stern/NYU, Sp Example - Natural join NameAddressGenderBirthdate Hanks123 Palm RdM01/01/60 Taylor456 Maple AvF02/02/40 Lucas789 Oak StM03/03/55 NameAddressNetworth Spielberg246 Palm Rd10M Taylor456 Maple Av 20M Lucas789 Oak St30M MovieStar MovieExec NameAddressGenderBirthdateNetworth Taylor456 Maple AvF02/02/4020M Lucas789 Oak StM03/03/5530M Select *from MovieStar Natural Join MovieExec

M.P. Johnson, DBMS, Stern/NYU, Sp Outer Join - Example NameAddressGenderBirthdate Hanks123 Palm RdM01/01/60 Taylor456 Maple AvF02/02/40 Lucas789 Oak StM03/03/55 NameAddressNetworth Spielberg246 Palm Rd10M Taylor456 Maple Av20M Lucas789 Oak St30M NameAddressGenderBirthdateNetworth Hanks123 Palm RdM01/01/60NULL Spielberg246 Palm RdNULL 10M Taylor456 Maple AvF02/02/4020M Lucas789 Oak StM03/03/5530M MovieStarMovieExec Select *from MovieStar NATURAL FULL OUTER JOIN MovieExec

M.P. Johnson, DBMS, Stern/NYU, Sp Outer Join - Example Select *from MovieStar LEFT OUTER JOIN MovieExec NameAddressGenderBirthdateNetworth Hanks123 Palm RdM01/01/60NULL Spielberg246 Palm RdNULL 10M Taylor456 Maple AvF02/02/4020M Lucas789 Oak StM03/03/5530M Select *from MovieStar RIGHT OUTER JOIN MovieExec NameAddressGenderBirthdateNetworth Hanks123 Palm RdM01/01/60NULL Spielberg246 Palm RdNULL 10M Taylor456 Maple AvF02/02/4020M Lucas789 Oak StM03/03/5530M

M.P. Johnson, DBMS, Stern/NYU, Sp R.A.  SQL People(ssn, name, street, city, state)  assume for clarity that cities are unique Q: Who lives on George’s street? Now, the second way in R.A.: People   street,city (  name=“George” (People)) In SQL?

M.P. Johnson, DBMS, Stern/NYU, Sp Live Examples Examples from sqlzoo.netsqlzoo.net