PostgreSQL Delete query is used to delete records from the table. If we supply the where clause then it will delete the specific rows and in case the where clause is omitted then the complete records are deleted and the empty table is left. To perform the delete operation, we should have the Delete table permission present on the object.

The syntax of Delete Query is as mentioned below.


Syntax:

DELETE FROM tablename WHERE [condition];


1. Delete Query With Where Clause

We have created a table named PRODUCTS_DETAIL and it has mentioned values, Now we will use the DELETE query to delete the records where the PRODUCT_NUMBER is 101.


Command:

postgres=# SELECT * FROM PRODUCTS_DETAIL;
postgres=# DELETE FROM PRODUCTS_DETAIL WHERE product_number=101;


Output:

From the below output, we can see that row 101 of the column PRODUCT_NUMBER has been deleted.


postgresql delete command with where cluase


2. Delete Query Without Where Clause

The Delete Query without where clause will delete all records from the table and left the table empty. It should be avoided unless required. In this section, we will use the Delete query without mentioning the where clause to delete all records from the table PRODUCTS_DETAIL.


Command:

postgres=# DELETE FROM PRODUCTS_DETAIL;


Output:

All records from the table PRODUCTS_DETAIL have been deleted as we can see in the below output.


postgresql delete all records