Lesson 13 +10 XP

DELETE Statement

The DELETE statement removes rows from a table.

Basic syntax


sql

### DELETE FROM Customers

### WHERE CustomerName = 'Cardinal';

Deletes only the rows that match the WHERE condition.

BE CAREFUL: DELETE without WHERE


sql

### DELETE FROM Customers;

Deletes every row in the table. The table structure stays, but it is empty.

Delete all rows (two ways)


sql

### DELETE FROM Customers;

or


sql

### TRUNCATE TABLE Customers;
  • DELETE removes rows one by one.
  • TRUNCATE removes all rows in one go and is faster.

DELETE does NOT drop the table

After DELETE, the table still exists - only its rows are gone.

The safe pattern


sql
SELECT * FROM Customers WHERE CustomerName = 'Cardinal'; - check first
DELETE FROM Customers WHERE CustomerName = 'Cardinal';

TL;DR

  • DELETE removes rows.
  • WHERE decides which rows to remove.
  • Without WHERE, all rows are deleted.
  • TRUNCATE TABLE empties a table faster.