Last Updated : 12 Jul, 2025
The DELETE statement is a key command in PostgreSQL used to remove existing records from a table. By using DELETE, you can eliminate unwanted or outdated records, helping keep your database organized and up to date.
In this article, we will explore the DELETE statement, its syntax, and some practical examples to help you grasp how to use it. Knowing how to effectively use the DELETE statement ensures you maintain data integrity.
PostgreSQL DELETEDELETE
statement is used to remove existing records from a table. DELETE
statement is essential for maintaining and managing our database.Syntax
DELETE FROM table_name WHERE condition;
The below rules need to be followed while using the DELETE statement:
table_name
: The name of the table from which you want to delete records.condition
: A condition that specifies which records to delete. If this condition is omitted, all records in the table will be deleted.Let’s set up a sample database and table for the demonstration of the DELETE statement.
CREATE DATABASE company; CREATE TABLE employee ( employee_id INT PRIMARY KEY, first_name VARCHAR (255) NOT NULL, last_name VARCHAR (255) NOT NULL, manager_id INT, FOREIGN KEY (manager_id) REFERENCES employee (employee_id) ON DELETE CASCADE ); INSERT INTO employee ( employee_id, first_name, last_name, manager_id ) VALUES (1, 'Sandeep', 'Jain', NULL), (2, 'Abhishek ', 'Kelenia', 1), (3, 'Harsh', 'Aggarwal', 1), (4, 'Raju', 'Kumar', 2), (5, 'Nikhil', 'Aggarwal', 2), (6, 'Anshul', 'Aggarwal', 2), (7, 'Virat', 'Kohli', 3), (8, 'Rohit', 'Sharma', 3)
Create a database named "company" with the below command:
The value in the 'manager_id' column represents the senior manager who the employee reports to. If it's NULL, he/she doesn't report to anyone. The overall hierarchy looks like the below image: The current database tables look like below:
Example 1: Deleting a Single RowHere we will be deleting the employee data whose first name is "Raju".
Query:
DELETE FROM employee WHERE first_name = 'Raju';
Output:
Explanation: The row where 'first_name'
is "Raju" will be deleted from the employee
table.
Here we will delete multiple rows from the "employee" table. We will be deleting the data of the employee named "Abhishek Kelenia" and employees who work under him.
Query:
DELETE FROM employee WHERE last_name = 'Kelenia';
Output:
Explanation: The row where 'last_name'
is "Kelenia" will be deleted, along with any rows dependent on this employee due to the ON DELETE CASCADE
foreign key constraint.
ON DELETE CASCADE
clause ensures that deleting a parent row also deletes dependent child rows.RETURNING
clause to return the deleted rows, which is useful for verification.The DELETE
statement in PostgreSQL is a crucial command for maintaining and managing your database. By allowing you to remove unwanted data, it plays a significant role in data integrity and organization.Understanding its syntax, the importance of the WHERE
clause, and the implications of constraints like ON DELETE CASCADE
will empower you to use this command effectively.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4