SQL DELETE

The DELETE statement in SQL is used to remove specific rows from a table based on a condition. Unlike the DROP TABLE statement, it does not delete the table structure, allowing for further data insertion.

Syntax


DELETE FROM table_name WHERE condition;
            
- table_name: The name of the table from which rows are to be deleted.
- condition: Specifies which rows should be deleted. If omitted, all rows will be deleted.

Example

Let us delete employees from the Employees table where the department is HR:

Code Example


-- Delete rows where Department is HR
DELETE FROM Employees WHERE Department = 'HR';

-- Display the Remaining Table Content
SELECT * FROM Employees;
                

Output

-- Before DELETE:
ID Name Age Department
1 Alice 30 HR
2 Bob 25 IT
3 Charlie 35 Finance
-- After DELETE:
ID Name Age Department
2 Bob 25 IT
3 Charlie 35 Finance

Explanation

- The DELETE statement removes rows where the department is HR.
- The SELECT * statement verifies the remaining data in the table.

Best Practices

- Always specify a WHERE condition to avoid deleting all rows unintentionally.
- Use transactions (e.g., BEGIN and ROLLBACK) to ensure data safety during testing.