Easy Tutorial
❮ Postgresql Trigger Postgresql Update ❯

PostgreSQL DELETE Statement

You can use the DELETE statement to remove data from a PostgreSQL table.

Syntax

The following is the general syntax for the DELETE statement to delete data:

DELETE FROM table_name WHERE [condition];

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

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

Example

Create the COMPANY table (download COMPANY SQL file), with the following data content:

tutorialprodb# 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 data with ID 2:

tutorialprodb=# DELETE FROM COMPANY WHERE ID = 2;

The result 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 result, 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;
❮ Postgresql Trigger Postgresql Update ❯