61A Lecture 32 Announcements Joining Tables Reminder: John the - - PowerPoint PPT Presentation

61a lecture 32 announcements joining tables reminder john
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 32 Announcements Joining Tables Reminder: John the - - PowerPoint PPT Presentation

61A Lecture 32 Announcements Joining Tables Reminder: John the Patriotic Dog Breeder E isenhower F illmore A braham D elano G rover B arack C linton H erbert 4 Reminder: John the Patriotic Dog Breeder E isenhower select "abraham" as


slide-1
SLIDE 1

61A Lecture 32

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Joining Tables

slide-4
SLIDE 4

Reminder: John the Patriotic Dog Breeder

4

Delano Herbert Clinton Abraham Barack Fillmore Eisenhower Grover

slide-5
SLIDE 5

Reminder: John the Patriotic Dog Breeder

4

Delano Herbert Clinton Abraham Barack Fillmore Eisenhower Grover

select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union select "delano" , "herbert" union select "fillmore" , "abraham" union select "fillmore" , "delano" union select "fillmore" , "grover" union select "eisenhower" , "fillmore";

slide-6
SLIDE 6

Reminder: John the Patriotic Dog Breeder

4

Delano Herbert Clinton Abraham Barack Fillmore Eisenhower Grover

create table parents as select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union select "delano" , "herbert" union select "fillmore" , "abraham" union select "fillmore" , "delano" union select "fillmore" , "grover" union select "eisenhower" , "fillmore";

slide-7
SLIDE 7

Reminder: John the Patriotic Dog Breeder

4

Delano Herbert Clinton Abraham Barack Fillmore Eisenhower Grover

create table parents as Parent Child abraham barack abraham clinton delano herbert fillmore abraham fillmore delano fillmore grover eisenhower fillmore Parents: select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union select "delano" , "herbert" union select "fillmore" , "abraham" union select "fillmore" , "delano" union select "fillmore" , "grover" union select "eisenhower" , "fillmore";

slide-8
SLIDE 8

Joining Two Tables

5

slide-9
SLIDE 9

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

slide-10
SLIDE 10

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

A

create table dogs as select "abraham" as name, "long" as fur union

slide-11
SLIDE 11

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

A B

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union

slide-12
SLIDE 12

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

A B C

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union

slide-13
SLIDE 13

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

A D B C

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union select "delano" , "long" union

slide-14
SLIDE 14

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

E A D B C

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union select "delano" , "long" union select "eisenhower" , "short" union

slide-15
SLIDE 15

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

E F A D B C

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union select "delano" , "long" union select "eisenhower" , "short" union select "fillmore" , "curly" union

slide-16
SLIDE 16

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

E F A D G B C

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union select "delano" , "long" union select "eisenhower" , "short" union select "fillmore" , "curly" union select "grover" , "short" union

slide-17
SLIDE 17

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

E F A D G B C H

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union select "delano" , "long" union select "eisenhower" , "short" union select "fillmore" , "curly" union select "grover" , "short" union select "herbert" , "curly";

slide-18
SLIDE 18

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

E F A D G B C H

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union select "delano" , "long" union select "eisenhower" , "short" union select "fillmore" , "curly" union select "grover" , "short" union select "herbert" , "curly"; create table parents as select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union ...;

slide-19
SLIDE 19

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

E F A D G B C H

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union select "delano" , "long" union select "eisenhower" , "short" union select "fillmore" , "curly" union select "grover" , "short" union select "herbert" , "curly"; create table parents as select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union ...; Select the parents of curly-furred dogs

slide-20
SLIDE 20

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

E F A D G B C H

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union select "delano" , "long" union select "eisenhower" , "short" union select "fillmore" , "curly" union select "grover" , "short" union select "herbert" , "curly"; create table parents as select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union ...; Select the parents of curly-furred dogs select parent from parents, dogs where child = name and fur = "curly";

slide-21
SLIDE 21

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

E F A D G B C H

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union select "delano" , "long" union select "eisenhower" , "short" union select "fillmore" , "curly" union select "grover" , "short" union select "herbert" , "curly"; create table parents as select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union ...; Select the parents of curly-furred dogs select parent from parents, dogs where child = name and fur = "curly";

slide-22
SLIDE 22

Joining Two Tables

Two tables A & B are joined by a comma to yield all combos of a row from A & a row from B

5

E F A D G B C H

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union select "clinton" , "long" union select "delano" , "long" union select "eisenhower" , "short" union select "fillmore" , "curly" union select "grover" , "short" union select "herbert" , "curly"; create table parents as select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union ...; Select the parents of curly-furred dogs select parent from parents, dogs where child = name and fur = "curly"; (Demo)

slide-23
SLIDE 23

Aliases and Dot Expressions

slide-24
SLIDE 24

