đī¸ The DELETE statement in SQL is used to remove one or more rows from a database table. It is one of the core Data Manipulation Language (DML) statements and allows you to delete specific records using conditions or remove every record from a table while keeping the table structure intact.
đ What is the DELETE Statement?
The DELETE statement removes existing rows from a table. You typically use a WHERE clause to specify which rows should be deleted. If the WHERE clause is omitted, every row in the table is deleted.
Information
đ¯ Why Use DELETE?
The DELETE statement is commonly used to remove outdated, incorrect, or unnecessary records from a database.
- đ Remove obsolete records.
- đ Delete incorrect or duplicate data.
- đ Clean up test or temporary data.
- đ Archive data before permanent removal.
- đ Maintain accurate and organized databases.
đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Age | Marks |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 20 | 92 |
| 102 | Bob | Mathematics | 21 | 85 |
| 103 | Charlie | Physics | 22 | 78 |
| 104 | David | Computer Science | 20 | 95 |
đ Basic Syntax
DELETE Syntax
DELETE FROM table_name
WHERE condition;đĄ Delete a Single Row
Delete the student whose StudentID is 103.
Delete One Record
DELETE FROM Students
WHERE StudentID = 103;Result:
| StudentID | Name | Department |
|---|---|---|
| 101 | Alice | Computer Science |
| 102 | Bob | Mathematics |
| 104 | David | Computer Science |
đ Delete Multiple Rows
Use a condition that matches multiple rows to delete all of them.
Delete Multiple Records
DELETE FROM Students
WHERE Department = 'Computer Science';đ Delete All Rows
Omitting the WHERE clause deletes every row from the table.
Delete All Records
DELETE FROM Students;Warning
đ DELETE with Subquery
A subquery can be used to determine which rows should be deleted.
Delete Using a Subquery
DELETE FROM Students
WHERE Department IN
(
SELECT Department
FROM Departments
WHERE Status = 'Inactive'
);đ DELETE with EXISTS
The EXISTS operator can also be used to delete rows based on the existence of related records.
Delete Using EXISTS
DELETE FROM Students s
WHERE EXISTS
(
SELECT 1
FROM GraduatedStudents g
WHERE g.StudentID = s.StudentID
);âī¸ DELETE vs TRUNCATE vs DROP
| Feature | DELETE | TRUNCATE | DROP |
|---|---|---|---|
| Removes Rows | â Yes | â Yes | â Yes |
| Supports WHERE | â Yes | â No | â No |
| Removes Table Structure | â No | â No | â Yes |
| Typical Use | Delete selected rows. | Remove all rows quickly. | Remove the entire table. |
đŧ Real-World Example
A university wants to remove records of students who have graduated and whose data has already been archived.
Delete Archived Students
DELETE FROM Students
WHERE StudentID IN
(
SELECT StudentID
FROM GraduatedStudents
);This query deletes only students whose records already exist in the GraduatedStudents table.
â ī¸ Common Mistakes
- â Forgetting the WHERE clause and deleting every row.
- â Using an incorrect filter condition.
- â Deleting records without first creating a backup.
- â Ignoring foreign key constraints that may prevent deletion.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ DELETE removes existing rows from a table.
- đ The WHERE clause controls which rows are deleted.
- đ Omitting WHERE deletes every row in the table.
- đ The table structure remains unchanged after deletion.
- đ Subqueries and EXISTS can be used for advanced deletion conditions.
- đ DELETE, TRUNCATE, and DROP serve different purposes.