PostgreSQL DELETE Query

PostgreSQL DELETE Query is used to delete one or more rows of a table. In DELETE query, you can also use clauses like WHERE, LIKE, IN, NOT IN, etc., to select the rows for which the DELETE operation will be performed.

The syntax of DELETE query is;

</>
Copy
 DELETE FROM table_name
	WHERE condition;

The use of WHERE clause is optional.

If you do not provide any selection criteria for the rows, then all the rows of the table would be deleted.

PostgreSQL DELETE One Row

We will start this example, where we will delete only one row. For this, we need to prepare WHERE clause which selects only one row.

Consider the following students table.

PostgreSQL DELETE query example

The following DELETE query will delete the row where id=3.

</>
Copy
 DELETE FROM students
	WHERE id=3;
PostgreSQL DELETE Query

Now, we will check the rows of the table.

PostgreSQL DELETE all rows of Table

Now we will delete all the rows of students table. Following would be the query to delete all rows of table. You have to exercise caution while running this query, because once all rows are deleted, they would be deleted permanently.

</>
Copy
 DELETE FROM students;
PostgreSQL DELETE

Let us see the contents of the table.

PostgreSQL DELETE rows

All the rows have been deleted.

Conclusion

In this PostgreSQL Tutorial, we deleted one or multiple or all rows of a table using PostgreSQL DELETE query.