SQL/Python and SQL, Sqlitetutorial for many, sql for the weary Flashcards
many from dataquest?
basic query using sqlite3, also sorting in reverse alphabetical order
conn = sqlite3.connect(“jobs2.db”)
cursor = conn.cursor()
query = “select Major from recent_grads ORDER BY Major DESC;”
cursor.execute(query)
reverse_alphabetical = cursor.fetchall()
Sort
SELECT
select_list
FROM
table
ORDER BY
column_1 ASC,
column_2 DESC;
NA
NULL , use IS NULL
Select and NULL
SQLite considers NULL values as duplicates.
select species, sex, island
from penguins
where sex is null;
https://gvwilson.github.io/sql-tutorial/
Third smallest
SELECT
trackid,
name,
bytes
FROM
tracks
ORDER BY
bytes
LIMIT 1 OFFSET 2;
Combine where and in with list generated by where
SELECT
TrackId,
Name,
AlbumId
FROM
Tracks
WHERE
AlbumId IN (
SELECT
AlbumId
FROM
Albums
WHERE
ArtistId = 12
);
LIKE, with % and _
The percent sign % wildcard examples
The s% pattern that uses the percent sign wildcard ( %) matches any string that starts with s e.g.,son and so.
The %er pattern matches any string that ends with er like peter, clever, etc.
And the %per% pattern matches any string that contains per such as percent and peeper.
The underscore _ wildcard examples
The h_nt pattern matches hunt, hint, etc. The __pple pattern matches topple, supple, tipple, etc.
Note that SQLite LIKE operator is case-insensitive. It means “A” LIKE “a” is true.
However, for Unicode characters that are not in the ASCII ranges, the LIKE operator is case sensitive e.g., “Ä” LIKE “ä” is false.
In case you want to make LIKE operator works case-sensitively, you need to use the following PRAGMA:
PRAGMA case_sensitive_like = true
Unix like matching
Glob
Change names
select
flipper_length_mm / 10.0 as flipper_cm,
body_mass_g / 1000.0 as weight_kg,
island as where_found
from penguins
limit 3;
Some aggregation functions
Note that it ignores NULL values
Select
max(bill_length_mm) as longest_bill,
min(flipper_length_mm) as shortest_flipper,
avg(bill_length_mm) / avg(bill_depth_mm) as weird_ratio
from penguins;
out/common_aggregations.out
This actually shouldn’t work: can’t calculate maximum or average if any values are null SQL does the useful thing instead of the right one
longest_bill | shortest_flipper | weird_ratio |
|————–|——————|——————|
| 59.6 | 172 | 2.56087082530644 |
Count
count(*) counts rows
count(column) counts non-null entries in column
count(distinct column) counts distinct non-null entries
Averages over a group
select
sex,
avg(body_mass_g) as average_mass_g
from penguins
group by sex;