joins in sqlmathcs.pugetsound.edu/~dchiu/cs455/notes/cs455_3a-sql_3.pdf · cs 455: principles of...

13
CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 ./ R 1 R 2 ./ R 1 R 2 R 1 R 2 ./ ./ R 1 R 2 R1 NATURAL JOIN R2 R1 LEFT OUTER NATURAL JOIN R2 R1 RIGHT OUTER NATURAL JOIN R2 R1 FULL OUTER NATURAL JOIN R2 ./ R 1 R 2 Relational Bowties SQL Equivalent Stylish Bowties (just shown)

Upload: others

Post on 06-Aug-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

Joins in SQL

�41

./R1 R2

./R1 R2

R1 R2./

./R1 R2

R1 NATURAL JOIN R2

R1 LEFT OUTER NATURAL JOIN R2

R1 RIGHT OUTER NATURAL JOIN R2

R1 FULL OUTER NATURAL JOIN R2

./✓R1 R2

Relational Bowties

SQL Equivalent

Stylish Bowties

(just shown)

Page 2: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

Let's Run Some SFW Queries

�42

HomerBartLisaFrankRobertNedFrank

f_namepassengers

JHG

NT

m_nameSimpsonSimpsonSimpsonLovejoyQuimbyFlandersRyerson

l_name111-11-1111444-44-4444222-22-2222555-55-5555666-66-6666777-77-7777333-33-3333

ssn

12345

tail_noplane

BoeingBoeingAirbusMcDonnel DouglasAirbus

make747747A350DC10A380

model525525270380200

capacity570570580610500

mph

555-55-5555111-11-1111222-22-2222777-77-7777333-33-3333666-66-6666444-44-4444777-77-7777555-55-5555

ssnonboard

624531734

flight_no

1234567

flight_noflight

Springfield, ILColumbus, OHNew York, NYParis, FranceHartford, CTMiami, FLAkron, OH

dep_loc7:1516:0012:3015:009:0010:4514:20

dep_timeChicago, ILPortland, ORMiami, FLMunich, GermanyPhoenix, AZAustin, TXHartford, CT

arr_loc4421123

tail_no7:4522:0013:0017:4012:0013:3016:45

arr_time

32F2B1F25A25A30C30C25A25A

seat

For SQL Subquery Lectures

Notes:Airbus A380 not flying any flights

Query:Who (just names) can we expectto land in Munich?

Page 3: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

Let's Run Some SFW Queries

�43

HomerBartLisaFrankRobertNedFrank

f_namepassengers

JHG

NT

m_nameSimpsonSimpsonSimpsonLovejoyQuimbyFlandersRyerson

l_name111-11-1111444-44-4444222-22-2222555-55-5555666-66-6666777-77-7777333-33-3333

ssn

12345

tail_noplane

BoeingBoeingAirbusMcDonnel DouglasAirbus

make747747A350DC10A380

model525525270380200

capacity570570580610500

mph

555-55-5555111-11-1111222-22-2222777-77-7777333-33-3333666-66-6666444-44-4444777-77-7777555-55-5555

ssnonboard

624531734

flight_no

1234567

flight_noflight

Springfield, ILColumbus, OHNew York, NYParis, FranceHartford, CTMiami, FLAkron, OH

dep_loc7:1516:0012:3015:009:0010:4514:20

dep_timeChicago, ILPortland, ORMiami, FLMunich, GermanyPhoenix, AZAustin, TXHartford, CT

arr_loc4421123

tail_no7:4522:0013:0017:4012:0013:3016:45

arr_time

32F2B1F25A25A30C30C25A25A

seat

For SQL Subquery Lectures

Notes:Airbus A380 not flying any flights

Query:All passengers not currentlyon a flight using LEFT JOIN?

Page 4: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

Your Turn!

�44

HomerBartLisaFrankRobertNedFrank

f_namepassengers

JHG

NT

m_nameSimpsonSimpsonSimpsonLovejoyQuimbyFlandersRyerson

l_name111-11-1111444-44-4444222-22-2222555-55-5555666-66-6666777-77-7777333-33-3333

ssn

12345

tail_noplane

BoeingBoeingAirbusMcDonnel DouglasAirbus

make747747A350DC10A380

model525525270380200

capacity570570580610500

mph

555-55-5555111-11-1111222-22-2222777-77-7777333-33-3333666-66-6666444-44-4444777-77-7777555-55-5555

ssnonboard

624531734

flight_no

1234567

flight_noflight

Springfield, ILColumbus, OHNew York, NYParis, FranceHartford, CTMiami, FLAkron, OH

dep_loc7:1516:0012:3015:009:0010:4514:20

dep_timeChicago, ILPortland, ORMiami, FLMunich, GermanyPhoenix, AZAustin, TXHartford, CT

arr_loc4421123

tail_no7:4522:0013:0017:4012:0013:3016:45

arr_time

32F2B1F25A25A30C30C25A25A

seat

For SQL Subquery Lectures

Notes:Airbus A380 not flying any flights

Query 1:First and last names of people assigned to seat 25A

Query 2:List of planes (all attributes)not serving a flight today (don't use Left Outer Join)

Page 5: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

Topics

‣ Structured Query Language (SQL)• Data Definition Language (DDL)

• Data Manipulation Language (DML)- Select

– from, where

– set operations

– rename

– order by

– sub-queries

– aggregation and grouping

- Insert

- Delete

- Update �45

Page 6: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

Renaming Relations and Attributes

‣ Relational Algebra Syntax:• Meaning: Renames to

‣ SQL Syntax

‣ Example:

�46

sqlite> select mph as speed from planes as pl where pl.mph > 500;

speed ----------570 570 580 610

⇢R2(b1,. . . ,bn)(R1)

R1(a1, . . . , an) R2(b1, . . . , bn)

SELECT a1 as b1, a2 as b2, ...FROM R1 as X, R2 as Y, R3 as Z, ...WHERE C;

Page 7: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

Renaming Example

‣ Don't need to rename both tables and attributes!• You can do them separately.

• Just rename some attributes in this example:

�47

sqlite> select f_name as F, l_name as L from passengers where l_name='Simpson';

F L ---------- ----------Homer Simpson Bart Simpson Lisa Simpson

Page 8: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

Notes on Renaming

‣ Important:• Can't refer to a relation or attribute by original name after the rename

• Rename only good for lifetime of query

�48

sqlite> select * from passengers as A where passengers.l_name='Flanders';Error: no such column: passengers.l_name

sqlite> select * from passengers as A where A.l_name='Flanders';

f_name m_name l_name ssn ---------- ---------- ---------- -----------Ned T Flanders 777-77-7777

sqlite> select * from A;Error: no such table: A

Page 9: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

The WITH Clause: Making Temporary Relations

‣ In relational algebra, it was possible to create temporary relations to simplify writing queries:

‣ In SQL, this can be accomplished with the WITH clause:

• Syntax:

�49

WITH tmp1 as (SFW), tmp2 as (SFW), ... SFW -- tmp can be accessed in the subsequent query

tmp1 E1

tmp2 E2

...

//do something with tmpi

Page 10: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

The WITH Clause (cont.)

‣ Example of temporary relations:

�50

sqlite> with simpsonsID as (select ssn from passengers where l_name='Simpson') select * from simpsonsID natural join onboard;

ssn flight_no seat----------- ---------- ----------111-11-1111 2 2B444-44-4444 7 30C222-22-2222 4 1F

sqlite> select * from simpsonsID;Error: no such table: simpsonsID

simpsonsID ⇡ssn(�lname=‘Simpson0(passengers))

simpsonsID ./ onboard<latexit sha1_base64="iou6ygwLoEDFU5Ivmk6VYjnaS10=">AAAF43icjVRLbxMxEHZLAyW8WjhysWgi2kOi3agVHCiqgAMgDuXRh1RHwbuZbNx4bct2+tBqfwE3xJX/w1/gT3CFI/YmgaRbSi2tNDPfN9+Mx2tHijN jg+D73PyVhcrVa4vXqzdu3rp9Z2n57q6RQx3DTiy51PsRNcCZgB3LLId9pYGmEYe9aPDc43tHoA2T4oM9VdBOaSJYj8XUulBnKTIsVUYK8+oFJhx6lmotjzFRrJMZI/JVYliS0k7GBU1h8+P7Ef1hvqqoMSASp722Rkh1WieSx5YBliKSVHc7SytBMygWLhvh2FhB47XdWV5gpCvjYQrCxtyVOQgDZdsZ1ZbFHPIqGRpQNB7QBA6c6Tsz7awYRo7rLtLFPandJywuotMZGU2NOU0jx0yp7ZuzmA/+wer4HfBiVJRjKro4ogmmPIFIU+xVJHcCAo5jmaYOz0ispTF5Rhx0ktWIZa63Wp7PknTR84RFONUJ1IjuyxJzKFztv3LxUJUoTFh3CBDbWSa9gHkx7VCyKaHXzitR3A83JTPZQPGrlLjcQMpmNf1wNXA3CMoMhMqSfqFzpIcccB9Y0rcbyuIuKNsPPGwGTDXCpgvWntZGLm60Cv+Jq1ie8O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Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

Topics

‣ Structured Query Language (SQL)• Data Definition Language (DDL)

• Data Manipulation Language (DML)- Insert

- Delete

- Update

- Select From Where

– joins

– set operations

– order by

– sub-queries

– aggregation and grouping

�51

Page 12: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

ORDER BY: Imposing Order on Results

‣ Databases are based on sets, so they do not have to guarantee ordering on the results• But we can impose an ordering using the

following optional clause:

�52

SFW ORDER BY a1 DESC, a2 DESC, ..., ak DESC;

-- Sorts results by attribute a1 first, then a2, ...

-- Optional DESC keyword sorts results in descending order

Page 13: Joins in SQLmathcs.pugetsound.edu/~dchiu/CS455/notes/CS455_3a-sql_3.pdf · CS 455: Principles of Database Systems - 3a - SQL Joins in SQL 41 R 1./ R 2 R 1./ R 2 R 1./ R 2 R 1./ R

CS 455: Principles of Database Systems - 3a - SQL

Imposing Order on Results (Cont.)

�53

sqlite> select * from plane ORDER BY make, tail_no;

tail_no make model capacity mph ---------- ---------- ---------- ---------- ----------3 Airbus A350 270 580 5 Airbus A380 200 500 1 Boeing 747 525 570 2 Boeing 747 525 570 4 McDonnel D DC10 380 610

sqlite> select * from passengers ORDER BY l_name, f_name DESC;

f_name m_name l_name ssn ---------- ---------- ---------- -----------Ned T Flanders 777-77-7777Frank NULL Lovejoy 555-55-5555Robert N Quimby 666-66-6666Frank NULL Ryerson 333-33-3333Lisa G Simpson 222-22-2222Homer J Simpson 111-11-1111Bart H Simpson 444-44-4444