This Oracle tutorial explains how to use the Oracle DELETE statement with syntax, examples, and practice exercises.
The Oracle DELETE statement is used to delete a single record or multiple records from a table in Oracle.
The syntax for the DELETE statement in Oracle/PLSQL is:
The table that you wish to delete records from.
Optional. The conditions that must be met for the records to be deleted. If no conditions are provided, then all records from the table will be deleted.
Let's look at a simple Oracle DELETE query example, where we just have one condition in the DELETE statement.
For example:
This Oracle DELETE example would delete all records from the customers table where the last_name is Smith.
You may wish to check for the number of rows that will be deleted. You can determine the number of rows that will be deleted by running the following Oracle SELECT statement before performing the delete.
Let's look at an Oracle DELETE example, where we just have two conditions in the DELETE statement.
For example:
This Oracle DELETE example would delete all records from the customers table where the last_name is 'James' and the customer_id is greater than 25.
You may wish to check for the number of rows that will be deleted. You can determine the number of rows that will be deleted by running the following Oracle SELECT statement before performing the delete.
You can also perform more complicated deletes.
You may wish to delete records in one table based on values in another table. Since you can't list more than one table in the Oracle FROM clause when you are performing a delete, you can use the Oracle EXISTS clause.
For example:
This Oracle DELETE example would delete all records in the suppliers table where there is a record in the customers table whose customer_id is greater than 25, and the customer_id matches the supplier_id.
If you wish to determine the number of rows that will be deleted, you can run the following Oracle SELECT statement before performing the delete.
Question: How would I write an Oracle DELETE statement to delete all records in TableA whose data in field1 & field2 DO NOT match the data in fieldx & fieldz of TableB?
Answer: You could try something like this for your Oracle DELETE statement:
Based on the contacts table, delete all records from the contacts table who reside in the City of 'Las Vegas' and whose first_name is 'Jane'.
The following Oracle DELETE statement would delete these records from the contacts table:
Based on the contacts table, delete all records from the contacts table whose contact_id is greater than or equal to 5000 and less than 6000.
The following Oracle DELETE statement would delete these records from the contacts table:
Or you could write the solution using the BETWEEN clause as follows: