Deleting Data
The final piece of Data Manipulation Language (DML) is the ability to remove data entirely. When a user deletes their account, or a product is permanently discontinued, we use the DELETE statement.
The Syntax
The DELETE syntax is actually the simplest of the DML commands. You only need to specify the table and the condition for removal.
DELETE FROM users
WHERE id = 101;
This will find the user with the Primary Key of 101 and completely erase their row from the table.
Deleting Multiple Rows
Just like UPDATE and SELECT, the WHERE clause in a DELETE statement can match multiple rows.
If we want to delete all users from a specific country, we can do:
DELETE FROM users
WHERE country = 'Antarctica';
Every row that matches the condition will be removed in a single operation.
The Danger of DELETE without WHERE
Just like the UPDATE statement, the DELETE statement is incredibly dangerous if used carelessly.
DELETE FROM users;
Because there is no WHERE clause, this query will instantly delete every single row in the users table. The table structure will remain, but it will be completely empty. There is no “Undo” button in standard SQL. If you haven’t backed up your database, that data is gone forever.
[!CAUTION] Always double-check your
WHEREclause before running aDELETEstatement. A common best practice is to write aSELECT * FROM table WHERE ...query first to preview exactly which rows you are about to target. Once you verify the list is correct, you changeSELECT *toDELETE.
Try using DELETE safely in the interactive lab to clean up our inactive user accounts!