0% found this document useful (0 votes)
7 views

36 Aggregation 4pp

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

36 Aggregation 4pp

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Aggregation Announcements

Aggregate Functions

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

[expression] as [name], [expression] as [name], ...

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

Aggregation An aggregate function in the [columns] clause computes a value from a group of rows

create table animals as animals:


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

4
Mixing Aggregate Functions and Single Values

An aggregate function also selects some row in the table to supply the values of columns
that are not aggregated. In the case of max or min, this row is that of the max or min
value. Otherwise, it is arbitrary.

select max(weight), kind from animals; select max(legs), kind from animals;

select min(kind), kind from animals; select avg(weight), kind from animals;

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

Grouping Rows Selecting Groups

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

[expression] as [name], [expression] as [name], ... [expression] as [name], [expression] as [name], ...

select [columns] from [table] group by [expression] having [expression]; select [columns] from [table] group by [expression] having [expression];

The number of groups is the number of unique values of an expression A having clause filters the set of groups that are aggregated

select legs, max(weight) from animals group by legs; select weight/legs, count(*) from animals group by weight/legs having count(*)>1;
animals: animals:
kind legs weight kind legs weight
dog 4 20 weight/legs=5 dog 4 20
legs max(weight) weight/legs count(*)
legs=4 cat 4 10 weight/legs=2 cat 4 10
4 20 5 2
ferret 4 10 weight/legs=2 ferret 4 10
2 12000 2 2
parrot 2 6 weight/legs=3 parrot 2 6
legs=2 penguin 2 10 weight/legs=5 penguin 2 10
(Demo) t-rex 2 12000 weight/legs=6000 t-rex 2 12000
8

7
Discussion Question

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

Optional (but fun) content from here onward

Modifying a Database

Add a row to the end of an existing table:

INSERT INTO [table] VALUES ([column_0_value], [column_1_value], ...);

Change the values in some rows of an existing table:

Python and SQL


How each row is changed Which rows get updated

UPDATE [table] SET [column_label]=[value] WHERE ...;

Delete a table if it exists (typically used to rebuild a table from scratch):


(Demo)
DROP TABLE IF EXISTS [table];

11
Casino Blackjack

Player:

Database Connections
(Demo)

Dealer:

14

You might also like