Emptying a table
- SQL course
- Beginner
- Lesson 86/115
- 4 min
Safely remove all rows.
DELETE with JOIN
In some databases you can use DELETE with JOIN to remove rows from related tables.
DELETE p FROM products p
JOIN order_items oi ON p.id = oi.product_id
WHERE p.stock = 0;This query removes out-of-stock products that were ordered.
Rows are deleted from products. Only the rows where p.stock = 0 holds are affected.
DELETE with JOIN — details
MySQL supports this syntax. PostgreSQL uses a USING clause. The syntax varies by database.
Practice DELETE with JOIN
Delete the row with id=3 from products.
Starter code
-- delete row
DELETE FROM products
WHERESample solution
DELETE FROM products
WHERE id = 3Expected output
1 satır silindi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Delete with join
DELETE p FROM products p
____ order_items oi ON p.id = oi.product_id;
DELETE p FROM products p
JOIN order_items oi ON p.id = oi.product_id;
Explanation: Using JOIN with DELETE picks the rows to delete by their match in another table.
2 Which database uses USING for DELETE JOIN?
- PostgreSQL Correct answer
- MySQL
- SQL Server
- SQLite
Explanation: PostgreSQL uses the USING clause.
3 Emptying a table: match the terms
| Term | Meaning |
|---|---|
DELETE |
delete |
JOIN |
join |
WHERE |
condition |
FROM |
table |