← SQL and databases

SQL and databases: recap

For after the lessons: one card per lesson, to check what you remember. Answer each question in your head or on paper first, then check it. Nothing here is marked.

Ask a table

1. Look inside a table

Try firstWhat is the difference between a row and a column in the songs table?

Check your answer

A row is one song with all its details. A column is one kind of detail, such as title, for every song.

A table has columns (title, artist, year...) and rows (one per song). SELECT picks columns and FROM names the table. SELECT * means every column. Without a filter every row comes back.

SELECT chooses the columns. FROM names the table.
Open the lesson →

2. Keep only the rows you want

Try firstWhat does WHERE do, and why is 'pop' in quotes?

Check your answer

It keeps only the rows where the test is true. Quotes show that pop is a value, not a column.

WHERE tests each row and keeps the rows where the test is true. A test is a column, a comparison (=, >, <) and a value. Text values go in single quotes. Numbers do not.

WHERE keeps only the rows that pass a test.
Open the lesson →

3. Put the rows in order

Try firstWhat is the order of the parts of a query so far?

Check your answer

SELECT, FROM, WHERE, ORDER BY, LIMIT.

ORDER BY column sorts rows smallest first. DESC flips it. LIMIT n keeps the first n rows after sorting. The parts always come in this order: SELECT, FROM, WHERE, ORDER BY, LIMIT.

ORDER BY sorts. DESC puts the biggest first.
Open the lesson →

Summarise

4. Count and add up

Try firstWhat is the difference between SELECT title and SELECT COUNT(*)?

Check your answer

SELECT title gives one row for each song. COUNT(*) gives one row with how many rows there are.

COUNT(*), SUM(col), AVG(col), MIN(col) and MAX(col) turn many rows into one value. WHERE filters first, so the summary covers only the rows that pass.

COUNT counts rows instead of listing them.
Open the lesson →

5. One answer for each group

Try firstWhat does GROUP BY genre do?

Check your answer

It makes one pile for each genre, so a summary such as COUNT(*) gives one answer for each genre.

GROUP BY column makes a pile for each value of the column and gives each pile one summary row. Show the grouped column beside the summary. The order of the parts is SELECT, FROM, WHERE, GROUP BY, ORDER BY, LIMIT.

GROUP BY makes one row for each group.
Open the lesson →

More than one table

6. Join two tables

Try firstWhy keep artists in their own table instead of a country column in songs?

Check your answer

A country is stored once for each artist, so a mistake is fixed in one place and nothing is repeated.

A JOIN pairs rows from two tables. ON says which columns must match. A column is written table.column so SQL knows which table it means. After the join the result is an ordinary table: you can filter it with WHERE, group it with GROUP BY and sort it.

JOIN puts two tables side by side where a value matches.
Open the lesson →

7. Add a row

Try firstWhat goes in the brackets after VALUES?

Check your answer

One value for each column, in the table's column order, with text in single quotes.

INSERT INTO table VALUES (...) adds one row. Give a value for every column in order, or name the columns first: INSERT INTO table (a, b) VALUES (...). Columns you leave out are NULL. Follow a change with a SELECT to check it.

INSERT adds a new row.
Open the lesson →

8. Change and remove rows

Try firstWhat happens if you write DELETE FROM songs; with no WHERE?

Check your answer

Every row in the table is removed. WHERE chooses which rows.

UPDATE table SET column = value WHERE test changes the rows that pass the test. DELETE FROM table WHERE test removes them. With no WHERE, they act on every row. Check the rows with a SELECT first.

UPDATE changes values in rows that match.
Open the lesson →

9. Design your own table

Try firstWhat is a primary key?

Check your answer

A column whose value is different in every row, so each row can be found and pointed at.

CREATE TABLE name (column TYPE, ...) makes a table. INTEGER holds whole numbers and TEXT holds text. A PRIMARY KEY column is different in every row. Add rows with INSERT INTO.

CREATE TABLE makes a new, empty table.
Open the lesson →

10. Link tables with keys

Try firstWhy does reviews store song_id instead of the song's title?

Check your answer

An id is never repeated or changed, so each review points at exactly one song and a title can be fixed in one place.

A foreign key is a column that holds another table's primary key, such as reviews.song_id holding songs.id. A join on the pair puts each review beside its song. Storing the id means each fact lives in one place.

A key links a row in one table to a row in another.
Open the lesson →

SQL from Python

11. Ask the database from Python

Try firstWhy pass a typed genre with ? instead of joining it into the query with +?

Check your answer

So the typed text is always a value and can never change the SQL itself.

conn.execute("SQL") sends a query and gives back the rows. Each row is a pair (a tuple) you can take apart by position. ? in the query marks a value, passed separately as (value,), so what a person types can never become SQL.

Open the lesson →