PostgreSQL DELETE Statement

You can use the DELETE statement to delete data from PostgreSQL tables.

Syntax

The following is the general syntax for deleting data using the DELETE statement:

DELETE FROM table_name WHERE [condition];

If no WHERE clause is specified, all records in the PostgreSQL table will be deleted.

Generally, we need to specify conditions in the WHERE clause to delete the corresponding records. The condition statement can use AND or OR operators to specify one or more conditions.

Examples

Create the COMPANY table (Download the COMPANY SQL file), with the data content as follows:

exampledb# select * from COMPANY;
 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen |  25 | Texas     |  15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)

The following SQL statement will delete the record with ID 2:

exampledb=# DELETE FROM COMPANY WHERE ID = 2;

The result obtained is as follows:

 id | name  | age | address     | salary
----+-------+-----+-------------+--------
  1 | Paul  |  32 | California  |  20000
  3 | Teddy |  23 | Norway      |  20000
  4 | Mark  |  25 | Rich-Mond   |  65000
  5 | David |  27 | Texas       |  85000
  6 | Kim   |  22 | South-Hall  |  45000
  7 | James |  24 | Houston     |  10000
(6 rows)

From the above results, it can be seen that the data with id 2 has been deleted.

The following statement will delete the entire COMPANY table:

DELETE FROM COMPANY;
Other Extensions