The delete statement in SQL is used to remove existing records from a table, you can delete specific row based on a condition.
DELETE FROM table_name
WHERE condition;
table_name
– The name of table where you want to delete data.WHERE condition
– Specifies which rows to delete. If omitted, all rows will be deleted.To delete specific records, you need to define a condition in where clause.
DELETE FROM employees
WHERE employee_id = 101;
This will delete the employee with employee_id = 101
.
You can delete multiple rows by specifying a condition that matches multiple records.
DELETE FROM employees
WHERE department = 'Sales';
This will delete all employees in “Sales” department.
To delete all records from table we can use eighter DELETE
or TRUNCATE
. Both will remove the records from table, but as DELETE
removes records from table one by one it is slow, and TRUNCATE
is faster for this operation.
DELETE FROM employees;
TRUNCATE TABLE employees;
DELETE
and TRUNCATE
Feature | DELETE | TRUNCATE |
---|---|---|
Can use WHERE? | ✅ Yes | ❌ No |
Faster for large tables? | ❌ No | ✅ Yes |
Generates transaction logs? | ✅ Yes | ❌ No |
Can be rolled back? | ✅ Yes (if within a transaction) | ❌ No (usually irreversible) |
DELETE
with JOINYou can also delete rows based on conditions from another table using JOIN
.
DELETE o
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE c.status = 'Inactive';
This deletes the orders for customers who are inactive.
DELETE
with RETURNING
clauseSome databases allow to use RETURN
clause, which will return the deleted rows.
DELETE FROM employees
WHERE department = 'HR'
RRETURINING *;
This will delete the employees in HR department and return the deleted rows.
DELETE
with LIMIT
We can also define to delete only a limited number of records
DELETE FORM employees
WHERE department = 'Sales'
LIMIT 5;
This will delete only 5 employees from Sales department.
DELETE
inside a Transaction (ROLLBACK SUPPORT)START TRANSACTION;
DELETE FROM employees
WHERE department = 'IT';
ROLLBACK;
COMMIT;
✅ Always use a WHERE clause (unless deleting all rows intentionally).
✅ Use transactions for safety when deleting important data.
✅ Check affected rows using SELECT before deleting.
✅ Use TRUNCATE instead of DELETE for performance when removing all rows.
✅ Backup your data before running DELETE on critical tables.
Thank you for reading! We hope this article has been helpful in understanding about DELETE in SQL. If you found it valuable, please share it with your network.
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Python Practice Questions & Solutions Day 5 of Learning Python for Data Science Welcome back…
Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing Understanding…
Python Practice Questions & Solutions Day 4 of Learning Python for Data Science Welcome back…
Day 4 of Learning Python for Data Science Day 4 of Learning Python for Data…
Test your Python skills with these 20 practice questions and solutions from Day 3 of…
Understanding Python’s conditional statements is essential for controlling the flow of a program. Today, we…