Home Tutorials SQL Tutorial Deleting Data
Deleting Data

Deleting Data


Deleting Data

Definition: The DELETE statement is used to remove existing records from a table. While UPDATE modifies data, DELETE permanently erases the entire row from the database.

Why: Beginner SQL tutorials include deletion shortly after learning to insert and update data. This completes the "CRUD" cycle (Create, Read, Update, Delete), which are the four essential functions of any persistent storage system.


Syntax

To remove data, you use the DELETE FROM keywords followed by the table name and a WHERE clause to identify which record(s) should be removed.

DELETE FROM table_name
WHERE condition;

Example: Removing a Specific Record

If a student with ID #3 leaves the institution, you would remove their entire record from the students table using this query:

DELETE FROM students
WHERE id = 3;

Explanation

  • DELETE FROM students: This targets the specific table you want to clean up.
  • WHERE id = 3: This is the most important safeguard. It tells the database to only delete the row where the ID matches 3.
  • Permanent Action: Unlike a computer's "Recycle Bin," a SQL DELETE is typically permanent. Once executed, the data cannot be recovered easily.

Key Notes

  • The "Nuclear" Option: If you omit the WHERE clause (e.g., DELETE FROM students;), the database will delete every single record in the table, leaving you with an empty table structure.
  • DELETE vs. DROP: Use DELETE to remove data (rows) but keep the table itself. Use DROP TABLE only if you want to delete the entire table structure and everything inside it.
  • Safety Tip: Professional developers often run a SELECT query with the same WHERE clause first (e.g., SELECT * FROM students WHERE id = 3;) to confirm exactly which row will be deleted before they commit to the action.

🏋️ Test Yourself With Exercises

Take our quiz on Deleting Data to test your knowledge.

Browse Quizzes »