CodingNic

CRUD Operations

DELETE

CRUD Operations 8 min read

DELETE

Objectives

By the end of this lesson, you should be able to:

  • Remove rows with DELETE
  • Explain why WHERE matters for DELETE, the same way it does for UPDATE
  • Explain the difference between DELETE and TRUNCATE TABLE

💡 Why this matters: Sometimes a row genuinely needs to be gone, an employee who left, a cancelled order. DELETE removes rows permanently, which makes it exactly as important to target correctly as UPDATE.

⚠️ A note on verification: every statement and result in this lesson was run against a real, live PostgreSQL 18 database, using the employees table from Module 3, filtered with WHERE (covered fully in Module 6).

The DELETE Statement

sql
DELETE FROM employees WHERE first_name = 'Jordan';
text
 first_name
------------
 Erin
 Maya
 Priya

Jordan’s row is gone entirely, not marked inactive, not hidden, permanently removed. WHERE narrows down exactly which rows are deleted, using the same conditions covered fully in Module 6.

Why WHERE Matters Here Too

Just like UPDATE, leaving off WHERE affects every row:

sql
DELETE FROM employees;

This empties the entire table, one row at a time, permanently. Always double-check WHERE before running a DELETE, exactly the same discipline as UPDATE.

DELETE vs. TRUNCATE TABLE

Both remove rows, but they’re not the same tool. DELETE FROM employees; (no WHERE) and TRUNCATE TABLE employees; (from Module 3) both end with an empty table, but DELETE processes and removes rows one at a time and can be limited with WHERE, while TRUNCATE removes everything at once and can’t be filtered at all. For removing a subset of rows, DELETE ... WHERE is the only option, TRUNCATE is all-or-nothing.

Try It

  1. Delete one specific employee by first_name, and confirm they’re gone with a SELECT.
  2. Explain, in your own words, the difference between DELETE FROM table; (no WHERE) and TRUNCATE TABLE table;.
  3. Explain why you can’t use TRUNCATE TABLE to remove just the employees in one specific department.

Recap

  • DELETE FROM table WHERE condition; permanently removes matching rows.
  • Leaving off WHERE deletes every row, the same risk as an UPDATE with no WHERE.
  • DELETE can target a subset of rows with WHERE, TRUNCATE TABLE (Module 3) always removes everything at once.

Next lesson: the RETURNING clause, seeing exactly which rows an INSERT, UPDATE, or DELETE just affected.