Joining a Table with Itself

7

E F A D G B C H

slide-25
SLIDE 25

Joining a Table with Itself

Two tables may share a column name; dot expressions and aliases disambiguate column values

7

E F A D G B C H

slide-26
SLIDE 26

Joining a Table with Itself

Two tables may share a column name; dot expressions and aliases disambiguate column values

7

E F A D G B C H

select [columns] from [table] where [condition] order by [order];

slide-27
SLIDE 27

Joining a Table with Itself

Two tables may share a column name; dot expressions and aliases disambiguate column values

7

E F A D G B C H

select [columns] from [table] where [condition] order by [order]; [table] is a comma-separated list of table names with optional aliases

slide-28
SLIDE 28

Joining a Table with Itself

Two tables may share a column name; dot expressions and aliases disambiguate column values

7

E F A D G B C H

select [columns] from [table] where [condition] order by [order]; [table] is a comma-separated list of table names with optional aliases Select all pairs of siblings

slide-29
SLIDE 29

Joining a Table with Itself

Two tables may share a column name; dot expressions and aliases disambiguate column values

7

E F A D G B C H

select [columns] from [table] where [condition] order by [order]; [table] is a comma-separated list of table names with optional aliases Select all pairs of siblings select a.child as first, b.child as second from parents as a, parents as b where a.parent = b.parent and a.child < b.child;

slide-30
SLIDE 30

Joining a Table with Itself

Two tables may share a column name; dot expressions and aliases disambiguate column values

7

E F A D G B C H

select [columns] from [table] where [condition] order by [order]; [table] is a comma-separated list of table names with optional aliases Select all pairs of siblings select a.child as first, b.child as second from parents as a, parents as b where a.parent = b.parent and a.child < b.child;

slide-31
SLIDE 31

Joining a Table with Itself

Two tables may share a column name; dot expressions and aliases disambiguate column values

7

E F A D G B C H

select [columns] from [table] where [condition] order by [order]; [table] is a comma-separated list of table names with optional aliases Select all pairs of siblings select a.child as first, b.child as second from parents as a, parents as b where a.parent = b.parent and a.child < b.child; First Second barack clinton abraham delano abraham grover delano grover

slide-32
SLIDE 32

Example: Grandparents

Which select statement evaluates to all grandparent, grandchild pairs?

8

E F A D G B C H

select a.grandparent, b.child from parents as a, parents as b where b.parent = a.child; select a.grandparent, b.child from parents as a, parents as b where a.parent = b.child; select a.parent, b.child from parents as a, parents as b where b.parent = a.child; select a.parent, b.child from parents as a, parents as b where a.parent = b.child; None of the above 1 2 3 4 5

slide-33
SLIDE 33

Joining Multiple Tables

9

E F A D G B C H

slide-34
SLIDE 34

Joining Multiple Tables

Multiple tables can be joined to yield all combinations of rows from each

9

E F A D G B C H

slide-35
SLIDE 35

Joining Multiple Tables

Multiple tables can be joined to yield all combinations of rows from each

9

E F A D G B C H

create table grandparents as select a.parent as grandog, b.child as granpup from parents as a, parents as b where b.parent = a.child;

slide-36
SLIDE 36

Joining Multiple Tables

Multiple tables can be joined to yield all combinations of rows from each

9

E F A D G B C H

Select all grandparents with the same fur as their grandchildren create table grandparents as select a.parent as grandog, b.child as granpup from parents as a, parents as b where b.parent = a.child;

slide-37
SLIDE 37

Joining Multiple Tables

Multiple tables can be joined to yield all combinations of rows from each

9

E F A D G B C H

Select all grandparents with the same fur as their grandchildren create table grandparents as select a.parent as grandog, b.child as granpup from parents as a, parents as b where b.parent = a.child; Which tables need to be joined together?

slide-38
SLIDE 38

Joining Multiple Tables

Multiple tables can be joined to yield all combinations of rows from each

9

E F A D G B C H

Select all grandparents with the same fur as their grandchildren select grandog from grandparents, dogs as c, dogs as d where grandog = c.name and granpup = d.name and c.fur = d.fur; create table grandparents as select a.parent as grandog, b.child as granpup from parents as a, parents as b where b.parent = a.child; Which tables need to be joined together?

slide-39
SLIDE 39

Example: Dog Triples

slide-40
SLIDE 40

Fall 2014 Quiz Question (Slightly Modified)

11

slide-41
SLIDE 41

Fall 2014 Quiz Question (Slightly Modified)

Write a SQL query that selects all possible combinations of three different dogs with the same fur and lists each triple in inverse alphabetical order

11

slide-42
SLIDE 42

Fall 2014 Quiz Question (Slightly Modified)

