Lesson 11: Deleting Data
Introduction
The DELETE
statement removes rows from a table. To delete safely, always use a WHERE
clause. Otherwise, all rows will be deleted!
Deleting a Single Row
SQL
DELETE FROM customers
WHERE customer_id = 5;
Deleting Multiple Rows
If the WHERE
condition matches more than one row, they will all be removed.
SQL
DELETE FROM customers
WHERE city = 'Chicago';
Deleting All Rows
Omitting the WHERE
clause deletes every row in the table.
SQL
DELETE FROM customers;
DELETE vs TRUNCATE
- DELETE: Removes rows one by one, can use WHERE, logs each deletion.
- TRUNCATE: Removes all rows instantly, cannot use WHERE, minimal logging.
Practice Quiz
Test your knowledge about deleting data: