all data is stored as tables in DBs
at the heart of relational databases is how different pieces of data relate to each other
data types and meaning consistency are very relevant in DBs
Georgia
’ has string datatype, but can mean:
anytime a single value is computed from a set of values
aggregation usually answers some question about a DB summary
common SQL aggregation functions:
count # how many rows?
avg # average of numbers?
max # largest of numbers?
min # smallest of numbers?
sum # summation of numbers?
the code must connect to the DB first before sending out queries
str
typeint
but different limitsYYYY-MM-DD
always put ‘single quotes’ around ‘text’ strings and ‘data/time’ values
str
likeint
likefloat
likea query always returns a table per the parameters of the query
select ... from ... where ...
query:
select name, birthdate from animals where species = 'gorilla';
select
select
columns: ‘name, birthdate
’from
animals
where
select
only rows where
column species
reads gorilla
and
, or
and not
and
example:
select name, birthdate from animals where species = 'gorilla' and name = 'Max';
not
example:
select name from animals where (not species = 'gorilla') and (not name = 'Max');
select name from animals where species != 'gorilla' and name != Max;
or
example:
select name from animals where not (species = 'gorilla' or name = 'Max');
=
,<
,>
,<=
, >=
, !=
select name from animals where birthdate > '1995-01-01' and birthdate < '1998-12-31' and species == 'llama'
fig: name of the llama returned
LIMIT count OFFSET skip
OFFSET
is optionalcount
and skip
are whole numberslimit 10 offset 150
ORDER BY columns DESC
ORDER BY
: sort byDESC
: optional
order by species, name
GROUP BY columns
select species, min(birthdate) from animals group by species;
AS num
num
select name, count(*) as num from animals group by name;
count(*)
: count all the rows..as num
: and call the count column num
group by name
WHERE
:
count(*)
, AS num
HAVING
:
python
synonyms of some sql queries:
count(*)
:len(rsults)
limit 100 offset 10
: results[10:100]
order by column
: sorted(results, key = lambda x:x[column])
\dt
and \d tablename
show tables
and describe tablename
.tables
and .schema tablename
QUERY = "select max(name) from animals;"
QUERY = "select * from animals limit 10;"
QUERY = "select * from animals where species = 'orangutan' order by birthdate;"
order by
) birthdate
QUERY = "select name from animals where species = 'orangutan' order by birthdate desc;"
order by
(sort) birthdate descendingQUERY = "select name, birthdate from animals order by name limit 10 offset 20;"
order by
(sort) name,QUERY = "select name, count(*) as num from animals group by name order by num desc limit 5;"
num
in returned data
- *
: selects all
num
in descending orderQUERY = "select species, min(birthdate) from animals group by species;"
QUERY = "select species, count(species) from animals group by species order by count(species) desc"
QUERY = "select food, count(animals.name) as num from diet join animals on diet.species = animals.species group by food having num = 1"
select food, count(animals.name) as num
: food and corresponding animalsfrom animals join diet
: from joined table of animals and diet
diet.species = animals.species
: table joining conditiongroup by food
: aggregate selection food, count(animals.name)
by food
COUNT
:
QUERY = "select count(*) from animals;"
QUERY = "select count(*) from animals where species = 'gorilla';"
QUERY = "select species, count(*) from animals group by species;"
INSERT into table values (42,'stuff');
INSERT into table (col2, col1) values ('stuff',42);
INSERT_QUERY = '''
insert into animals values ('Ribid','opossum','2019-12-12');
'''
select T.thing, S.stuff from T join S on T.target = S.match
T.thing, S.stuff
: rowsT join S
: joined tablesT.target = S.match
: join conditionsselect T.thing, S.stuff from T,S where T.target = S.match
T.thing, S.stuff
: rowsT,S
: tablesT.target = S.match
: restriction
QUERY = "select animals.name from animals join diet on animals.species = diet.species where food = 'fish'"
animals.name
columnanimals.species
and diet.species
food = 'fish'
select name, weight
from players,
(select avg(weight) as av
from players) as subqures
where weight < av;
create view topFive as
select species, count(*) as num
from animals
group by species
order by num desc
limit 5;
PRIMARY
keyword in an SQL tableREFERENCES
keyword in an SQL table pointing to a related table or column