Write a SQL query that selects all possible combinations of three different dogs with the same fur and lists each triple in inverse alphabetical order

11

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union ...;

slide-43
SLIDE 43

Fall 2014 Quiz Question (Slightly Modified)

Write a SQL query that selects all possible combinations of three different dogs with the same fur and lists each triple in inverse alphabetical order

11

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union ...; create table parents as select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union ...;

slide-44
SLIDE 44

Fall 2014 Quiz Question (Slightly Modified)

Write a SQL query that selects all possible combinations of three different dogs with the same fur and lists each triple in inverse alphabetical order

11

E F A D G B C H

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union ...; create table parents as select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union ...;

slide-45
SLIDE 45

Fall 2014 Quiz Question (Slightly Modified)

Write a SQL query that selects all possible combinations of three different dogs with the same fur and lists each triple in inverse alphabetical order

11

Expected output: delano|clinton|abraham
 grover|eisenhower|barack

E F A D G B C H

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union ...; create table parents as select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union ...;

slide-46
SLIDE 46

Fall 2014 Quiz Question (Slightly Modified)

Write a SQL query that selects all possible combinations of three different dogs with the same fur and lists each triple in inverse alphabetical order

11

Expected output: delano|clinton|abraham
 grover|eisenhower|barack

E F A D G B C H

create table dogs as select "abraham" as name, "long" as fur union select "barack" , "short" union ...; create table parents as select "abraham" as parent, "barack" as child union select "abraham" , "clinton" union ...; (Demo)

slide-47
SLIDE 47

Numerical Expressions

slide-48
SLIDE 48

Numerical Expressions

Expressions can contain function calls and arithmetic operators

13

slide-49
SLIDE 49

Numerical Expressions

Expressions can contain function calls and arithmetic operators

13

select [columns] from [table] where [expression] order by [expression];

slide-50
SLIDE 50

Numerical Expressions

Expressions can contain function calls and arithmetic operators

13

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ...

slide-51
SLIDE 51

Numerical Expressions

Expressions can contain function calls and arithmetic operators

13

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... Combine values: +, -, *, /, %, and, or

slide-52
SLIDE 52

Numerical Expressions

Expressions can contain function calls and arithmetic operators

13

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... Combine values: +, -, *, /, %, and, or Transform values: abs, round, not, -

slide-53
SLIDE 53

Numerical Expressions

Expressions can contain function calls and arithmetic operators

13

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... Combine values: +, -, *, /, %, and, or Transform values: abs, round, not, - Compare values: <, <=, >, >=, <>, !=, =

slide-54
SLIDE 54

Numerical Expressions

Expressions can contain function calls and arithmetic operators

13

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... Combine values: +, -, *, /, %, and, or Transform values: abs, round, not, - Compare values: <, <=, >, >=, <>, !=, = (Demo)

slide-55
SLIDE 55

String Expressions

slide-56
SLIDE 56

String Expressions

String values can be combined to form longer strings

15

slide-57
SLIDE 57

String Expressions

String values can be combined to form longer strings

15

sqlite> select "hello," || " world";
 hello, world

slide-58
SLIDE 58

String Expressions

String values can be combined to form longer strings

15

Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world

slide-59
SLIDE 59

String Expressions

String values can be combined to form longer strings

15

sqlite> create table phrase as select "hello, world" as s; Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world

slide-60
SLIDE 60

String Expressions

String values can be combined to form longer strings

15

sqlite> create table phrase as select "hello, world" as s; sqlite> select substr(s, 4, 2) || substr(s, instr(s, " ")+1, 1) from phrase; Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world

slide-61
SLIDE 61

String Expressions

String values can be combined to form longer strings

15

sqlite> create table phrase as select "hello, world" as s; sqlite> select substr(s, 4, 2) || substr(s, instr(s, " ")+1, 1) from phrase; low Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world

slide-62
SLIDE 62

String Expressions

String values can be combined to form longer strings

15

sqlite> create table phrase as select "hello, world" as s; sqlite> select substr(s, 4, 2) || substr(s, instr(s, " ")+1, 1) from phrase; low Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world

slide-63
SLIDE 63

String Expressions

String values can be combined to form longer strings

15

sqlite> create table phrase as select "hello, world" as s; sqlite> select substr(s, 4, 2) || substr(s, instr(s, " ")+1, 1) from phrase; low Strings can be used to represent structured values, but doing so is rarely a good idea Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world

slide-64
SLIDE 64

String Expressions

String values can be combined to form longer strings

15

sqlite> create table phrase as select "hello, world" as s; sqlite> select substr(s, 4, 2) || substr(s, instr(s, " ")+1, 1) from phrase; low Strings can be used to represent structured values, but doing so is rarely a good idea Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world sqlite> create table lists as select "one" as car, "two,three,four" as cdr;

slide-65
SLIDE 65

