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
RETURNING in PostgreSQL
The RETURNING clause in PostgreSQL is a powerful feature that allows developers to retrieve data directly after executing SQL operations such as INSERT, UPDATE, or DELETE. This feature eliminates the need for multiple queries to fetch modified data, resulting in optimized performance, cleaner code,
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 - CREATE TABLE
In PostgreSQL, the CREATE TABLE statement is used to define a new table within a database. It allows us to specify the table's structure, including column names, data types, and constraints, ensuring data integrity and consistency. Understanding the PostgreSQL table creation process is essential for
5 min read
PostgreSQL - INSERT
PostgreSQL INSERT statement is one of the fundamental SQL commands used to add new rows to a specified table within a PostgreSQL database. This command allows users to insert data efficiently, whether for a single record or multiple records at once. With the PostgreSQL INSERT INTO clause, we can spe
4 min read
PostgreSQL MERGE Statement
The MERGE statement in PostgreSQL is a powerful data manipulation tool introduced in PostgreSQL 15, enabling conditional INSERT, UPDATE, and DELETE operations in a single command. This feature streamlines data synchronization between tables, making it ideal for tasks such as upserts and handling tab
4 min read
PostgreSQL Exercises
PostgreSQL is a powerful, open-source relational database system that supports complex queries, data types, and performance optimization features. This article provides PostgreSQL practice exercises with solutions, allowing learners to explore how joins, aggregations, triggers, and foreign keys work
15+ min read
PostgreSQL - Deleting Duplicate Rows using Subquery
In PostgreSQL, handling duplicate rows is a common task, especially when working with large datasets. Fortunately, PostgreSQL provides several techniques to efficiently delete duplicate rows, and one of the most effective approaches is using subqueries.In this article, we will demonstrate how to ide
3 min read