SQL UPDATE and DELETE

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

SQL UPDATE and DELETE

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement changes Mia's grade to 'B' without affecting any other student?
A UPDATE Students SET grade = 'B' WHERE name = 'Mia';
B UPDATE Students SET grade = 'B';
2. What happens if you run DELETE FROM Orders; with no WHERE condition?
A Every record in the Orders table is deleted
B Only the last record in the Orders table is deleted
3. Which keyword is used to change the value in a field of existing records?
A CHANGE
B UPDATE
4. Which statement correctly removes only the record with StudentID 12?
A DELETE Students WHERE StudentID = 12;
B DELETE FROM Students WHERE StudentID = 12;
5. True or false: UPDATE Products SET price = 10; only changes one product.
A False: it sets the price of every product to 10
B True: UPDATE always changes just one record

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1UPDATE Students SET year = 10 WHERE id = 7; AUPDATE Students SET email = 'sam@school.edu' WHERE id = 12;
2DELETE FROM Students WHERE id = 7; BDELETE FROM Students;
3Change a student's email address, but only for the student with id 12 CFROM is missing. The correct syntax is DELETE FROM Students WHERE id = 7;
4Danger: leaving out WHERE deletes every record in the table DChanges the year field to 10 for only the student whose id is 7
5UPDATE Students SET year = 10; ERemoves only the record for the student whose id is 7
6DELETE Students WHERE id = 7; FWHERE is missing, so every student's year is changed to 10. Add WHERE id = 7; to change just one record.

Definitions

UPDATE table SET field = value WHERE condition;

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______ students SET year = 10 WHERE id = 3;
2.
UPDATE students ___ year = 10 WHERE id = 3;
3.
DELETE ____ students WHERE id = 7;
4.
DELETE FROM students _____ id = 7;
5.
UPDATE students SET year = 10 WHERE id _ 3;

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A UPDATE students
B SET year == 10
C WHERE id = 7;
Question 2
A DELETE FROM orders
B WHERE id = 12;
C UPDATE orders
D SET status = sent
E WHERE id = 15;
Question 3
A UPDATE pets
B SET age = 4
C WHERE id = 2;
D DELETE FROM pets;
Question 4
A UPDATE songs
B SET likes = 20
C WHEN title = 'Echo';
Question 5
A -- remove used tickets
B DELETE tickets
C WHERE used = 1;

Answer Key

SQL UPDATE and DELETE
Section A: Quick Questions
Q1
A — UPDATE Students SET grade = 'B' WHERE name = 'Mia';
Q2
A — Every record in the Orders table is deleted
Q3
B — UPDATE
Q4
B — DELETE FROM Students WHERE StudentID = 12;
Q5
A — False: it sets the price of every product to 10
Section B: Match the Pairs
Q1
UPDATE Students SET year = 10 WHERE id = 7; → Changes the year field to 10 for only the student whose id is 7
Q2
DELETE FROM Students WHERE id = 7; → Removes only the record for the student whose id is 7
Q3
Change a student's email address, but only for the student with id 12 → UPDATE Students SET email = 'sam@school.edu' WHERE id = 12;
Q4
Danger: leaving out WHERE deletes every record in the table → DELETE FROM Students;
Q5
UPDATE Students SET year = 10; → WHERE is missing, so every student's year is changed to 10. Add WHERE id = 7; to change just one record.
Q6
DELETE Students WHERE id = 7; → FROM is missing. The correct syntax is DELETE FROM Students WHERE id = 7;
Section C: Definitions
Q1
UPDATE table SET field = value WHERE condition; — UPDATE changes field values and DELETE FROM removes records. Always add WHERE so only the chosen records change. Without it, every record in the table is changed or deleted.
Section D: Fill in the Blanks
Q1
UPDATE students SET year = 10 WHERE id = 3;
Q2
UPDATE students SET year = 10 WHERE id = 3;
Q3
DELETE FROM students WHERE id = 7;
Q4
DELETE FROM students WHERE id = 7;
Q5
UPDATE students SET year = 10 WHERE id = 3;
Section E: Spot the Error
Q1
B — SQL uses a single = to give a field its new value in SET. The line should be SET year = 10.
Q2
D — Text values must be in quotes. Without them, SQL treats sent as a field name. It should be SET status = 'sent'.
Q3
D — Leaving out WHERE deletes every record in the pets table. It should be DELETE FROM pets WHERE id = 9; so only one pet is removed.
Q4
C — The keyword that picks which records to change is WHERE, not WHEN. It should be WHERE title = 'Echo';
Q5
B — A DELETE statement needs the keyword FROM before the table name. It should be DELETE FROM tickets.