English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PostgreSQL DELETE Statement

You can use the DELETE statement to delete data from the 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.

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.

Online Example

Create COMPANY table (Download COMPANY SQL file ),The data content is as follows:

w3codeboxdb# 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 ID of 2 The data:

w3codeboxdb=# 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 result, it can be seen that the id of 2 The data has been deleted.

The following statement will delete the entire COMPANY table:

DELETE FROM COMPANY;