6.4

The Subquery In a Delete Statement

The Subquery in a DELETE statement

  • A subquery can be used in a DELETE statement.
  • Always back up your data and test your DELETE statement before running it on live data.

NOTE: Before you can run a DELETE or UPDATE statement without a WHERE clause, you must uncheck “Safe Updates” checkbox in MySQL Preference. Please see below.

sub_04.png

Code Sample:

USE world;
DELETE FROM city_bak
WHERE CountryCode IN
    (SELECT code FROM country
        WHERE region = 'Central Africa');

Results:

sub_05.png

USE world;

DELETE FROM city_bak

WHERE CountryCode IN

(SELECT code FROM country

     WHERE region = 'Central Africa');

This content is provided to you freely by EdTech Books.

Access it online or download it at https://edtechbooks.org/learning_mysql/the_subquery_in_a_de.