PostgreSQL DELETE Rows from Select
โก Smart Summary
PostgreSQL DELETE removes one or more rows from a table, and pairing it with a WHERE clause targets only the records that match a condition, while omitting WHERE clears every row in the table.

Delete Query in PostgreSQL
The DELETE statement in PostgreSQL is used to delete one or more records from a table. To delete only selected rows from a table, PostgreSQL allows you to combine the DELETE statement with the WHERE clause; otherwise, it deletes all records in the table.
PostgreSQL Delete Query Syntax
The DELETE statement can be defined using the syntax below:
[ WITH [ RECURSIVE ] with-query [, ...] ] DELETE FROM [ ONLY ] table-name [ * ] [ [ AS ] alias ] [ USING using-list ] [ WHERE condition(s) | WHERE CURRENT OF cursor-name] [ RETURNING * | output-expression [ [ AS ] output-name] [, ...] ]
Parameters
The following parameters are available:
- with-query: the WITH clause allows you to reference one or more subqueries by name inside the DELETE query.
- table-name: the name of the table from which records are to be deleted.
- alias: a substitute for the name of the target table.
- using-list: table expressions that allow columns from other tables to be used in the WHERE clause.
- condition(s): optional. These are the conditions that must be satisfied for records to be deleted. If this section is not provided, all table-name records will be deleted.
- cursor-name: the cursor to be used in the WHERE CURRENT OF condition. The last row fetched by this cursor will be deleted.
- output-expression: the expression to be processed and returned by the DELETE statement after every row is deleted.
- output-name: the name to be used for the returned column.
Note that since the DELETE statement deletes the entire row, you do not need to specify the column names.
PostgreSQL Delete Query with One Condition
The DELETE statement can be used with a single condition, which is set using the WHERE clause. Consider the Price table with the following data:
Price:
Let us delete the record with an id of 4:
DELETE FROM Price WHERE id = 4;
The above command deletes the records in which the id is 4. Let us confirm whether the deletion was successful:
The row with an id of 4 has been deleted.
Delete Query with Two Conditions
The PostgreSQL DELETE statement can also take two conditions. The two conditions should be joined using the AND operator. We will use the following table:
Price:
Consider the example given below:
DELETE FROM Price WHERE id = 3 AND price = 300;
In the above command, we are deleting the row in which the id is 3 and the price is 300. We can now query the table:
SELECT * FROM Price;
This returns the following:
The record with an id of 3 and a price of 300 was deleted.
PostgreSQL Delete Query using EXISTS Condition
With the EXISTS condition, you can make the DELETE more complex. Sometimes there may be a need to delete records in one table based on records in another table.
Because the FROM clause does not allow you to list records from more than one table when performing a delete, the EXISTS clause becomes very useful. We have the following two tables:
Book:
Price:
We can then run the following query:
DELETE FROM Book WHERE EXISTS (SELECT 1 FROM Price WHERE Price.id = Book.id AND price < 250 );
The command deletes rows from the Book table that have a matching id in the Price table with a price below 250.
The Book table is now as follows:
The record with an id of 1 was deleted.
How To Delete a Row in PostgreSQL using pgAdmin
The same delete queries can also be run visually through the pgAdmin interface, following the steps below:
With One Condition
Step 1) Login to your pgAdmin account. Open pgAdmin and log in to your account using your credentials.
Step 2) Create a Demo Database.
- From the navigation bar on the left, click Databases.
- Click Demo.
Step 3) Type the below query in the query editor:
DELETE FROM Price WHERE id = 4;
Step 4) Click the Execute button.
Step 5) Let us check whether the row was deleted:
With Two Conditions
Step 1) Login to your pgAdmin account.
Step 2)
- From the navigation bar on the left, click Databases.
- Click Demo.
Step 3) Type the query in the query editor:
DELETE FROM Price WHERE id = 3 AND price = 300;
Step 4) Click the Execute button.
Step 5) Let us check whether the deletion was successful:
Using EXISTS Condition
Step 1) Login to your pgAdmin account.
Step 2)
- From the navigation bar on the left, click Databases.
- Click Demo.
Step 3) Type the query in the query editor:
DELETE FROM Book WHERE EXISTS (SELECT 1 FROM Price WHERE Price.id = Book.id AND price < 250 );
Step 4) Click the Execute button.
Step 5) Let us check whether the deletion was successful:
Download the Database used in this Tutorial
















