SQL
Example Table Creation and Data Deletion
1. Create the employees table:

CREATE TABLE employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    age INT,
    department VARCHAR(50)
);

2. Insert sample data into the employees table:

INSERT INTO employees (first_name, last_name, age, department)
VALUES 
('John', 'Doe', 30, 'Engineering'),
('Jane', 'Smith', 28, 'Marketing'),
('Alice', 'Johnson', 35, 'HR'),
('Bob', 'Brown', 40, 'Finance');

3. Delete data from the employees table:

Delete John Doe's record:


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

Delete all employees older than 60 years:


DELETE FROM employees
WHERE age > 60;

Delete all records:


DELETE FROM employees;

Precautions
  • Always ensure your WHERE clause is accurate to avoid accidentally deleting unintended data.
  • It’s good practice to perform a SELECT statement with the same WHERE clause before performing the DELETE to verify which rows will be deleted.
  • Consider the use of transactions when performing deletions to allow for rollback in case of errors.

This covers the basics of the DELETE statement in SQL. By following these examples, you can efficiently delete data from your database tables.