← Teacher guide

Change and remove rows

Students change rows with UPDATE and remove rows with DELETE, and learn that WHERE decides which rows.

SQL and databases · More than one table · about 25 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 7 min · Modify and Make 10 min

Running the lesson

  1. Predict: ask students to vote before running. The no-WHERE slip is the key idea of the lesson.
  2. Run: after the 15, press Reset so students see the table come back. In a real database there is no Reset, so people test with SELECT before they UPDATE or DELETE.
  3. Investigate: plays + 100 uses the old value. Ask what plays = 100 would do instead.
  4. Tip to share: write the WHERE as a SELECT first, check the rows, then change SELECT * to DELETE.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Remembering WHERE. Teach the habit: write it as a SELECT first, then change the first word.

When students get stuck

What you will seeA question to askThe fix
Every row changed.Does your query have a WHERE line?Press Reset, add the WHERE line, and run again.
Nothing changed.Does any row pass your WHERE test?Check the id or spelling against the table.
syntax error near DELETEIs FROM in the DELETE line?It is DELETE FROM table.

Answers to the checks

  1. Predict: This change has no WHERE line. What will the last line show?
    15. Without WHERE, UPDATE changes every row, so all 15 songs have 0 plays.
  2. Delete the folk songs: Run the query. How many songs are left after the folk songs are deleted?
    13
  3. Add 100 plays: Which line adds 100 plays to the song with id 3?
    UPDATE songs SET plays = plays + 100 WHERE id = 3;
    plays + 100 uses the old value, and WHERE id = 3 picks one row.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

UPDATE songs
SET year = 2018
WHERE id = 6;

SELECT title, year FROM songs WHERE id = 6;

Make

DELETE FROM songs
WHERE plays < 2000;

SELECT title FROM songs;

Stretch and support

Stretch: Raise the plays of every pop song by 10% with plays * 1.1, then look at the results.

Support: Give the UPDATE with the WHERE line missing and ask students to complete it.

Exit ticket

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

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

What each step is for

Where it leads

Next lesson: designing a table, with keys that make each row unique.