PostgreSQL
PostgreSQL DELETE
Safely remove selected records from a PostgreSQL table with the DELETE statement.
The DELETE statement
The DELETE statement removes existing records from a table. Use the
WHERE clause to specify which record or records should be deleted.
The condition is evaluated for every row, and matching rows are removed.
Delete matching records
To delete all records where name is Laptop, use:
DELETE FROM products
WHERE name = 'Laptop';
This removes every product whose name is Laptop. Before executing
the delete, verify the matching records with:
SELECT * FROM products
WHERE name = 'Laptop';
Result
After the delete runs, PostgreSQL reports:
DELETE 1
This means that one row was deleted. If several rows match the condition, the number will reflect the total rows removed.
Display the table
To check the remaining records, display the table with:
SELECT * FROM products;
WHERE clause, PostgreSQL deletes every record in the table:
DELETE FROM products;
This removes all rows but leaves the products table itself in place.
Further Reading
Continue learning with these related tutorials:
- PostgreSQL Introduction
- PostgreSQL pgAdmin 4
- PostgreSQL Create Table
- PostgreSQL Insert Data
- PostgreSQL Select Data
- PostgreSQL ADD COLUMN
- PostgreSQL UPDATE
- PostgreSQL ALTER COLUMN
- PostgreSQL DROP COLUMN
- PostgreSQL DELETE
- PostgreSQL DROP TABLE
- PostgreSQL Operators
- PostgreSQL SELECT
- PostgreSQL SELECT DISTINCT
- PostgreSQL WHERE
- PostgreSQL ORDER BY
- PostgreSQL LIMIT
- PostgreSQL MIN and MAX
- PostgreSQL COUNT
- PostgreSQL SUM
- PostgreSQL AVG
- PostgreSQL LIKE
- PostgreSQL IN
- PostgreSQL BETWEEN
- PostgreSQL AS
- PostgreSQL Joins
- PostgreSQL INNER JOIN
- PostgreSQL LEFT JOIN
- PostgreSQL RIGHT JOIN
- PostgreSQL FULL JOIN
- PostgreSQL CROSS JOIN
- PostgreSQL UNION
- PostgreSQL GROUP BY
- PostgreSQL HAVING
- PostgreSQL EXISTS
- PostgreSQL ANY
- PostgreSQL ALL
- PostgreSQL CASE
Continue learning
