FB

The SQL DELETE statement allows you to delete records from a table. I’ll explain more about the SQL DELETE statement in this article.

This article covers Oracle, SQL Server, MySQL, and PostgreSQL.

 

What Is The SQL DELETE Statement?

The SQL DELETE statement, or delete query, is a statement you can run to delete records from a table.

A WHERE clause is used to specify the criteria, and any rows matching these criteria will be deleted. You can use it to delete a single record, multiple records, or all records in a table.

 

How Can I Write a DELETE Query in SQL?

The DELETE query in SQL has some pretty simple syntax:

DELETE FROM [ table | (subquery) ] [alias]
[WHERE condition];

Let me explain this a bit further.

You start with DELETE FROM. There is no need to specify the columns here – we’re working on deleting rows so the columns don’t matter.

Then, you specify either a table or a subquery. The table here can be either a table, a view, or a materialized view.

Then, you can give the table an alias, to help when writing your WHERE condition.

After this, you can write WHERE then a condition. The condition allows you to specify which rows to delete. If you only want to delete some of the rows in a table, you’ll need the WHERE condition. If you want to delete all rows in a table, don’t use a WHERE condition.

 

Examples of an SQL DELETE Query

Let’s see some examples. First, let’s see some sample data that we can run our DELETE SQL statements on.

SELECT student_id, first_name, last_name
FROM student;
STUDENT_IDFIRST_NAMELAST_NAME
1JohnSmith
2SusanJohnson
3TomCapper
4MarkHolloway
5StevenWebber
6JulieArmstrong
7MichelleRandall
8AndrewCooper
9RobertPickering
10TanyaHall
11JarradWinston
12MaryTaylor
100MarkAnderson
102JohnRogers

 

Example 1 – Simple Delete

This is an example of deleting a single record.

DELETE FROM student
WHERE student_id = 1;

The record that has the student_id of 1 is deleted. All other rows remain in the table.

Example 2 – Using Two Conditions

This example uses two conditions in the WHERE clause.

DELETE FROM student
WHERE enrolment_date > '10-JAN-2015'
AND fees_paid > 400;
1 row(s) deleted.

As you can see, 1 row has been deleted because 1 row matched the WHERE condition.

 

Can You Delete From Multiple Tables?

No, you can’t. In all vendors in this article (Oracle, SQL Server, MySQL, PostgreSQL), the SQL DELETE statement only allows you to specify one table at a time.

So, a query like this will not work:

DELETE from student, student_subject
WHERE student_id = 1;

If you want to delete from multiple tables, you need to write multiple DELETE statements.

DELETE from student
WHERE student_id = 1;

DELETE from student_subject
WHERE student_id = 1;

 

Can You Delete Data Based On Data In Another Table?

Yes, you can.

Let’s explain what I mean.

Say, for example, you had your student table. And let’s say you wanted to delete all student records that were not in any subjects. This data, though, is in the student_subject table, which links students to subjects.

But, we can’t specify more than one table in the SQL DELETE statement, right?

Actually, you can. You just can’t delete from more than one table at a time.

You can do that using a join or using a subquery. The method you use may depend on the vendor:

  • Oracle: Subquery only, does not support join
  • SQL Server: Subquery or join
  • MySQL: Subquery or join
  • PostgreSQL: Subquery or join

 

SQL Delete With Subquery

Let’s see how we can do this.

DELETE FROM student
WHERE student_id NOT IN (
  SELECT student_subject.student_id
  FROM student_subject
);

This query first looks up student_id values in the student_subject table. Then, it deletes records from the student table where the student_id is not in that list.

The method of using a subquery to limit values works in a similar way to the SELECT query. You can use IN, EXISTS, and NOT EXISTS in the same way (read more about these operators here).

 

SQL Delete With Join

To delete from one table based on data in another table, you can delete using a join. This will work in MySQL, PostgreSQL, and SQL Server.

You can join two tables together in the same way as you would with a SELECT statement, but you just need to specify the name of the table to delete from:

DELETE student
FROM student
INNER JOIN student_subject ON student.student_id = student_subject.student_id
WHERE student_subject.subject_id = 1;

This will delete all student records that have a student_subject record with a subject_id of 1.

You can also use table aliases to make the query easier to read and write:

DELETE student
FROM student st
INNER JOIN student_subject ss ON st.student_id = ss.student_id
WHERE ss.subject_id = 1;

This will give you the same result.

 

Can I See What I’m Deleting Before It Is Deleted?

Yes, you can.

If you want to be sure you’re deleting the right records, you can check what you delete before you actually delete the records.

To do this, simply copy the DELETE statement and run it as a SELECT statement first – with the same WHERE clause and everything.

As an example, if we wanted to delete the students that were not enrolled in a subject (just like the previous example), we can check we’re deleting the right records first.

The SELECT query is here:

SELECT * FROM student
WHERE student_id NOT IN (
  SELECT student_subject.student_id
  FROM student_subject
);

And, the Oracle DELETE query to do that is here:

DELETE FROM student
WHERE student_id NOT IN (
  SELECT student_subject.student_id
  FROM student_subject
);

As you can see, I just replaced the DELETE with a SELECT *.

 

My SQL DELETE Statement Is Too Slow!

Are you trying to delete data from a table but it’s too slow?

Well, there are a few things you can do.

First, if you’re deleting all of the data in the table, it may be better to use the TRUNCATE TABLE statement. It’s faster (because it doesn’t write to a log and allow for rollbacks), but you can’t undo it, so you need to be sure. You also can’t specify a WHERE condition with a TRUNCATE TABLE statement.

If you can’t use TRUNCATE, you can try to filter your data differently or use indexes on your table.

Another method is actually creating a new table with the data you want to keep. You can do this by :

  • Write a CREATE TABLE AS SELECT method and then specifying the WHERE clause for the records you want to keep
  • Delete the original table
  • Rename the new table to the old table

 

Summary of Differences Between Vendors

There are some differences between the way the DELETE statement works with each vendor:

CriteriaOracleSQL ServerMySQLPostgreSQL
Basic DELETEFollows standardFollows standardFollows standardFollows standard
Delete with subqueryAllowedAllowedAllowedAllowed
Delete with joinNot allowedAllowedAllowedAllowed

 

Conclusion

So, that’s how you run an Oracle DELETE SQL statement. You can delete a single row, multiple rows, or the entire table, using WHERE conditions which can involve subqueries.

Lastly, if you enjoy the information and career advice I’ve been providing, sign up to my newsletter below to stay up-to-date on my articles. You’ll also receive a fantastic bonus. Thanks!

Get Your SQL Cheat Sheets Now: