REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
SQL delete row – Operator DELETE
The DELETE command removes rows from a table or the main database table view, for example, in MySQL, Oracle. In this article you will learn how to use the DELETE statement, with syntax and examples.
Description and syntax of DELETE operator in SQL
The DELETE SQL statement is used to remove one or more records from a table.
DELETE FROM table
[WHERE conditions];
- table – The table from which you want to remove the records.
- WHERE conditions – It’s optional. Conditions that must be met to delete records. If no conditions are met, all records in the table will be deleted.
Note: You do not need to list the fields in the DELETE operator as you delete the whole row from the table.
An example of a DELETE operator with one condition
If you run DELETE without conditions in the WHERE sentence, all entries from the table will be deleted. As a result, you will most often include the WHERE offer with at least one condition in your DELETE statement.
Let’s start with a simple example of a DELETE query that has one condition in the WHERE clause.
In this example, we have a table with the following data:
supplier_id | supplier_name | city | state |
---|---|---|---|
100 | Yandex | Moscow | Moscow |
200 | Lansing | Michigan | |
300 | Oracle | Redwood City | California |
400 | Bing | Redmond | Washington |
500 | Yahoo | Sunnyvale | Washington |
600 | DuckDuckGo | Paoli | Pennsylvania |
700 | Qwant | Paris | Ile de France |
800 | Menlo Park | California | |
900 | Electronic Arts | San Francisco | California |
Enter the following DELETE operator:
DELETE FROM suppliers
WHERE supplier_name = 'Yandex';
1 record will be deleted. Select the data from the supplier table again:
SELECT * FROM suppliers;
Here are the results you should get:
supplier_id | supplier_name | city | state |
---|---|---|---|
200 | Lansing | Michigan | |
300 | Oracle | Redwood City | California |
400 | Bing | Redmond | Washington |
500 | Yahoo | Sunnyvale | Washington |
600 | DuckDuckGo | Paoli | Pennsylvania |
700 | Qwant | Paris | Ile de France |
800 | Menlo Park | California | |
900 | Electronic Arts | San Francisco | California |
This example removes all entries from the table where supplier_name is Yandex.
You can check the number of rows to be deleted. You can determine the number of rows that will be deleted by executing the following SELECT query before deleting:
SELECT COUNT(*)
FROM suppliers
WHERE supplier_name = 'Yandex';
This query will return the number of records that will be deleted when the DELETE operator is executed.
COUNT(*) |
---|
1 |
Example – DELETE operator with more than one condition
You can have more than one condition in a DELETE instruction in SQL using either the AND or OR condition. The AND condition allows you to delete a record if all conditions are met. Condition OR deletes a record if one of the conditions is met.
Let us look at an example of using a DELETE operator with two conditions using the AND condition.
In this example, we have a product table with the following data:
product_id | product_name | category_id |
---|---|---|
1 | Pear | 50 |
2 | Banana | 50 |
3 | Orange | 50 |
4 | Apple | 50 |
5 | Bread | 75 |
6 | Sliced Ham | 25 |
7 | Kleenex | NULL |
Enter the following DELETE operator:
DELETE FROM products
WHERE category_id = 50
AND product_name <> 'Pear';
Three records will be deleted. Select the data from the products table again:
SELECT * FROM products;
Here are the results you’ll get:
product_id | product_name | category_id |
---|---|---|
1 | Pear | 50 |
5 | Bread | 75 |
6 | Sliced Ham | 25 |
7 | Kleenex | NULL |
In this example, all the records from the products table with category_id equal to 50 and product_name NOT ‘Pear’ are removed.
Example – using EXISTS with DELETE operator
You can also perform more complex deletions.
You can delete records in one table based on the values in another table. Since you cannot list more than one table in the FROM sentence when performing deletions, you can use the EXISTS sentence.
In this example, we have a table with the following data:
customer_id | first_name | last_name | favorite_website |
---|---|---|---|
4000 | Justin | Bieber | google.com |
5000 | Selena | Gomez | bing.com |
6000 | Mila | Kunis | yahoo.com |
7000 | Tom | Cruise | oracle.com |
8000 | Johnny | Depp | NULL |
9000 | Russell | Crowe | google.com |
And the table with the following data:
order_id | customer_id | order_date |
---|---|---|
1 | 7000 | 2019/06/18 |
2 | 5000 | 2019/06/18 |
3 | 8000 | 2019/06/19 |
4 | 4000 | 2019/06/20 |
5 | NULL | 2019/07/01 |
Введите следующий оператор DELETE:
DELETE FROM orders
WHERE EXISTS
(SELECT *
FROM customers
WHERE customers.customer_id = orders.customer_id
AND customers.last_name = 'Bieber');
1 record will be deleted. Select the orders data from the table again:
SELECT * FROM orders;
Here are the results you should get:
order_id | customer_id | order_date |
---|---|---|
1 | 7000 | 2019/06/18 |
2 | 5000 | 2019/06/18 |
3 | 8000 | 2019/06/19 |
5 | NULL | 2019/07/01 |
In this example, all records are removed from the order table where the customer table has a record with the name ‘Bieber’ and the same customer_id value in both tables. In this example, the record for order_id = 4 was deleted.
DELETE command keywords and parameters
- schema – authorization identifier, usually matching the name of some user
- table view – the name of the table from which rows are deleted; if a view is defined, the server deletes rows from the main view table
- subquery – a subquery that selects rows to be deleted; the server executes the subquery and uses the rows of its result as a FROM phrase table
- WHERE – removes only rows that satisfy the condition; the condition may refer to the table and contain a subquery.
When a DELETE statement is issued, any DELETE trigger defined on the table is enabled.
DELETE command Example #1
Deleting all rows from the table:
DELETE FROM temp_assign;
In this example, the DELETE command removes all rows without exception.
DELETE command Example #2
Removes from the table all sellers with a commission of less than $100 per month:
DELETE FROM emp WHERE JOB = 'SALESMAN' AND COMM < 100;
In this example, the DELETE command removes all rows that fall under the condition JOB = ‘SALESMAN’ AND COMM < 100;
DELETE command Example #3
The previous example can be recorded in a different way:
DELETE FROM (select * from emp) WHERE job = 'SALESMAN' AND comm < 100;
You can use the following DELETE command to remove all MySQL records:
DELETE * FROM table_nam;
The SQL DELETE Statement
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...