← Code

SQL and databases

Ask a table a question and get an answer back.

Start1. Look inside a table
For teachers Usually Years 9–10 · 11 lessons

Ask a database questions. Pick the rows and columns you want, filter, sort, count and join, then change the data and design your own tables.

  • Usually Years 9–10
  • Lessons 11
  • Do first Data detective
Teacher guide

Standards, in general: main focus: Trace algorithms for errors (8P06), Acquire, store and validate data (10P01), Model and query entities and relationships (10P03); touches on: Acquire and validate data (8P01), Model structured data (8P03). What this means

Ask a table

Choose columns, filter rows and put them in order.

1Look inside a tableNot started

Read data from a table with SELECT, choosing the columns you want.

  1. Predict
  2. Investigate › Count the columns
  3. Investigate › Pick the right query
  4. Modify
  5. Make
Start lesson
2Keep only the rows you wantNot started
3Put the rows in orderNot started

Sort with ORDER BY, reverse with DESC, and keep the top few with LIMIT.

  1. Predict
  2. Investigate › Which comes first?
  3. Investigate › Newest song
  4. Modify
  5. Make
Start lesson

Summarise

Count, add up and average rows, then do it for each group.

4Count and add upNot started

Turn many rows into one answer with COUNT, SUM, AVG and MAX.

  1. Predict
  2. Investigate › Add them up
  3. Investigate › Pick the function
  4. Modify
  5. Make
Start lesson
5One answer for each groupNot started

Use GROUP BY to count or add up for each genre, artist or year at once.

  1. Predict
  2. Investigate › Count the piles
  3. Investigate › Total for each artist
  4. Modify
  5. Make
Start lesson

More than one table

Join tables, change data and design tables with keys.

6Join two tablesNot started

Combine rows from two tables with JOIN ... ON, matching a column they share.

  1. Predict
  2. Investigate › Count the Australian songs
  3. Investigate › Read the matching rule
  4. Modify
  5. Make
Start lesson
7Add a rowNot started

Put a new row into a table with INSERT INTO, then check it with SELECT.

  1. Predict
  2. Investigate › Look at the empty columns
  3. Investigate › Which line works?
  4. Modify
  5. Make
Start lesson
8Change and remove rowsNot started

Fix rows with UPDATE, remove them with DELETE, and always say which rows with WHERE.

  1. Predict
  2. Investigate › Delete the folk songs
  3. Investigate › Add 100 plays
  4. Modify
  5. Make
Start lesson
9Design your own tableNot started

Make a table with CREATE TABLE, choose a type for each column, and give every row a key.

  1. Predict
  2. Investigate › Pick the type
  3. Investigate › Why a key?
  4. Modify
  5. Make
Start lesson
10Link tables with keysNot started

Store another table's id in a column, so a row can point at exactly one row elsewhere.

  1. Predict
  2. Investigate › Find an average
  3. Investigate › Changing a title
  4. Modify
  5. Make
Start lesson

SQL from Python

Run queries from a program and use the answers.

11Ask the database from PythonNot started

Run a SQL query from a Python program, loop over the rows it returns, and pass values in safely with ?.

  1. Predict
  2. Investigate › Take a row apart
  3. Investigate › A safer way
  4. Modify
  5. Make
Start lesson

Progress is saved in this browser only. Nothing is sent anywhere.

For teachers: Teacher guide (notes, answers, solutions, slides and worksheets)