python sql

QUERY = '''
select name, birthdate from animals where species = 'gorilla';
'''

+———+————+
| name | birthdate |
+=========+============+
| Max | 2001-04-23 |
| Dave | 1988-09-29 |
| Becky | 1979-07-04 |
| Liz | 1998-06-12 |
| George | 2011-01-09 |
| George | 1998-05-18 |
| Wendell | 1982-09-24 |
| Bjorn | 2000-03-07 |
| Kristen | 1990-04-25 |
+———+————+

select name, birthdate from animals where species = ‘gorilla’ and name = ‘Max’

否定

QUERY = '''
select name, birthdate from animals where species != 'gorilla' and name != 'Max';
'''

between文

QUERY = '''
select name from animals where species = 'llama' AND birthdate between '1995-01-01' AND '1998-12-31';
'''

query

QUERY = "select max(name) from animals;"

QUERY = "select * from animals limit 10;"

QUERY = "select * from animals where species = 'orangutan' order by birthdate;"

QUERY = "select name from animals where species = 'orangutan' order by birthdate desc;"

QUERY = "select name, birthdate from animals order by name limit 10 offset 20;"

QUERY = "select species, min(birthdate) from animals group by species;"

QUERY = '''
 select name, count(*) as num from animals
 group by name
 order by num desc
 limit 5;
 '''

Limit count Offset skip
limit is how many rows to return
offset is how far into the results to start

Order by columns DESC
columns is which columns to sort by, separated with commas
DESC is sort in reverse order(descending)

Group by columns
columns is which columns to use as groupings when aggregating

QUERY = "select count(*) as num, species from animals group by species order by num desc;"

insert

INSERT_QUERY = '''
insert into animals (name, species, birthdate) values ('yoshi', 'opposum', '2016-12-13');
'''