PostgreSQL - DELETE USING
Last Updated :
25 Oct, 2024
The PostgreSQL DELETE USING statement is a powerful and efficient way to delete duplicate rows from a table, especially when duplicates are based on specific conditions. This method is particularly useful in situations where we need to remove rows by comparing data within the same table or across multiple tables.
In this article, we will explain the DELETE USING syntax, provide examples to demonstrate its functionality and cover best practices for safely deleting duplicate rows in PostgreSQL.
What is DELETE USING in PostgreSQL?
In PostgreSQL, the DELETE USING statement allows us to delete rows based on conditions that involve multiple instances of the same table or multiple tables. It is especially handy for removing duplicates by defining conditions to identify which duplicate rows to keep and which to delete.
Syntax:
DELETE FROM table_name row1
USING table_name row2
WHERE condition;
Key Terms
- table_name: The table from which we want to delete rows.
- row1 and row2: Aliases for the table, making it possible to refer to the same table in the
DELETE
and USING
clauses.
- condition: The criteria used to identify which rows to delete.
Examples of PostgreSQL DELETE USING
Let’s set up a sample table named basket to demonstrate the DELETE USING statement in PostgreSQL. The basket
table stores information about fruits and includes an id
column to uniquely identify each row.
Query:
CREATE TABLE basket(
id SERIAL PRIMARY KEY,
fruit VARCHAR(50) NOT NULL
);
INSERT INTO basket(fruit) values('apple');
INSERT INTO basket(fruit) values('apple');
INSERT INTO basket(fruit) values('orange');
INSERT INTO basket(fruit) values('orange');
INSERT INTO basket(fruit) values('orange');
INSERT INTO basket(fruit) values('banana');
SELECT * FROM basket;
Output
PostgreSQL Delete UsingIdentifying Duplicate Rows
Now that we have set up the sample table, we will query for the duplicates using the following. Before removing duplicates, let us identify them by querying the basket
table. This query groups fruits and counts occurrences to help us identify duplicates.
Query:
SELECT
fruit,
COUNT( fruit )
FROM
basket
GROUP BY
fruit
HAVING
COUNT( fruit )> 1
ORDER BY
fruit;
Output
Identifying Duplicate RowsExample 1: Removing Duplicates Using DELETE USING
Now that we know the duplicate rows we can use the DELETE USINGstatement to remove duplicate rows as follows. The query keeps the row with the smallest id
for each fruit and deletes the others.
Query:
DELETE FROM
basket a
USING basket b
WHERE
a.id < b.id
AND a.fruit = b.fruit;
Explanation:
This query works by comparing each row in the table with every other row. If it finds duplicates ('a.fruit = b.fruit') and the 'id' of the first row ('a.id') is less than the 'id' of the second row ('b.id'), it deletes the row with the smaller 'id'. This should remove all duplicate from the table basket
Verification
To verify that duplicates have been removed, run the following query:
Query:
SELECT
fruit,
COUNT( fruit )
FROM
basket
GROUP BY
fruit
HAVING
COUNT( fruit )> 1
ORDER BY
fruit;
Output
PostgreSQL Delete Using Example1Explanation:
This query should return no rows, indicating all duplicate fruits have been successfully deleted from the basket
table.
Full Table Check: Remaining Rows
After removing duplicates, we can confirm the contents of the basket
table to ensure only unique fruits remain. This Query will indicate that all duplicate rows have been removed, and only one instance of each fruit remains in the table.
Query:
SELECT * FROM basket ORDER BY id;
Output
id | fruit |
---|
1 | Apple |
2 | Banana |
4 | Orange |
6 | Grapes |
Important Points About PostgreSQL DELETE USING Statement
- The DELETE USING statement is used to delete rows from a table based on a condition involving multiple tables or multiple instances of the same table.
- Aliases can be used for tables to make the query more readable, especially when the same table is referenced multiple times.
- Be cautious with the DELETE USING statement as it can delete multiple rows. Ensure that the WHEREcondition is correctly specified to avoid unintentional data loss.
- Test the
DELETE
operation on a subset of your data to ensure it works as expected.
Conclusion
The DELETE USING statement in PostgreSQL is an effective tool for deleting rows based on complex conditions, such as removing duplicate rows from a table. With options to define specific criteria using aliases and multiple WHERE conditions, DELETE USING is flexible for data maintenance tasks. By following best practices and carefully defining conditions, PostgreSQL users can efficiently clean up data and ensure database integrity.
Similar Reads
PostgreSQL - DELETE 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
4 min read
PostgreSQL - ALTER TABLE In PostgreSQL, the ALTER TABLE statement is a powerful and essential tool that allows us to modify the structure of an existing table to meet evolving database needs. With PostgreSQL ALTER TABLE, we can perform various modifications on the table without disrupting the ongoing operations of our datab
6 min read
PostgreSQL CRUD Operations using Java CRUD (Create, Read, Update, Delete) operations are the basic fundamentals and backbone of any SQL database system. CRUD is frequently used in database and database design cases. It simplifies security control by meeting a variety of access criteria. The CRUD acronym identifies all of the major funct
8 min read
PostgreSQL - DROP TRIGGER The DROP TRIGGER statement in PostgreSQL is essential for managing database triggers effectively. Triggers in PostgreSQL are database callbacks that automatically execute functions in response to certain events, such as INSERT, UPDATE, or DELETE. DROP TRIGGER provides database administrators and dev
4 min read
PostgreSQL - Trigger A PostgreSQL trigger is a powerful tool that allows automatic invocation of a function whenever a specified event occurs on a table. Events that can trigger a function include INSERT, UPDATE, DELETE, or TRUNCATE. Triggers help maintain data integrity and automate complex database operations.What is
3 min read
PostgreSQL - DROP SCHEMA PostgreSQL offers a powerful schema management system, allowing database administrators to organize and manage objects within specific schemas. The DROP SCHEMA statement in PostgreSQL provides a straightforward way to delete entire schemas and the associated objects, making it a valuable tool for da
4 min read
PL/SQL DELETE Statement In PL/SQL(Procedural Language/Structured Query Language), the DELETE statement is the powerful command used to remove one or more records from the database table. It is an essential part of database management and enables the users to efficiently manage and maintain the data integrity by selectively
6 min read
PostgreSQL - WHERE clause The PostgreSQL WHERE clause is a critical component of SQL queries, allowing users to filter records based on specified conditions. In this tutorial, we'll explore how the WHERE clause works in PostgreSQL, its integration with the SELECT statement, and various examples. By using the WHERE clause, we
6 min read
Delete Views in SQL Server In the area of relational databases, SQL Server is one of the most powerful and popular systems. It is flexible to make possible the development of complex data structures and their manipulation. SQL Server offers a crucial tool for managing data which is Delete Views. They allow users to delete row
4 min read
SQL DELETE Statement The SQL DELETE statement is one of the most commonly used commands in SQL (Structured Query Language). It allows you to remove one or more rows from the table depending on the situation. Unlike the DROP statement, which removes the entire table, the DELETE statement removes data (rows) from the tabl
4 min read