New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› SQL› Inserting, Updating and Deleting

Inserting, Updating and Deleting

💻 SQL

Change data safely with INSERT, UPDATE and DELETE, and always scope changes with WHERE.

Lesson 6 of 9 Tutorial
0 of 9 completed

Reading data is safe. Changing it is not. Every write statement deserves a test query first.

By the end of this lesson you can:
  • Insert one or many rows.
  • Update specific rows.
  • Delete safely.

INSERT

List the columns explicitly so the statement keeps working if the table gains a column.

INSERT INTO students (name, level, email)
VALUES ('Amina Bello', 'SS2', 'amina@example.com');

INSERT INTO students (name, level)
VALUES
    ('Bola Ade', 'SS1'),
    ('Chidi Okeke', 'SS3');

UPDATE

Always include a WHERE clause. Run the same condition as a SELECT first to see which rows you are about to change.

-- Check first
SELECT id, name, score FROM students WHERE level = 'SS1';

-- Then update
UPDATE students
SET score = score + 5
WHERE level = 'SS1';

DELETE

Delete with a precise condition. To remove every row on purpose, use TRUNCATE, which is faster and resets auto-increment.

DELETE FROM students
WHERE id = 12;
Warning An UPDATE or DELETE without WHERE affects every row. Check the condition twice, or wrap the statement in a transaction.

Practice

  1. Insert three new courses in one statement.
  2. Increase the price of one course by 10%.
  3. Delete students who have no email and are inactive.

Hands-on exercise

Write two statements:
1. A SELECT that shows courses priced below 5000.
2. An UPDATE that raises those prices by 10%.
The UPDATE must not touch any other course.

Recap

INSERT adds rows, UPDATE changes them and DELETE removes them. Always preview with SELECT and keep the WHERE clause precise.

Example

INSERT INTO students (name, level, email)
VALUES ('Amina Bello', 'SS2', 'amina@example.com');

INSERT INTO students (name, level)
VALUES
    ('Bola Ade', 'SS1'),
    ('Chidi Okeke', 'SS3');

Exercises

Exercise 1. Safe price update Medium
Write two statements:
1. A SELECT that shows courses priced below 5000.
2. An UPDATE that raises those prices by 10%.
The UPDATE must not touch any other course.
SQL exercise-1.sql
Expected Output
Only courses below 5000 are increased, by exactly 10%.

Lesson Nav

Lesson sections