SQL/Python and SQL, Sqlitetutorial for many, sql for the weary Flashcards

many from dataquest?

1
Q

basic query using sqlite3, also sorting in reverse alphabetical order

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Sort

A

SELECT
select_list
FROM
table
ORDER BY
column_1 ASC,
column_2 DESC;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

NA

A

NULL , use IS NULL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Select and NULL

A

SQLite considers NULL values as duplicates.

select species, sex, island
from penguins
where sex is null;

https://gvwilson.github.io/sql-tutorial/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Third smallest

A

SELECT
trackid,
name,
bytes
FROM
tracks
ORDER BY
bytes
LIMIT 1 OFFSET 2;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Combine where and in with list generated by where

A

SELECT
TrackId,
Name,
AlbumId
FROM
Tracks
WHERE
AlbumId IN (
SELECT
AlbumId
FROM
Albums
WHERE
ArtistId = 12
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

LIKE, with % and _

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Unix like matching

A

Glob

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Change names

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Some aggregation functions

A

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 |

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Count

A

count(*) counts rows
count(column) counts non-null entries in column
count(distinct column) counts distinct non-null entries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Averages over a group

A

select
sex,
avg(body_mass_g) as average_mass_g
from penguins
group by sex;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly