• iconteach@devfunda.com

DELETE Statement

DELETE Statement

31-08-2025 16:13:16 Time to read : 12 Minutes

The DELETE statement in PostgreSQL is an essential command used to remove records from a table. It allows you to get rid of outdated or unnecessary data, ensuring that your database remains clean, organized, and up to date.

The DELETE statement is used to remove one or more records (rows) from a table. Important: If you don’t use a WHERE clause, all rows in the table will be deleted!

Syntax

DELETE FROM table_name
WHERE condition;

table_name: The table from which you want to remove data.
WHERE: Decides which rows to delete.

Delete a Single Row
Removes the student whose student_id is 5.

DELETE FROM students WHERE student_id = 5;

Delete All Rows
Deletes all rows in the students table but keeps the table structure.

DELETE FROM students;

Safer Delete Using RETURNING
Deletes the student with ID 10 and also returns the deleted row. (This is PostgreSQL-specific and very handy).

DELETE FROM students
WHERE student_id = 10
RETURNING *;

 

Want to learn in class