RienSQL
LESSON 36 / 62 · Deleting data

Deleting data, DELETE operator

How to delete records from an SQL table with DELETE: syntax with WHERE, deleting based on another table with a subquery and JOIN, differences between DELETE and TRUNCATE. Examples for MySQL and PostgreSQL.

When data is no longer needed, it gets deleted: a product is removed from the list, a cancelled reservation is erased. Rows are deleted with the DELETE operator, and a whole table can also be cleared with TRUNCATE. Note that emptying a table with TRUNCATE cannot always be undone.

General query structure with the DELETE operator

DELETE FROM table_name
[WHERE condition];
  • [ ]optional part

Which rows to delete. Without WHERE, every row of the table is deleted.

table_name
The table from which records are deleted.
condition
Optional

For example, there is not a single payment for the vacuum cleaner in the Payments table, so it can safely be removed from the Goods list:

member_idINTgood_idINTgood_type_idINTpayment_idINT

DELETE FROM Goods
WHERE good_name = 'vacuum cleaner';
good_idgood_nametype
1010 more rowspineapples3
11television8
12vacuum cleaner8
13jacket7
145 more rowsfur coat7

The WHERE condition is written the same way as in a SELECT query: you can compare values, combine conditions with AND and OR, and use IN, LIKE, and subqueries.

Be careful when deleting data. If you skip the WHERE operator, all entries in the table will be deleted. Before running DELETE, it helps to run a SELECT with the same WHERE condition and make sure it returns exactly the rows you are going to delete.

Try it yourself: choose a condition, see which rows would be deleted, and then run the query.

FieldValueWHEREtype=2ANDOR–––MySQL 8.4

DELETE FROM Goods
WHERE type = 2;
good_idgood_nametype
3bread2
4milk2
5red caviar3
6cinema4
9potato2
10pineapples3
13jacket7
14fur coat7

A query without a condition empties the whole table, while the table itself remains:

DELETE FROM Payments;

You can also delete all records of a table using the TRUNCATE operator. It does not go through the rows one by one like DELETE, but empties the whole table at once, so it is much faster, especially on large tables.

General structure of a query with the TRUNCATE operator

TRUNCATE TABLE table_name;
table_name
The table to clear completely.

At the same time, TRUNCATE behaves differently from DELETE:

  • You cannot specify a WHERE condition — the whole table is emptied
  • Delete triggers do not fire
  • The action cannot be undone: TRUNCATE immediately commits the current transaction, and ROLLBACK does not bring the rows back
  • Resets the AUTO_INCREMENT counter to the initial value
  • Does not work if the table is referenced by foreign keys of other tables
  • To use it, you need the DROP privilege on the table

To learn more about how DELETE differs from TRUNCATE and DROP, read the article TRUNCATE vs DELETE vs DROP in SQL: The Difference and What to Choose.

Deleting based on data from another table

Sometimes the rows to delete have to be selected using data from another table. Suppose the service no longer works with homes that have no kitchen, and the reservations for such homes have to be deleted. Information about the kitchen is stored in the Rooms table, and reservations are stored in the Reservations table.

idINTidINTuser_idINTroom_idINTidINTowner_idINTidINTreservation_idINT

DELETE FROM Reservations
WHERE room_id IN (
    SELECT id
    FROM Rooms
    WHERE has_kitchen = false
);

You can do the same with JOIN. In this case, list the tables you delete records from right after DELETE.

MySQL 8.4

DELETE table_name_1 [, table_name_2] FROM
table_name_1 JOIN table_name_2
ON join_condition
[WHERE condition];
  • [ ]optional part

The table from which records are deleted. Its name goes both after DELETE and in the JOIN.

The joined table whose data helps select the records to delete. If you also list it after DELETE, records are deleted from both tables.

How the rows are linked, for example table_name_1.field = table_name_2.field.

Which records to delete. The condition can check columns of both tables.

condition
Optional

For our task, the query looks like this:

DELETE Reservations FROM
Reservations JOIN Rooms ON
Reservations.room_id = Rooms.id
WHERE Rooms.has_kitchen = false;

Now let's delete the homes themselves along with the reservations.

DELETE Reservations, Rooms FROM
Reservations JOIN Rooms ON
Reservations.room_id = Rooms.id
WHERE Rooms.has_kitchen = false;

This query deletes only the homes that had reservations: Rooms rows without a match in Reservations do not get into the join.

A subquery on the table you delete from

A subquery cannot read the same table that DELETE removes rows from. For example, you cannot delete reservations cheaper than the average price this way:

DELETE FROM Reservations
WHERE price < (SELECT AVG(price) FROM Reservations);

The query fails with an error:

ERROR 1093 (HY000): You can't specify target table 'Reservations' for update in FROM clause

You can work around this by wrapping the subquery in one more SELECT. The average price is then first calculated and stored in a temporary table, and the deletion reads from it:

DELETE FROM Reservations
WHERE price < (
    SELECT avg_price
    FROM (SELECT AVG(price) AS avg_price FROM Reservations) AS stats
);

Test yourself

You need to delete only the reservations cheaper than 100 from the Reservations table. Which query does this?

TRUNCATE TABLE Reservations WHERE price < 100;DELETE FROM Reservations WHERE price < 100;DELETE FROM Reservations;CheckArticle contentGeneral query structure with the DELETE operatorGeneral structure of a query with the TRUNCATE operatorDeleting based on data from another tableTest yourselfEdit on GitHub

Python Academy

Practice

Solve it in the editor. The query runs against a sample database in your browser.