In this tutorial we will learn about the use of DELETE
statement.
So, let's get started...
Use of DELETE
statement
DELETE
statement is used to delete one or more rows from a table.
Syntax
DELETE FROM table_name
WHERE condition;
In the above syntax first we need to specify the table from which we want to delete the rows.
Second we have to specify the condition in the WHERE
clause to delete the rows.
Note one thing here, 'WHERE' clause is optional, if we omit the 'WHERE' clause, it will delete all the rows in the table.
Example
Let's look at the example and practice it.
Suppose we have the following table which shows all the links to websites:
1) DELETE one row from table
The following query will delete one row from the table.
DELETE FROM links
WHERE id = 8;
The above query deletes the row which has id
=8.
2) Delete a row and return the deleted row
In this example we will see how to return a deleted row:
DELETE FROM links
WHERE id = 7
RETURNING *;
In the above syntax row which has id
=7 is deleted. The RETURNING
statement at the end returns the deleted row.
Output is as:
Conclusion
In this tutorial we explored about the 'DELETE' statement in PostgreSQL.
Top comments (0)