← SQL and databases

SQL reference

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)

Asking questions

SELECT * FROM songs;

Example

SELECT * FROM songs;
SELECT everything

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;
SELECT some columns

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;
WHERE

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, OR

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%';
LIKE

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;
ORDER BY

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;
LIMIT

Stops after that many rows. With ORDER BY it gives a top three or bottom three.

id: limit

Counting and adding up

SELECT COUNT(*) FROM songs;

Example

SELECT COUNT(*) FROM songs WHERE genre = 'rock';
COUNT

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;
SUM, AVG, MIN, MAX

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;
AS (a nicer column name)

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;
GROUP BY

Makes one row per group and works out the count, sum or average for each group.

id: group-by

Tables that link

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;
JOIN

Puts two tables side by side, matching rows where the ON test is true. Name the table before the column: songs.title.

id: join

Changing the data

INSERT INTO songs (title, artist) VALUES ('Song', 'Artist');
INSERT

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;
UPDATE

Changes values in existing rows. Always add a WHERE, or every row changes.

id: update

DELETE FROM songs WHERE id = 3;
DELETE

Removes rows. Always add a WHERE, or every row goes.

id: delete

Designing tables

CREATE TABLE pets (
  id INTEGER PRIMARY KEY,
  name TEXT,
  age INTEGER
);
CREATE TABLE

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
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)
Link to another table

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

SQL from Python

import sqlite3
db = sqlite3.connect('music.db')
rows = db.execute('SELECT * FROM songs').fetchall()
Connect and ask

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,))
A ? for each value

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()
Save the changes

INSERT, UPDATE and DELETE only stick once you commit. Without it the change is lost when the program ends.

id: save-the-changes