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
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Introduction of DBMS (Database Management System) A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. They enable calculations across a specific set of rows, known as a "window," while retaining the individual rows in the dataset. Unlike traditional aggregate functions that summarize data for the entire group, win
7 min read