Skip to content
Shaman spell

SQL

Ranks 1-3

The shaman's Far Sight: stand still, see across an entire database at once. Data lives in tables; SQL is how you ask them questions.

Wears the Classic spell Far Sight.

Coming from Python: Your dict-of-lists intuition becomes rows and columns. The re-orientation is thinking in whole sets at once instead of looping row by row.

Advertisement

Summoning the language

Rank 1

First summoning

SQLite is already on your machine and Python ships a module for it. Create a table, insert rows, and ask for some back. Notice there is no loop: the question IS the work.

CREATE TABLE spells (name TEXT, rank INTEGER);
INSERT INTO spells VALUES ('fireball', 3);
SELECT * FROM spells WHERE rank >= 2;
Rank 2

The temper

The power move is joining tables and aggregating: one question that cross-references two tables and counts as it goes. GROUP BY collapses rows into answers; ORDER BY ranks them.

SELECT c.name, COUNT(s.id) AS castable
FROM characters c
JOIN spells s ON s.owner_id = c.id
GROUP BY c.name
ORDER BY castable DESC;
Rank 3

First real chore

Put real data you care about into SQLite (your expenses, your game collection, a log file you parse with Python) and answer five real questions with queries. Then learn EXPLAIN: it shows the plan, and indexes are how you change it.

EXPLAIN QUERY PLAN
SELECT * FROM spells WHERE name = 'fireball';
CREATE INDEX idx_spells_name ON spells(name); -- then EXPLAIN again
Back to the grimoireThe Programmer's GrimoirePython, the eight ranks, and every second spell.