Learning MySQL By Example
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');

CC BY-NC-ND International 4.0

CC BY-NC-ND International 4.0: This work is released under a CC BY-NC-ND International 4.0 license, which means that you are free to do with it as you please as long as you (1) properly attribute it, (2) do not use it for commercial gain, and (3) do not create derivative works.

End-of-Chapter Survey

: How would you rate the overall quality of this chapter?
  1. Very Low Quality
  2. Low Quality
  3. Moderate Quality
  4. High Quality
  5. Very High Quality
Comments will be automatically submitted when you navigate away from the page.
Like this? Endorse it!