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

What Is The Oracle DELETE SQL Statement?

The Oracle DELETE SQL 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 this 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 Oracle DELETE Query in SQL

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;
1 John Smith
2 Susan Johnson
3 Tom Capper
4 Mark Holloway
5 Steven Webber
6 Julie Armstrong
7 Michelle Randall
8 Andrew Cooper
9 Robert Pickering
10 Tanya Hall
11 Jarrad Winston
12 Mary Taylor
100 Mark Anderson
102 John Rogers


Example 1 – Simple Delete

This is an example of deleting a single record.

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.

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.


In Oracle, Can You Delete From Multiple Tables?

No, you can’t. The Oracle DELETE SQL 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.

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 Oracle DELETE statement, right?

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

But you can add the tables to check in a subquery.

Let’s see how we can do this.

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).

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:

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 Oracle 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

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!

Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Get Your SQL Cheat Sheets Now: