If you are someone who handles databases at work, I’m sure you use SQL a lot. Doesn’t SQL make it a breeze to work with and edit the contents of large databases? With so many built-in functions for all kinds of queries, SQL is a must-know tool for all data scientists and data analysts. In this article we are going to learn about one such SQL function – SQL DELETE. This is a DML (Data manipulation language) command that comes in very handy. We will look at the syntax of the SQL DELETE statement and learn how to use it through practical examples.
If you’re just starting out to explore SQL, here’s a beginner’s guide to help you: SQL For Data Science: A Beginner Guide
The SQL DELETE statement is a tool used to delete records existing from the table. The DELETE statement can remove records selectively from the table as well. To remove records selectively WHERE clause is used. DELETE statement is very important as it manages the integrity of the table. Note: we can delete single or multiple records based on our condition.
Below is the basic syntax of the SQL DELETE statement
DELETE FROM table_name
WHERE condition;
Note:
Let’s experiment with the DELETE command with a sample table.
To test the DELETE statement in SQL, we will first create a sample employees’ table.
CREATE TABLE employees (
employee_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
We will insert some sample records
INSERT INTO employees (first_name, last_name, department, salary) VALUES
('John', 'Doe', 'Sales', 60000),
('Jane', 'Smith', 'Marketing', 65000),
('Jim', 'Brown', 'Sales', 55000),
('Jake', 'White', 'IT', 70000),
('Jill', 'Black', 'IT', 72000),
('Janet', 'Green', 'HR', 50000),
('James', 'Blue', 'HR', 52000),
('Jack', 'Yellow', 'Marketing', 67000),
('Julia', 'Purple', 'Sales', 63000),
('Jerry', 'Orange', 'Marketing', 62000);
DELETE FROM employees
WHERE employee_id = 5;
The above code specifically deletes the record with employee_id = 5. This will delete only one record as there is only one record with employee_id as 5.
In the above image we can see that the record with employee_id as 5 was deleted. We can see Query OK, 1 row affected which implies 1 row has been deleted.
DELETE FROM employees
WHERE department = 'Sales';
The above code will delete all the records with the department as Sales. This is how WHERE clause can be used to delete multiple records.
In the above image we can see that 3 rows have been deleted. There were 3 records with sales as their department. Those were deleted using our DELETE command.
DELETE FROM employees;
When we don’t specify our WHERE clause DELETE command will delete all the records from the table.
We can see that all the remaining 6 records from our table have been deleted. Hence we have to be careful when using the DELETE command.
DELETE FROM employees
WHERE salary < (
SELECT avg_salary FROM (
SELECT AVG(salary) AS avg_salary FROM employees
) AS derived_table
);
We would sometimes have to DELETE based on some conditions which require subqueries. The above is the example to delete records with a salary less than average salary. Since our table is empty we will add the same sample records, then use our query.
In the above image we can see that records with salary less than average salary have been deleted.
Some tips to consider when performing DELETE commands.
SQL Delete statements are important to manage tables in relational databases. With the understanding of the DELETE command you can remove records effectively from tables. Always use the WHERE clause to target specific records, and ensure you have proper backups before performing delete operations. With the employees sample we can get the basics of the DELETE command.
Learn More: SQL: A Full Fledged Guide from Basics to Advanced Level
A. The SQL DELETE statement is used to remove records from a database table.
A. Using the WHERE clause specifies which records to delete; without it, all records will be deleted.
A. Before using DELETE, ensure you have a backup, review conditions, consider foreign key constraints, and test in a safe environment.