Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

PostgreSQL DELETE


The DELETE Statement

The DELETE statement is used to delete existing records in a table.

Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement. The WHERE clause specifies which record(s) should be deleted.

If you omit the WHERE clause,
all records in the table will be deleted!.

To delete the record(s) where brand is 'Volvo', use this statement:

Example

Delete all records where brand is 'Volvo':

DELETE FROM cars
WHERE brand = 'Volvo';

Result

DELETE 1

Which means that 1 row was deleted.


Display Table

To check the result we can display the table with this SQL statement:

Example

SELECT * FROM cars;
Run Example »

Delete All Records

It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact.

The following SQL statement deletes all rows in the cars table, without deleting the table:

Example

Delete all records in the cars table:

DELETE FROM cars;

Result

DELETE 3

Which means that all 3 rows were deleted.


Display Table

To check the result we can display the table with this SQL statement:

Example

SELECT * FROM cars;
Run Example »

TRUNCATE TABLE

Because we omit the WHERE clause in the DELETE statement above, all records will be deleted from the cars table.

The same would have been achieved by using the TRUNCATE TABLE statement:

Example

Delete all records in the cars table:

TRUNCATE TABLE cars;

Result

TRUNCATE TABLE

Display Table

To check the result we can display the table with this SQL statement:

Example

SELECT * FROM cars;
Run Example »

PostgreSQL Exercises

Test Yourself With Exercises

Exercise:

Delete all records where the column brand has the value 'Volvo':

 cars
 brand = 'Volvo';
        

Start the Exercise


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.