This Notebook focuses on SQL, while using the Python-based IPython kernel. For this, we take advantage of the extensible IPython magic command system, as described below.
Our first step is to load the ipython-sql extension, which exposes the %%sql
cell magic.
Note: I accidentally missed updating the hub to include the SQL IPython support. We've requested a fix on the hub, but in the meantime, the cell below will do an install if necessary.
Please use similar code in your other uses of SQL on the hub til we get this fixed (should happen soon). Once this is fixed, the cell below can be replaced with simply
%load_ext sql
try:
%load_ext sql
except ModuleNotFoundError:
%pip install ipython-sql
import sys
print('#'*80 +
'\n\nPlease restart your kernel before continuing.\n\n' +
'#'*80 ,
file=sys.stderr)
# %load_ext sql
Then we will connect to the database. In this lecture example, the database is stored in a single file on our own computer called lec18_basic_examples.db.
Note that starting a cell with %%sql
tells Jupyter that you are running SQL code, not Python code.
In actual practice, you'd usually connect to some database server via a network connection, e.g. hosted on some computer on the internet.
%%sql
sqlite:///data/lec18_basic_examples.db
Now that we're connected, we can, for example, display the contents of the Dragon table.
%%sql
SELECT * FROM Dragon;
* sqlite:///data/lec18_basic_examples.db Done.
name | year | cute |
---|---|---|
hiccup | 2010 | 10 |
drogon | 2011 | -100 |
dragon 2 | 2019 | 0 |
%%sql
SELECT * FROM sqlite_master WHERE type='table'
* sqlite:///data/lec18_basic_examples.db Done.
type | name | tbl_name | rootpage | sql |
---|---|---|---|---|
table | sqlite_sequence | sqlite_sequence | 7 | CREATE TABLE sqlite_sequence(name,seq) |
table | Dragon | Dragon | 2 | CREATE TABLE Dragon ( name TEXT PRIMARY KEY, year INTEGER CHECK (year >= 2000), cute INTEGER ) |
table | Dish | Dish | 4 | CREATE TABLE Dish ( name TEXT PRIMARY KEY, type TEXT, cost INTEGER CHECK (cost >= 0) ) |
table | Scene | Scene | 6 | CREATE TABLE Scene ( id INTEGER PRIMARY KEY AUTOINCREMENT, biome TEXT NOT NULL, city TEXT NOT NULL, visitors INTEGER CHECK (visitors >= 0), created_at DATETIME DEFAULT (DATETIME('now')) ) |
%%sql
SELECT * FROM Dragon;
* sqlite:///data/lec18_basic_examples.db Done.
name | year | cute |
---|---|---|
hiccup | 2010 | 10 |
drogon | 2011 | -100 |
dragon 2 | 2019 | 0 |
%%sql
SELECT cute, year FROM Dragon;
* sqlite:///data/lec18_basic_examples.db Done.
cute | year |
---|---|
10 | 2010 |
-100 | 2011 |
0 | 2019 |
%%sql
SELECT cute AS cuteness, year AS birth FROM Dragon;
* sqlite:///data/lec18_basic_examples.db Done.
cuteness | birth |
---|---|
10 | 2010 |
-100 | 2011 |
0 | 2019 |
%%sql
SELECT cute AS cuteness,
year AS birth
FROM Dragon;
* sqlite:///data/lec18_basic_examples.db Done.
cuteness | birth |
---|---|
10 | 2010 |
-100 | 2011 |
0 | 2019 |
%%sql
SELECT name, year
FROM Dragon
WHERE cute > 0;
* sqlite:///data/lec18_basic_examples.db Done.
name | year |
---|---|
hiccup | 2010 |
%%sql
SELECT name, cute, year
FROM Dragon
WHERE cute > 0 OR year > 2015;
* sqlite:///data/lec18_basic_examples.db Done.
name | cute | year |
---|---|---|
hiccup | 10 | 2010 |
dragon 2 | 0 | 2019 |
%%sql
SELECT *
FROM Dragon
ORDER BY cute DESC;
* sqlite:///data/lec18_basic_examples.db Done.
name | year | cute |
---|---|---|
hiccup | 2010 | 10 |
dragon 2 | 2019 | 0 |
drogon | 2011 | -100 |
%%sql
SELECT *
FROM Dragon
LIMIT 2;
* sqlite:///data/lec18_basic_examples.db Done.
name | year | cute |
---|---|---|
hiccup | 2010 | 10 |
drogon | 2011 | -100 |
%%sql
SELECT *
FROM Dragon
LIMIT 2
OFFSET 1;
* sqlite:///data/lec18_basic_examples.db Done.
name | year | cute |
---|---|---|
drogon | 2011 | -100 |
dragon 2 | 2019 | 0 |
%%sql
SELECT *
FROM Dish;
* sqlite:///data/lec18_basic_examples.db Done.
name | type | cost |
---|---|---|
ravioli | entree | 10 |
pork bun | entree | 7 |
taco | entree | 7 |
edamame | appetizer | 4 |
fries | appetizer | 4 |
potsticker | appetizer | 4 |
ice cream | dessert | 5 |
%%sql
SELECT type
FROM Dish;
* sqlite:///data/lec18_basic_examples.db Done.
type |
---|
entree |
entree |
entree |
appetizer |
appetizer |
appetizer |
dessert |
%%sql
SELECT type
FROM Dish
GROUP BY type;
* sqlite:///data/lec18_basic_examples.db Done.
type |
---|
appetizer |
dessert |
entree |
%%sql
SELECT type, MAX(cost)
FROM Dish
GROUP BY type;
* sqlite:///data/lec18_basic_examples.db Done.
type | MAX(cost) |
---|---|
appetizer | 4 |
dessert | 5 |
entree | 10 |
%%sql
SELECT type,
SUM(cost),
MIN(cost),
MAX(name)
FROM Dish
GROUP BY type;
* sqlite:///data/lec18_basic_examples.db Done.
type | SUM(cost) | MIN(cost) | MAX(name) |
---|---|---|---|
appetizer | 12 | 4 | potsticker |
dessert | 5 | 5 | ice cream |
entree | 24 | 7 | taco |
%%sql
SELECT type, COUNT(cost)
FROM Dish
GROUP BY type;
* sqlite:///data/lec18_basic_examples.db Done.
type | COUNT(cost) |
---|---|
appetizer | 3 |
dessert | 1 |
entree | 3 |
%%sql
SELECT type, COUNT(cost)
FROM Dish
GROUP BY type;
* sqlite:///data/lec18_basic_examples.db Done.
type | COUNT(cost) |
---|---|
appetizer | 3 |
dessert | 1 |
entree | 3 |
%%sql
SELECT type, COUNT(*)
FROM Dish
GROUP BY type;
* sqlite:///data/lec18_basic_examples.db Done.
type | COUNT(*) |
---|---|
appetizer | 3 |
dessert | 1 |
entree | 3 |
%%sql
SELECT type, cost
FROM Dish
GROUP BY type, cost;
* sqlite:///data/lec18_basic_examples.db Done.
type | cost |
---|---|
appetizer | 4 |
dessert | 5 |
entree | 7 |
entree | 10 |
%%sql
SELECT type, cost, COUNT(*)
FROM Dish
GROUP BY type, cost;
* sqlite:///data/lec18_basic_examples.db Done.
type | cost | COUNT(*) |
---|---|---|
appetizer | 4 | 3 |
dessert | 5 | 1 |
entree | 7 | 2 |
entree | 10 | 1 |
%%sql
SELECT type, COUNT(*)
FROM Dish
GROUP BY type;
* sqlite:///data/lec18_basic_examples.db Done.
type | COUNT(*) |
---|---|
appetizer | 3 |
dessert | 1 |
entree | 3 |
%%sql
SELECT type, COUNT(*)
FROM Dish
GROUP BY type
HAVING MAX(cost) < 8
* sqlite:///data/lec18_basic_examples.db Done.
type | COUNT(*) |
---|---|
appetizer | 3 |
dessert | 1 |
%%sql
SELECT type, COUNT(*)
FROM Dish
WHERE cost < 8
GROUP BY type;
* sqlite:///data/lec18_basic_examples.db Done.
type | COUNT(*) |
---|---|
appetizer | 3 |
dessert | 1 |
entree | 2 |
%%sql
SELECT DISTINCT cost
FROM Dish;
* sqlite:///data/lec18_basic_examples.db Done.
cost |
---|
10 |
7 |
4 |
5 |
%%sql
SELECT DISTINCT type
FROM Dish
WHERE cost < 9;
* sqlite:///data/lec18_basic_examples.db Done.
type |
---|
entree |
appetizer |
dessert |
%%sql
SELECT DISTINCT type, cost
FROM Dish
WHERE cost < 9;
* sqlite:///data/lec18_basic_examples.db Done.
type | cost |
---|---|
entree | 7 |
appetizer | 4 |
dessert | 5 |
%%sql
SELECT DISTINCT type, cost
FROM Dish;
* sqlite:///data/lec18_basic_examples.db Done.
type | cost |
---|---|
entree | 10 |
entree | 7 |
appetizer | 4 |
dessert | 5 |
%%sql
SELECT type, AVG(cost)
FROM Dish
GROUP BY type;
* sqlite:///data/lec18_basic_examples.db Done.
type | AVG(cost) |
---|---|
appetizer | 4.0 |
dessert | 5.0 |
entree | 8.0 |
%%sql
SELECT type, AVG(DISTINCT cost)
FROM Dish
GROUP BY type;
* sqlite:///data/lec18_basic_examples.db Done.
type | AVG(DISTINCT cost) |
---|---|
appetizer | 4.0 |
dessert | 5.0 |
entree | 8.5 |