String Expressions

String values can be combined to form longer strings

15

sqlite> create table phrase as select "hello, world" as s; sqlite> select substr(s, 4, 2) || substr(s, instr(s, " ")+1, 1) from phrase; low Strings can be used to represent structured values, but doing so is rarely a good idea Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world sqlite> create table lists as select "one" as car, "two,three,four" as cdr; sqlite> select substr(cdr, 1, instr(cdr, ",")-1) as cadr from lists;

slide-66
SLIDE 66

String Expressions

String values can be combined to form longer strings

15

sqlite> create table phrase as select "hello, world" as s; sqlite> select substr(s, 4, 2) || substr(s, instr(s, " ")+1, 1) from phrase; low Strings can be used to represent structured values, but doing so is rarely a good idea Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world sqlite> create table lists as select "one" as car, "two,three,four" as cdr; sqlite> select substr(cdr, 1, instr(cdr, ",")-1) as cadr from lists; two

slide-67
SLIDE 67

String Expressions

String values can be combined to form longer strings

15

sqlite> create table phrase as select "hello, world" as s; sqlite> select substr(s, 4, 2) || substr(s, instr(s, " ")+1, 1) from phrase; low Strings can be used to represent structured values, but doing so is rarely a good idea Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world sqlite> create table lists as select "one" as car, "two,three,four" as cdr; sqlite> select substr(cdr, 1, instr(cdr, ",")-1) as cadr from lists; two

slide-68
SLIDE 68

String Expressions

String values can be combined to form longer strings

15

sqlite> create table phrase as select "hello, world" as s; sqlite> select substr(s, 4, 2) || substr(s, instr(s, " ")+1, 1) from phrase; low Strings can be used to represent structured values, but doing so is rarely a good idea Basic string manipulation is built into SQL, but differs from Python sqlite> select "hello," || " world";
 hello, world sqlite> create table lists as select "one" as car, "two,three,four" as cdr; sqlite> select substr(cdr, 1, instr(cdr, ",")-1) as cadr from lists; two (Demo)

slide-69
SLIDE 69

Database Management Systems

slide-70
SLIDE 70

Database Management System Architecture

17

Architecture of a Database System by Hellerstein, Stonebreaker, and Hamilton

slide-71
SLIDE 71

Query Planning

The manner in which tables are filtered, sorted, and joined affects execution time

18

slide-72
SLIDE 72

Query Planning

The manner in which tables are filtered, sorted, and joined affects execution time

18

Select the parents of curly-furred dogs: select parent from parents, dogs where child = name and fur = "curly";

slide-73
SLIDE 73

Query Planning

The manner in which tables are filtered, sorted, and joined affects execution time

18

Select the parents of curly-furred dogs: select parent from parents, dogs where child = name and fur = "curly";

slide-74
SLIDE 74

Query Planning

The manner in which tables are filtered, sorted, and joined affects execution time

18

Select the parents of curly-furred dogs: select parent from parents, dogs where child = name and fur = "curly";

slide-75
SLIDE 75

Query Planning

The manner in which tables are filtered, sorted, and joined affects execution time

18

Select the parents of curly-furred dogs: select parent from parents, dogs where child = name and fur = "curly";

slide-76
SLIDE 76

Query Planning

The manner in which tables are filtered, sorted, and joined affects execution time

18

Select the parents of curly-furred dogs: select parent from parents, dogs where child = name and fur = "curly"; Join all rows of parents to all rows of dogs, filter by child = name and fur = "curly"

slide-77
SLIDE 77

Query Planning

The manner in which tables are filtered, sorted, and joined affects execution time

18

Select the parents of curly-furred dogs: select parent from parents, dogs where child = name and fur = "curly"; Join all rows of parents to all rows of dogs, filter by child = name and fur = "curly" Join only rows of parents and dogs where child = name, filter by fur = "curly"

slide-78
SLIDE 78

Query Planning

The manner in which tables are filtered, sorted, and joined affects execution time

18

Select the parents of curly-furred dogs: select parent from parents, dogs where child = name and fur = "curly"; Join all rows of parents to all rows of dogs, filter by child = name and fur = "curly" Join only rows of parents and dogs where child = name, filter by fur = "curly" Filter dogs by fur = "curly", join result with all rows of parents, filter by child = name

slide-79
SLIDE 79

Query Planning

The manner in which tables are filtered, sorted, and joined affects execution time

18

Select the parents of curly-furred dogs: select parent from parents, dogs where child = name and fur = "curly"; Join all rows of parents to all rows of dogs, filter by child = name and fur = "curly" Join only rows of parents and dogs where child = name, filter by fur = "curly" Filter dogs by fur = "curly", join result with all rows of parents, filter by child = name Filter dogs by fur = "curly", join only rows of result and parents where child = name