Lesson 30 of 60 – DELETE
50%

MySQL DELETE

The DELETE statement is used to remove existing records from a MySQL table. You can delete a specific record, multiple records, or all records depending on the condition you use.

Note: Always use a suitable WHERE condition when deleting specific records. Without WHERE, all rows in the table may be deleted.

1. What is DELETE?

The DELETE statement removes existing rows from a table.

DELETE FROM students
WHERE id = 5;

This removes the student whose ID is 5.

2. Basic DELETE Syntax

The basic syntax is:

DELETE FROM table_name
WHERE condition;

The WHERE condition identifies the records that should be deleted.

3. DELETE Using ID

A primary key or unique ID is useful for deleting one specific record.

DELETE FROM students
WHERE id = 10;

Only the record with ID 10 is deleted.

4. DELETE with WHERE

WHERE allows you to select specific rows for deletion.

DELETE FROM students
WHERE course = 'PHP';

This deletes all students whose course is PHP.

5. DELETE Without WHERE

If WHERE is omitted, DELETE removes all rows from the table.

DELETE FROM students;

The table structure remains, but all records are removed.

Warning: DELETE without WHERE can remove every record in the table. Use it only when you intentionally want to remove all rows.

6. DELETE vs DROP TABLE

DELETE DROP TABLE
Removes rows Removes the entire table
Table structure remains Table structure is removed
Can use WHERE Does not use WHERE

7. DELETE vs TRUNCATE

DELETE TRUNCATE
Can remove selected rows Removes all rows
Supports WHERE Does not support WHERE
Table structure remains Table structure remains

8. DELETE with AND

Multiple conditions can be combined using AND.

DELETE FROM students
WHERE course = 'Python'
AND age < 18;

This deletes Python students who are younger than 18.

9. DELETE with OR

OR can be used when either condition should match.

DELETE FROM students
WHERE course = 'PHP'
OR course = 'JavaScript';

This deletes students from either PHP or JavaScript.

10. DELETE with IN

IN is useful when several values need to be deleted.

DELETE FROM students
WHERE course IN ('PHP', 'JavaScript');

This deletes records whose course matches either PHP or JavaScript.

11. DELETE with NOT IN

NOT IN can delete records that do not belong to a specified list.

DELETE FROM students
WHERE course NOT IN ('Python', 'Java');

This deletes students whose course is neither Python nor Java.

Tip: Carefully verify NOT IN conditions before executing DELETE.

12. DELETE with LIKE

LIKE can be used to delete records matching a text pattern.

DELETE FROM students
WHERE name LIKE 'Test%';

This deletes students whose names start with Test.

13. DELETE with BETWEEN

BETWEEN can be used to delete records within a range.

DELETE FROM students
WHERE age BETWEEN 18 AND 20;

This deletes students whose age is between 18 and 20, including the boundary values.

14. DELETE NULL Values

IS NULL can be used to delete records containing missing values.

DELETE FROM students
WHERE email IS NULL;

This deletes students whose email is NULL.

15. DELETE Non-NULL Values

IS NOT NULL can also be used as a DELETE condition.

DELETE FROM students
WHERE email IS NOT NULL;

This deletes all records where email contains a non-NULL value.

Warning: This condition may match many records. Always run SELECT first.

16. DELETE with Comparison Operators

Comparison operators can be used in the WHERE condition.

DELETE FROM students
WHERE fee < 5000;

This deletes students whose fee is less than 5000.

17. DELETE with Multiple Conditions

DELETE FROM students
WHERE course = 'Python'
AND age > 25
AND fee < 10000;

This deletes only records satisfying all three conditions.

18. DELETE with LIMIT

MySQL allows LIMIT with DELETE to restrict the number of rows removed.

DELETE FROM students
WHERE course = 'Test'
LIMIT 5;

This deletes up to five matching records.

Tip: Use LIMIT carefully and verify the matching rows before deletion.

19. DELETE with ORDER BY and LIMIT

MySQL allows ORDER BY and LIMIT together with DELETE.

DELETE FROM students
WHERE course = 'Test'
ORDER BY id ASC
LIMIT 5;

This selects the earliest five matching records according to id and deletes them.

20. DELETE Multiple Rows

A single DELETE statement can remove many rows.

DELETE FROM students
WHERE course = 'Python';

If ten records match the condition, all ten matching records are deleted.

21. DELETE Using a Subquery

