MySQL DELETE Statement

MySQL Tutorial

MySQL DELETE Statement

The DELETE statement in MySQL is used to remove existing records from a table. It is commonly use with the WHERE clause to delete specific rows.


 Syntax

  • table_name: The name of the table from which you want to delete records.

  • WHERE: Specifies which record(s) should be deleted.


 Important Warning

If you omit the WHERE clause, all records in the table will be deleted, but the table structure remains intact.

Example:

 Deletes all rows in the customers table.



 Example Table: students

idnameagecity
1John20New York
2Emma21Chicago
3Raj22Mumbai

 Example 1: Delete a Single Record

 Deletes the record where student id = 1.


 Example 2: Delete Multiple Records Based on Condition

 Deletes all students whose age is greater than 21.


 Example 3: Delete Records with NULL Values

 Removes all rows where the city value is NULL.


 Reset Auto Increment (Optional)

After many deletes, you may want to reset the auto‐increment counter:


DELETE vs TRUNCATE

FeatureDELETETRUNCATE
Deletes specific rows Yes No
Can use WHEREYes No
Deletes all rows Yes Yes
Faster performance NoYes
Resets auto-increment No Yes

Example of TRUNCATE:


 Summary

  • Use DELETE to remove data from a table.

  • Always use a WHERE clause unless you want to delete all records.

  • DELETE does not remove the table structure.

You may also like...