61A Lecture 34 Announcements Integer Examples (continued) A Very - - PowerPoint PPT Presentation

61a lecture 34 announcements integer examples continued a
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 34 Announcements Integer Examples (continued) A Very - - PowerPoint PPT Presentation

61A Lecture 34 Announcements Integer Examples (continued) A Very Interesting Number The mathematician G. H. Hardy once remarked to the mathematician Srinivasa Ramanujan... 4 A Very Interesting Number The mathematician G. H. Hardy once


slide-1
SLIDE 1

61A Lecture 34

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Integer Examples (continued)

slide-4
SLIDE 4

A Very Interesting Number

The mathematician G. H. Hardy once remarked to the mathematician Srinivasa Ramanujan...

4

slide-5
SLIDE 5

A Very Interesting Number

The mathematician G. H. Hardy once remarked to the mathematician Srinivasa Ramanujan...

4

(Demo)

slide-6
SLIDE 6

Aggregation

slide-7
SLIDE 7

Aggregate Functions

6

slide-8
SLIDE 8

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

slide-9
SLIDE 9

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

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

slide-10
SLIDE 10

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows

slide-11
SLIDE 11

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as

slide-12
SLIDE 12

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as select "dog" as kind, 4 as legs, 20 as weight union

slide-13
SLIDE 13

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union

slide-14
SLIDE 14

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union

slide-15
SLIDE 15

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union

slide-16
SLIDE 16

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union

slide-17
SLIDE 17

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000;

slide-18
SLIDE 18

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals:

slide-19
SLIDE 19

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; select max(legs) from animals; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals:

slide-20
SLIDE 20

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; select max(legs) from animals; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: max(legs) 4

slide-21
SLIDE 21

Aggregate Functions

So far, all SQL expressions have referred to the values in a single row at a time

6

select [columns] from [table] where [expression] order by [expression]; [expression] as [name], [expression] as [name], ... An aggregate function in the [columns] clause computes a value from a group of rows create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; select max(legs) from animals; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: max(legs) 4 (Demo)

slide-22
SLIDE 22

Mixing Aggregate Functions and Single Values

7

create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals:

slide-23
SLIDE 23

Mixing Aggregate Functions and Single Values

An aggregate function also selects a row in the table, which may be meaningful

7

create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals:

slide-24
SLIDE 24

Mixing Aggregate Functions and Single Values

An aggregate function also selects a row in the table, which may be meaningful

7

select max(weight), kind from animals; create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals:

slide-25
SLIDE 25

Mixing Aggregate Functions and Single Values

An aggregate function also selects a row in the table, which may be meaningful

7

select max(weight), kind from animals; select min(kind), kind from animals; create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals:

slide-26
SLIDE 26

Mixing Aggregate Functions and Single Values

An aggregate function also selects a row in the table, which may be meaningful

7

select max(weight), kind from animals; select min(kind), kind from animals; select max(legs), kind from animals; create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals:

slide-27
SLIDE 27

Mixing Aggregate Functions and Single Values

An aggregate function also selects a row in the table, which may be meaningful

7

select max(weight), kind from animals; select min(kind), kind from animals; select max(legs), kind from animals; select avg(weight), kind from animals; create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals:

slide-28
SLIDE 28

Mixing Aggregate Functions and Single Values

An aggregate function also selects a row in the table, which may be meaningful

7

select max(weight), kind from animals; select min(kind), kind from animals; select max(legs), kind from animals; select avg(weight), kind from animals; create table animals as select "dog" as kind, 4 as legs, 20 as weight union select "cat" , 4 , 10 union select "ferret" , 4 , 10 union select "parrot" , 2 , 6 union select "penguin" , 2 , 10 union select "t-rex" , 2 , 12000; kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: (Demo)

slide-29
SLIDE 29

Groups

slide-30
SLIDE 30

Grouping Rows

9

slide-31
SLIDE 31

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

slide-32
SLIDE 32

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ...

slide-33
SLIDE 33

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... The number of groups is the number of unique values of an expression

slide-34
SLIDE 34

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: The number of groups is the number of unique values of an expression

slide-35
SLIDE 35

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: The number of groups is the number of unique values of an expression select legs, max(weight) from animals group by legs;

slide-36
SLIDE 36

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: The number of groups is the number of unique values of an expression select legs, max(weight) from animals group by legs;

slide-37
SLIDE 37

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: The number of groups is the number of unique values of an expression select legs, max(weight) from animals group by legs; legs=4

slide-38
SLIDE 38

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: The number of groups is the number of unique values of an expression select legs, max(weight) from animals group by legs; legs=4