DELETE can use a subquery when records need to be selected based on another query.

DELETE FROM students
WHERE course IN (
    SELECT course_name
    FROM courses
    WHERE status = 'Inactive'
);

This concept allows records to be deleted based on information from another table.

22. DELETE Using JOIN

MySQL also supports deleting records based on a related table using JOIN syntax.

DELETE s
FROM students AS s
INNER JOIN courses AS c
ON s.course = c.course_name
WHERE c.status = 'Inactive';

This deletes matching student records whose related course is inactive.

23. Safe DELETE Workflow

A safe DELETE workflow is:

  1. Write the condition.
  2. Run SELECT using the same condition.
  3. Review the records returned.
  4. Run DELETE only after verification.
  5. Run SELECT again to confirm the deletion.
SELECT *
FROM students
WHERE id = 10;

DELETE FROM students
WHERE id = 10;

SELECT *
FROM students
WHERE id = 10;

24. DELETE and Transactions

When using transactional storage engines such as InnoDB, DELETE can be part of a transaction.

START TRANSACTION;

DELETE FROM students
WHERE id = 10;

ROLLBACK;

ROLLBACK can undo the DELETE if the transaction has not been committed.

25. DELETE and COMMIT

If the deletion is correct, the transaction can be committed.

START TRANSACTION;

DELETE FROM students
WHERE id = 10;

COMMIT;

COMMIT permanently applies the transaction according to the transaction rules of the storage engine.

26. Common DELETE Mistakes

  • Forgetting the WHERE clause
  • Using the wrong WHERE condition
  • Deleting more rows than intended
  • Confusing DELETE with DROP TABLE
  • Confusing DELETE with TRUNCATE
  • Not checking records with SELECT first
  • Using NOT IN without considering NULL values
  • Deleting production data without a backup or recovery plan

27. DELETE vs UPDATE

DELETE UPDATE
Removes records Modifies records
Uses DELETE FROM Uses UPDATE ... SET
WHERE selects rows to remove WHERE selects rows to modify
-- UPDATE
UPDATE students
SET fee = 20000
WHERE id = 1;

-- DELETE
DELETE FROM students
WHERE id = 1;

28. Practical Student Record Deletion

Suppose a student with ID 15 has left the institute.

SELECT *
FROM students
WHERE id = 15;

DELETE FROM students
WHERE id = 15;

SELECT *
FROM students
WHERE id = 15;

The first SELECT verifies the record, DELETE removes it, and the final SELECT checks whether it still exists.

29. Deleting Test Records

During development, test records can be removed using a suitable condition.

SELECT *
FROM students
WHERE name LIKE 'Test%';

DELETE FROM students
WHERE name LIKE 'Test%';

Using SELECT first helps verify exactly which records will be deleted.

30. Complete DELETE Example

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    course VARCHAR(100),
    age INT,
    email VARCHAR(150),
    fee DECIMAL(10,2)
);

INSERT INTO students
(name, course, age, email, fee)
VALUES
('Amit', 'Python', 22, 'amit@gmail.com', 15000.00),
('Priya', 'Java', 21, 'priya@gmail.com', 18000.00),
('Rahul', 'PHP', 24, 'rahul@gmail.com', 12000.00),
('Neha', 'Python', 20, NULL, 15000.00),
('Test User', 'Test', 18, 'test@example.com', 5000.00);

SELECT *
FROM students
WHERE course = 'Test';

DELETE FROM students
WHERE course = 'Test';

SELECT *
FROM students;

The first SELECT checks the test record. DELETE removes the matching record, and the final SELECT displays the remaining students.

📌 Key Points

  • DELETE is used to remove existing records from a table.
  • The WHERE clause identifies which rows should be deleted.
  • Without WHERE, all rows may be deleted.
  • DELETE does not remove the table structure.
  • DELETE can be combined with AND, OR, IN, LIKE, BETWEEN, and IS NULL.
  • MySQL supports LIMIT with DELETE.
  • ORDER BY can be used with DELETE and LIMIT in MySQL.
  • DELETE is different from DROP TABLE and TRUNCATE.
  • Transactions can provide a way to ROLLBACK a DELETE before COMMIT when supported by the storage engine.
  • Always run SELECT with the same WHERE condition before an important DELETE operation.

🧠 Quick Quiz

Question: Which clause is used to specify which records should be removed by a DELETE statement?