SQL DELETE
The DELETE command in SQL is used to remove existing records (rows) from a database table.
Whenever data is no longer required such as removing inactive users, outdated records, or incorrect entries the DELETE command is used.
Syntax
Deleting a Specific Record
Result
Only the record with id = 3 is removed.
Deleting Multiple Records Using a Condition
This removes all students whose marks are less than 90.
Deleting All Records from a Table
Difference Between DELETE and TRUNCATE
Important: The DELETE command permanently removes data from table.
Understanding DELETE
A database table can be compared to a register or spreadsheet.
- INSERT - adds new rows
- UPDATE - modifies existing rows
- DELETE - removes rows completely
Using DELETE is similar to crossing out an entire row from a register.
Example: Students Table
Example: Students Table
| id | name | marks |
|---|---|---|
| 1 | Rahul | 92 |
| 2 | Anjali | 90 |
| 3 | Amit | 88 |
Why Use the DELETE Command?
The DELETE command is used to
- Remove unnecessary or outdated records
- Delete incorrect data
- Maintain clean and accurate tables
- Remove records based on specific conditions
Syntax
DELETE FROM table_nameWHERE condition;- DELETE FROM specifies the table
- WHERE specifies which rows should be removed
Deleting a Specific Record
DELETE FROM studentsWHERE id = 3;Result
Only the record with id = 3 is removed.
| id | name | marks |
|---|---|---|
| 1 | Rahul | 92 |
| 2 | Anjali | 90 |
Deleting Multiple Records Using a Condition
DELETE FROM studentsWHERE marks < 90;This removes all students whose marks are less than 90.
Deleting All Records from a Table
DELETE FROM students;This deletes all rows from the table but keeps the table structure intact.
Difference Between DELETE and TRUNCATE
| DELETE | TRUNCATE |
|---|---|
| Deletes selected rows | Deletes all rows |
| Supports WHERE clause | Does not support WHERE |
| Can be rolled back (in transactions) | Cannot be rolled back |
| Slower than TRUNCATE | Faster |
Important Rules for Best Practices
- Always use WHERE unless you intend to delete all records
- Verify data using SELECT before deleting
- DELETE operations are permanent
- Use transactions if rollback support is required
Two Minute Dril
- DELETE removes rows from a table
- DELETE FROM specifies the table
- WHERE controls which records are deleted
- Without WHERE, all rows are removed
- DELETE keeps the table structure intact
- DELETE operations are permanent