• Learning MySQL By Example
  • Introduction
  • 1. How to Retrieve Data From a Single Table
  • 2. How to Retrieve Data from Multiple Tables
  • 3. Using Functions
  • 4. How to Insert, Update, Delete Data in Tables
  • 5. Summary Queries and Aggregate Functions
  • 6. Working With Subqueries
  • 7. SQL Views
  • 8. SQL Indexes
  • Glossary
  • Index
  • Download
  • Translations
  • 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.