Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

SQL delete row – Operator DELETE

19 June 2020

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_idsupplier_namecitystate
100YandexMoscowMoscow
200GoogleLansingMichigan
300OracleRedwood CityCalifornia
400BingRedmondWashington
500YahooSunnyvaleWashington
600DuckDuckGoPaoliPennsylvania
700QwantParisIle de France
800FacebookMenlo ParkCalifornia
900Electronic ArtsSan FranciscoCalifornia

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_idsupplier_namecitystate
200GoogleLansingMichigan
300OracleRedwood CityCalifornia
400BingRedmondWashington
500YahooSunnyvaleWashington
600DuckDuckGoPaoliPennsylvania
700QwantParisIle de France
800FacebookMenlo ParkCalifornia
900Electronic ArtsSan FranciscoCalifornia

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_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL

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_idproduct_namecategory_id
1Pear50
5Bread75
6Sliced Ham25
7KleenexNULL

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_idfirst_namelast_namefavorite_website
4000JustinBiebergoogle.com
5000SelenaGomezbing.com
6000 MilaKunisyahoo.com
7000TomCruiseoracle.com
8000JohnnyDeppNULL
9000RussellCrowegoogle.com

And the table with the following data:

order_idcustomer_idorder_date
170002019/06/18
250002019/06/18
380002019/06/19
440002019/06/20
5NULL2019/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_idcustomer_idorder_date
170002019/06/18
250002019/06/18
380002019/06/19
5NULL2019/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

 
Tags: , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB 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...

Preamble​​MS 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...

Preamble​​Atom 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...

Preamble​​MongoDB 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...

Preamble​​SQLShell 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...

Preamble​​Writing 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...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM 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....

Preamble​​If 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...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...