Skip to Content
Course content

3.3 Deleting Data: DELETE

The DELETE statement in SQL is used to remove rows from a table. It is a crucial operation for maintaining clean and accurate data, allowing you to eliminate unwanted or outdated records.

3.3.1 Overview of DELETE

  • Purpose: To delete one or more rows from a table based on specified conditions.
  • Syntax:
    DELETE FROM table_name
    WHERE condition;
    
    • table_name: The table from which the data will be deleted.
    • WHERE: The condition that determines which rows should be deleted. If omitted, all rows in the table will be deleted, so it's important to use it carefully.

3.3.2 Key Points

  1. Deleting Specific Rows:
    Always use the WHERE clause to specify which rows you want to delete. Without the WHERE clause, the entire table will be deleted.
    • Example: Delete an employee with a specific ID:
      DELETE FROM employees
      WHERE employee_id = 101;
      
  2. Deleting Multiple Rows:
    You can delete multiple rows at once based on a condition.
    • Example: Delete all employees from the "Sales" department:
      DELETE FROM employees
      WHERE department = 'Sales';
      
  3. Deleting All Rows:
    If you omit the WHERE clause, all rows in the table will be deleted. Use this cautiously.
    • Example: Delete all employees:
      DELETE FROM employees;
      
    • Note: Deleting all rows does not remove the table structure; the table remains, but empty.

3.3.3 Conditional Deletion

You can apply various conditions to delete specific data:

  1. Based on Comparison:
    • Example: Delete all employees whose salary is less than 30,000:
      DELETE FROM employees
      WHERE salary < 30000;
      
  2. Using Logical Operators:
    • Example: Delete employees who are either in the "HR" or "Sales" departments:
      DELETE FROM employees
      WHERE department IN ('HR', 'Sales');
      
  3. Using Date Conditions:
    • Example: Delete records that are older than a specific date:
      DELETE FROM transactions
      WHERE transaction_date < '2023-01-01';
      
  4. Using Subqueries:
    Subqueries can be used in the WHERE clause to delete records based on values from other tables.
    • Example: Delete orders that have no related customer:
      DELETE FROM orders
      WHERE customer_id NOT IN (SELECT customer_id FROM customers);
      

3.3.4 Best Practices

  1. Use the WHERE Clause: Always ensure that you have a WHERE clause to specify which rows to delete. Deleting all rows without a WHERE clause is irreversible unless you have a backup.
  2. Test with a SELECT Statement: Before performing a DELETE, run a SELECT query with the same condition to ensure you are deleting the correct records.
    • Example:
      SELECT * FROM employees WHERE department = 'Sales';
      
  3. Backup Data: Before executing a large or irreversible delete operation, consider creating a backup of the data, especially if it involves critical or large tables.
  4. Use Transactions: For critical operations, wrap the DELETE statement in a transaction so that you can roll back the operation if needed.
    • Example:
      BEGIN TRANSACTION;
      DELETE FROM employees WHERE status = 'Inactive';
      -- If something goes wrong
      ROLLBACK;
      -- Otherwise, commit the changes
      COMMIT;
      

3.3.5 Real-World Examples

  1. Deleting Outdated Records:
    Delete customers who have not placed an order in the last year:
    DELETE FROM customers
    WHERE last_order_date < '2023-01-01';
    
  2. Deleting Based on Multiple Conditions:
    Delete all employees in the "Sales" department who have a salary lower than 40,000:
    DELETE FROM employees
    WHERE department = 'Sales' AND salary < 40000;
    
  3. Removing a Single Record:
    Delete an employee by their ID:
    DELETE FROM employees
    WHERE employee_id = 123;
    
  4. Deleting Invalid Data:
    Delete orders that were made in the future (assuming the system mistakenly recorded future dates):
    DELETE FROM orders
    WHERE order_date > CURRENT_DATE;
    

3.3.6 Risks of Improper Use

  1. Accidental Deletion:
    Omitting the WHERE clause can result in the deletion of all rows in the table. Always double-check your query before executing it.
    • Example:
      DELETE FROM employees; -- This deletes all rows!
      
  2. Data Loss:
    Once data is deleted, it cannot be easily recovered unless there is a backup. Always make sure to back up important data before deletion.

Conclusion

The DELETE statement in SQL is a powerful tool for removing data from a database. By using it carefully with proper conditions and good practices, you can maintain a clean and efficient database. Always ensure that deletions are deliberate and reversible, especially when working with critical data.

Commenting is not enabled on this course.