PostgreSQL DELETE
To remove or delete existing records from a table, the PostgreSQL DELETE statement is used.
Syntax: To delete all rows from a table.
DELETE FROM table_name;
Syntax: To delete specific rows from a table.
DELETE FROM table_name WHERE conditions;
DELETE statement using UI:
Other than Query tool, we can also DELETE statement in PostgreSQL using UI. To DELETE statement using UI in PostgreSQL, follow the below steps.
- Right-click on the selected table.
- Move your cursor over the option scripts.
- Click on the “DELETE script” option.
- Put the values on the place of “?”.
- Fulfill the WHERE condition.
- Click on the “play” button.
The query will thus be executed.
Example 1: Deleting single row from a table.
Employment table before removal:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
3 | C | 65 |
4 | D | 80 |
5 | E | 78 |
DELETE FROM “EMPLOYMENT” WHERE “ID” = 3; |
Explanation:
The EMPLOYMENT is an already existing table, from which we are deleting the row where the value of ID is 3.
Employment table after removal:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
4 | D | 80 |
5 | E | 78 |
Example 2: Deleting multiple rows from a table.
Employment table before removal:
ID STATE RATE 1 A 60 2 B 70 3 C 65 4 D 80 5 E 78
DELETE FROM “EMPLOYMENT” WHERE “ID” > 2; |
Explanation:
The EMPLOYMENT is an already existing table, from which we are deleting the row where the value of ID is greater than 2.
Employment table after removal:
ID | STATE | RATE |
1 | A | 60 |
2 | B | 70 |
Example 3: Deleting all rows from a table.
DELETE FROM “EMPLOYMENT”; |
Explanation:
The EMPLOYMENT is an already existing table, from which we are deleting all the existing records but not the table structure.
Employment table after removal:
ID | STATE | RATE |