A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql/postgresql-delete/ below:

PostgreSQL - DELETE - GeeksforGeeks

PostgreSQL - DELETE

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 DELETE

Syntax

DELETE FROM table_name
WHERE condition;

The below rules need to be followed while using the DELETE statement:

Examples of PostgreSQL DELETE Statement

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 Row

Here 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.

Example 2: Deleting Multiple Rows

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.

Important Points About PostgreSQL DELETE Statement Conclusion

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