SQL: INSERT/UPDATE/DELETE
Computer Science · Databases
Question 1 of 10
0players name | points Cara | 13 Fay | 12 Jada | 77 Ben | 86 Kai | 28 The statement runs once on this table. INSERT INTO players (name, points) VALUES ('Dev', 38); How many rows does players contain afterwards?
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
SQL: INSERT/UPDATE/DELETE explained
SQL has commands for changing the data in a table as well as reading it. INSERT adds new rows, UPDATE changes values in existing rows, and DELETE removes rows. These are called data manipulation language (DML) commands, and together with SELECT they let a program keep a database up to date.
INSERT adds a new row to a table. You name the table, list the columns you are filling in, and give the values in the same order:
INSERT INTO Students (StudentID, Name, Year) VALUES (7, 'Priya', 11);
UPDATE changes values in rows that already exist. It needs a SET clause for the new values and, almost always, a WHERE clause to say which rows to change:
UPDATE Students SET Year = 12 WHERE StudentID = 7;
DELETE removes whole rows and also normally needs a WHERE clause to choose them:
DELETE FROM Students WHERE StudentID = 7;
A common mistake is to leave out the WHERE clause. Without it, UPDATE changes every row in the table and DELETE removes every row, so always check the condition before running the command.
Worked example
A table called Books has the columns BookID, Title and Copies. It currently holds one row: (1, Dune, 3).
- INSERT INTO Books (BookID, Title, Copies) VALUES (2, 'Emma', 1); adds a second row. The table now holds (1, Dune, 3) and (2, Emma, 1).
- UPDATE Books SET Copies = 5 WHERE BookID = 1; changes the Dune row to 5 copies. The Emma row is unchanged.
- DELETE FROM Books WHERE BookID = 2; removes only the Emma row, leaving the Dune row with 5 copies.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
players name | points Eli | 20 Dev | 76 Kai | 33 Gus | 45 Hana | 64 Jada | 85
The statement runs once on this table. INSERT INTO players (name, points) VALUES ('Ben', 74);
How many rows does players contain afterwards?
Medium
players name | points Eli | 25 Kai | 73 Ava | 38 Lena | 43
The statement runs once on this table. UPDATE players SET points = points + 7 WHERE points < 30;
What single value does SELECT SUM(points) FROM players; return afterwards?
Hard
players name | points Jada | 22 Hana | 54 Ava | NULL Kai | 64
The statement runs once on this table. UPDATE players SET points = points * 2 WHERE points > 48;
What single value does SELECT SUM(points) FROM players; return afterwards?