Skip to content
Hands-on Mentor
ENTR
Back to blog

SQL INSERT, UPDATE and DELETE: how to change data safely

SQL INSERT adds rows, UPDATE changes them and DELETE removes them. Learn the syntax, the WHERE rule that protects your data, and common errors.

4 min read

SQL INSERT, UPDATE and DELETE are the three commands that change data. INSERT adds new rows to a table, UPDATE changes values in existing rows, and DELETE removes rows. SELECT only reads data. These three write to it, so you need to use them with care.

The examples use the student table of the Northbridge University database in Port Alder, which starts with 120 students. Each example ran on a fresh copy of the database.

How do you add a row with INSERT?

Name the table, list the columns, then give the values in the same order:

INSERT INTO student (student_no, first_name, last_name, department_id, year, enrollment_date, city)
VALUES ('202609001', 'Maya', 'Lindqvist', 2, 1, '2026-09-21', 'Port Alder');

Check the new row:

SELECT id, student_no, first_name, last_name, year, city, has_scholarship, gpa
FROM student
WHERE student_no = '202609001';
idstudent_nofirst_namelast_nameyearcityhas_scholarshipgpa
121202609001MayaLindqvist1Port AlderfalseNULL

Three things happened here. The database created id 121 on its own. has_scholarship got its default value, false. gpa stayed NULL, because we gave no value and the column has no default.

You can insert several rows in one command by adding more value groups, separated by commas:

INSERT INTO student (student_no, first_name, last_name, department_id, year, enrollment_date, city)
VALUES ('202609002', 'Noah', 'Brandt', 3, 1, '2026-09-21', 'Lisbon'),
       ('202609003', 'Elena', 'Rossi', 3, 1, '2026-09-21', 'Seoul');

After this, SELECT COUNT(*) FROM student returns 123.

How do you change existing rows with UPDATE?

UPDATE has three parts: the table, the new values after SET, and a WHERE condition that picks the rows:

UPDATE student
SET has_scholarship = TRUE, gpa = 3.40
WHERE student_no = '202609001';

The database reports one changed row. You can also calculate the new value from the old one:

UPDATE student
SET year = year + 1
WHERE year < 4 AND student_no IN ('202609002', '202609003');

Both students were in year 1 and are now in year 2. The year < 4 check also keeps the update from pushing a student past year 4.

How do you remove rows with DELETE?

DELETE FROM student
WHERE student_no = '202609002';

One row is removed, and the table now has 122 rows. DELETE removes whole rows. To clear only one value inside a row, use UPDATE ... SET column = NULL instead.

What happens if you forget WHERE?

The command applies to every row in the table. This is the most serious mistake with UPDATE and DELETE:

UPDATE student SET city = 'Port Alder';

The database reports 123 changed rows. Now every student lives in Port Alder, and the original cities are gone. A grouped count confirms it: one group, Port Alder, with 123 students.

A safe habit has three steps:

1. Write the WHERE condition first, in a SELECT, and check which rows it returns.

2. Replace SELECT * with UPDATE or DELETE, and keep the same WHERE.

3. Run the command inside a transaction, so you can undo it with ROLLBACK. A transaction groups commands so that they succeed or fail together.

What errors can INSERT, UPDATE and DELETE cause?

The database protects its rules. These are real messages from the examples above (PostgreSQL wording):

CommandMistakeMessage
INSERT without a required columnLeft out last_namenull value in column "last_name" of relation "student" violates not-null constraint
INSERT with an invalid valueyear of 7new row for relation "student" violates check constraint "student_year_check"
INSERT with a repeated valueA student_no that existsduplicate key value violates unique constraint "student_student_no_key"
DELETE of a row that other rows useDelete a department that has instructorsupdate or delete on table "department" violates foreign key constraint "instructor_department_id_fkey" on table "instructor"

These errors help you. Without them, the table could fill with students who have no name or with two students who share one number. To delete a department, first move or delete the instructors and students that point to it.

What is the difference between the three commands?

CommandChangesNeeds WHERE?Example
INSERTAdds new rowsNoEnroll a new student
UPDATEChanges values in existing rowsAlmost alwaysGive a student a scholarship
DELETERemoves whole rowsAlmost alwaysRemove a duplicate student record

Together with SELECT, these four commands cover most daily work with data.

What to read next

  • Practice in your browser: [INSERT](/learn/sql/insert), [UPDATE](/learn/sql/update) and [DELETE and safe changes](/learn/sql/delete-safe-changes)
  • Understand the rules behind the errors: [Primary key vs foreign key](/blog/primary-key-vs-foreign-key)
  • Handle empty values: [SQL NULL values](/blog/sql-null-values)
  • Browse the whole course: [SQL course](/courses/sql)
  • #sql
  • #insert
  • #update
  • #delete
All posts