How to delete a record in SQL

How to Delete a Record in SQL

Deleting records in a relational database is a fundamental operation in data management. Whether you’re cleaning up outdated entries or correcting data errors, understanding how to safely and efficiently delete records is essential. This article walks through the process of deleting records in SQL, with code examples using standard SQL syntax.

Learn SQL online

For our instructor lead, more in depth look at learning to write a SQL SELECT, SORT BY and WHERE clauses why not check out our video course hosted on UDEMY or enjoy the free SQL course below.

Understanding the DELETE Statement

In SQL, the DELETE statement is used to remove one or more rows from a table. The basic syntax is:

				
					DELETE FROM table_name
WHERE condition;
				
			
  • table_name: The name of the table from which you want to delete data.

  • condition: A filter that specifies which records to delete. Without a WHERE clause, all records in the table will be deleted.

⚠️ Warning: Omitting the WHERE clause in a DELETE statement will remove all rows from the table.

Deleting a Single Record

To delete a specific record, use a condition that uniquely identifies the row, such as a primary key:

Example:

				
					DELETE FROM employees
WHERE employee_id = 105;
				
			

This statement deletes the employee with ID 105 from the employees table.

Deleting Multiple Records

You can also delete multiple rows by using a condition that matches several records.

Example:

				
					DELETE FROM employees
WHERE department_id = 4;
				
			

This deletes all employees in department 4.

Best Practices

Use WHERE Carefully

Always double-check the condition. A missing or incorrect WHERE clause can lead to unintended data loss.

Run a SELECT First

Before running a DELETE, it’s wise to preview the records you’re about to remove:

				
					SELECT * FROM employees
WHERE department_id = 4;
				
			

Use Transactions When Possible

When working in production, wrap deletions in a transaction block to allow for rollback if something goes wrong:

				
					BEGIN;

DELETE FROM employees
WHERE department_id = 4;

-- COMMIT; -- Uncomment this when you're sure
-- ROLLBACK; -- Use this if something doesn't look right
				
			

Deleting with JOIN

You can also delete records based on a JOIN with another table. This is useful when the condition involves multiple tables.

Example:

				
					DELETE e
FROM employees e
JOIN departments d ON e.department_id = d.id
WHERE d.location = 'Boston';
				
			

This deletes all employees who belong to departments located in Boston.

Note: Syntax for DELETE with JOIN may vary slightly depending on your SQL dialect (MySQL, PostgreSQL, SQL Server, etc.).

Deleting All Records (with Caution)

If you really need to clear an entire table:

				
					DELETE FROM employees;
				
			

Or, for better performance (especially in large tables) and if you don’t need to log individual row deletions:

				
					TRUNCATE TABLE employees;
				
			

TRUNCATE is faster but irreversible and may reset identity columns, depending on the DBMS.

Conclusion

Deleting records in SQL is straightforward but requires caution and planning. Always validate the conditions before executing, and make use of transactions and backups when working in sensitive environments. With proper handling, deletions can be a powerful tool for maintaining data integrity and cleanliness in your database systems.

Please see the other SQL video learning articles below as part of this learning SQL online course video. 

If you would like an instructor lead SQL course that goes into more detail with worked examples then we also provide a video course to learn SQL online hosted on UDEMY. 

Learning SQL programming with SQL server
Learn SQL Online