SQL Delete

Syntax:

                                  
                                    DELETE FROM table_name
WHERE condition;
                                  
                                

  • table_name: The name of the table from which you want to delete rows.
  • WHERE condition: The condition that specifies which rows to delete. If omitted, all rows in the table will be deleted.

Examples:

Assuming we have a table named students with columns student_id, first_name, last_name, and age.

Example 1: Delete All Rows

                                  
                                    DELETE FROM students;
                                  
                                

This deletes all rows from the students table.


Example 2: Delete Rows Based on a Condition

                                  
                                    DELETE FROM students
WHERE age > 25;
                                  
                                

This deletes all students from the students table where the age is greater than 25.

Example 3: Delete Specific Rows

                                
                                    DELETE FROM students
WHERE first_name = 'John' AND last_name = 'Doe';
                                
                              

This deletes the student with the first name 'John' and last name 'Doe' from the students table.

Tips:

  • Always use a WHERE clause to specify which rows to delete, unless you intentionally want to delete all rows from the table.
  • Be cautious with the condition to avoid unintentional data loss.
  • It's a good practice to test your DELETE statements using a SELECT statement with the same condition before executing them.