The SQL from the course, each command with what it does. Copy a card into the query box.
Print a cheat sheet (the whole course or one unit, with a picture of what each command draws)
SELECT * FROM songs;
Example
SELECT * FROM songs;
Shows every column and every row of a table. The star means all columns.
id: select-everything
SELECT title, artist FROM songs;
Example
SELECT title, artist FROM songs;
Shows only the columns you name, in the order you name them, separated by commas.
id: select-some-columns
SELECT * FROM songs WHERE year > 2015;
Example
SELECT title, year FROM songs WHERE year > 2015;
Keeps only the rows where the test is true. Use =, <, >, <=, >= and != to compare.
id: where
SELECT * FROM songs WHERE genre = 'pop' AND year > 2015;
Example
SELECT title FROM songs WHERE genre = 'pop' AND year > 2015;
AND needs both tests to be true. OR needs at least one. Text goes in single quotes.
id: and-or
SELECT * FROM songs WHERE title LIKE '%love%';
Example
SELECT title FROM songs WHERE title LIKE '%love%';
Matches text by pattern. % stands for any run of letters, so %love% finds a title with love anywhere in it.
id: like
SELECT * FROM songs ORDER BY plays DESC;
Example
SELECT title, plays FROM songs ORDER BY plays DESC;
Sorts the rows. ASC goes low to high (the default); DESC goes high to low.
id: order-by
SELECT * FROM songs ORDER BY plays DESC LIMIT 3;
Example
SELECT title, plays FROM songs ORDER BY plays DESC LIMIT 3;
Stops after that many rows. With ORDER BY it gives a top three or bottom three.
id: limit
SELECT COUNT(*) FROM songs;
Example
SELECT COUNT(*) FROM songs WHERE genre = 'rock';
Counts the rows. Add a WHERE to count only the rows that pass the test.
id: count
SELECT AVG(seconds) FROM songs;
Example
SELECT MIN(seconds), MAX(seconds), AVG(seconds) FROM songs;
One answer from a whole column: SUM adds, AVG averages, MIN and MAX find the smallest and largest.
id: sum-avg-min-max
SELECT COUNT(*) AS how_many FROM songs;
Example
SELECT COUNT(*) AS how_many FROM songs;
Gives a result column a name of your choice, so the table reads clearly.
id: as-a-nicer-column-name
SELECT genre, COUNT(*) FROM songs GROUP BY genre;
Example
SELECT genre, COUNT(*) AS songs FROM songs GROUP BY genre;
Makes one row per group and works out the count, sum or average for each group.
id: group-by
SELECT songs.title, artists.country FROM songs JOIN artists ON songs.artist = artists.name;
Example
SELECT songs.title, artists.country
FROM songs
JOIN artists ON songs.artist = artists.name;
Puts two tables side by side, matching rows where the ON test is true. Name the table before the column: songs.title.
id: join
INSERT INTO songs (title, artist) VALUES ('Song', 'Artist');
Adds one new row. List the columns, then the values in the same order.
id: insert
UPDATE songs SET plays = plays + 1 WHERE id = 3;
Changes values in existing rows. Always add a WHERE, or every row changes.
id: update
DELETE FROM songs WHERE id = 3;
Removes rows. Always add a WHERE, or every row goes.
id: delete
CREATE TABLE pets (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER
);
Makes a new, empty table. Each column has a name and a type: INTEGER, REAL (decimals) or TEXT.
id: create-table
id INTEGER PRIMARY KEY
A column whose value is different in every row, so each row can be told apart and found again. It is the column other tables point to.
id: primary-key
song_id INTEGER REFERENCES songs(id)
A column that holds another table's key (a foreign key). It is how a review points to the song it is about.
id: link-to-another-table
import sqlite3
db = sqlite3.connect('music.db')
rows = db.execute('SELECT * FROM songs').fetchall()
Opens the database file, runs a query and gets the rows back as a list you can loop over.
id: connect-and-ask
db.execute('SELECT * FROM songs WHERE year > ?', (2015,))
Put a ? where a value goes and give the values in brackets after. Never join text into the query yourself.
id: a-for-each-value
db.commit()
INSERT, UPDATE and DELETE only stick once you commit. Without it the change is lost when the program ends.
id: save-the-changes