Deleting rows with DELETE #
The DELETE statement removes rows from a table:
DELETE FROM table_name
WHERE
condition;
table_name: The table to delete rows from.WHERE condition: Chooses which rows get deleted.
Just like UPDATE, leaving out WHERE means DELETE removes every single row in the table, not just some of them. Always double check your WHERE clause before running a DELETE.
Exercise #
We have already created a table orders with the columns id, customer_id, order_date, status, and shipped_date. Write a command that deletes every order whose status is Cancelled, without deleting any other order.
Tests #
- No row in
ordershasstatusequal toCancelledanymore. - Every order that was not
Cancelledis still in the table.
Solution
DELETE FROM orders
WHERE
status = 'Cancelled';
Basic Data Retrieval
Filtering Data
Aggregating Data
Joining Tables
Creating and Managing Tables
Transactions and Data Integrity