Menu
CoddyTech

Delete and Return

Part of the SQL Foundations section of Coddy's PostgreSQL journey. Lesson 18 of 22.

DELETE FROM removes rows that match its WHERE condition. RETURNING can show values from the removed rows so you can inspect what was deleted. Without WHERE, DELETE targets every row.

Input table products:

idnamecategorypricestockactivenote
1Maptravel125TrueNULL
2Notebookbooks80TrueRecycled
3Atlasbooks243FalseNULL
4Mughome128TrueBlue
5lamphome302FalseClearance
6Mini maptravel610TrueNULL
DELETE FROM products WHERE id = 5 RETURNING id, name;

The result identifies lamp even though that row has been removed. DELETE removes rows, while DROP TABLE removes a whole table object; they solve different problems.

Use DELETE with a precise WHERE condition. RETURNING reports the removed rows.

challenge icon

Challenge

Medium

Remove product id 3 from this exercise database and return id and name for the removed row.

The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.

Try it yourself

-- Write your SQL here.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in SQL Foundations

Practice on your own: PostgreSQL playground