slide-39
SLIDE 39

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: The number of groups is the number of unique values of an expression select legs, max(weight) from animals group by legs; legs=4 legs=2

slide-40
SLIDE 40

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: The number of groups is the number of unique values of an expression select legs, max(weight) from animals group by legs; legs=4 legs=2 legs max(weight) 4 20 2 12000

slide-41
SLIDE 41

Grouping Rows

Rows in a table can be grouped, and aggregation is performed on each group

9

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: The number of groups is the number of unique values of an expression select legs, max(weight) from animals group by legs; legs=4 legs=2 legs max(weight) 4 20 2 12000 (Demo)

slide-42
SLIDE 42

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals:

slide-43
SLIDE 43

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated

slide-44
SLIDE 44

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated select weight/legs, count(*) from animals group by weight/legs having count(*)>1;

slide-45
SLIDE 45

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated select weight/legs, count(*) from animals group by weight/legs having count(*)>1; weight/legs=5

slide-46
SLIDE 46

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated select weight/legs, count(*) from animals group by weight/legs having count(*)>1; weight/legs=5 weight/legs=2

slide-47
SLIDE 47

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated select weight/legs, count(*) from animals group by weight/legs having count(*)>1; weight/legs=5 weight/legs=2 weight/legs=2

slide-48
SLIDE 48

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated select weight/legs, count(*) from animals group by weight/legs having count(*)>1; weight/legs=5 weight/legs=2 weight/legs=2 weight/legs=3

slide-49
SLIDE 49

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated select weight/legs, count(*) from animals group by weight/legs having count(*)>1; weight/legs=5 weight/legs=2 weight/legs=2 weight/legs=3 weight/legs=5

slide-50
SLIDE 50

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated select weight/legs, count(*) from animals group by weight/legs having count(*)>1; weight/legs=5 weight/legs=2 weight/legs=2 weight/legs=3 weight/legs=5 weight/legs=6000

slide-51
SLIDE 51

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated select weight/legs, count(*) from animals group by weight/legs having count(*)>1; weight/legs count(*) 5 2 2 2 weight/legs=5 weight/legs=2 weight/legs=2 weight/legs=3 weight/legs=5 weight/legs=6000

slide-52
SLIDE 52

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated select weight/legs, count(*) from animals group by weight/legs having count(*)>1; weight/legs count(*) 5 2 2 2 weight/legs=5 weight/legs=2 weight/legs=2 weight/legs=3 weight/legs=5 weight/legs=6000

slide-53
SLIDE 53

Selecting Groups

Rows in a table can be grouped, and aggregation is performed on each group

10

select [columns] from [table] group by [expression] having [expression]; [expression] as [name], [expression] as [name], ... kind legs weight dog 4 20 cat 4 10 ferret 4 10 parrot 2 6 penguin 2 10 t-rex 2 12000 animals: A having clause filters the set of groups that are aggregated select weight/legs, count(*) from animals group by weight/legs having count(*)>1; weight/legs count(*) 5 2 2 2 weight/legs=5 weight/legs=2 weight/legs=2 weight/legs=3 weight/legs=5 weight/legs=6000

slide-54
SLIDE 54

Discussion Question

What's the maximum difference between leg count for two animals with the same weight?

11

slide-55
SLIDE 55

Select Grammar

slide-56
SLIDE 56

SQL Select Statements

13

slide-57
SLIDE 57

SQL Select Statements

13

...

slide-58
SLIDE 58

SQL Select Statements

13

http://www.sqlite.org/lang_select.html ... ...

slide-59
SLIDE 59

SQL Select Statements

13

http://www.sqlite.org/lang_select.html ... ...

slide-60
SLIDE 60

SQL Select Statements

13

http://www.sqlite.org/lang_select.html ... ...

slide-61
SLIDE 61

SQL Select Statements

13

http://www.sqlite.org/lang_select.html ... ...

slide-62
SLIDE 62

SQL Select Statements

13

http://www.sqlite.org/lang_select.html ... ...

slide-63
SLIDE 63

SQL Select Statements

13

http://www.sqlite.org/lang_select.html ... ...

slide-64
SLIDE 64

SQL Select Statements

13

http://www.sqlite.org/lang_select.html ... ...

slide-65
SLIDE 65

SQL Select Statements

13

Union http://www.sqlite.org/lang_select.html ... ...

slide-66
SLIDE 66

SQL Select Statements

13

Union http://www.sqlite.org/lang_select.html ... ...