TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    MySQL

    Delete rows in a table using Subquery

    Overview

    In this tutorial, we will learn how to delete rows using a subquery.  Use of subqueries is not limited to SELECT statements. We can use the subqueries to update or delete rows in a table.

    Syntax

    The general syntax for the SQL statement is as follows:

    DELETE FROM <table_name>
    WHERE <condition>
    (<Sub query>)

    Example

    In this example, we will use the City table from the world MySQL table. We will remove all the cities from the City table that has population less than 100.

    Let’s check how many Cities have population < 100 in the City table with the following query.

    mysql> SELECT * FROM City WHERE Population < 100;
    +——+———–+————-+————+
    | ID | Name | CountryCode | Population |
    +——+———–+————-+————+
    | 2912 | Adamstown | PCN | 42 |
    +——+———–+————-+————+
    1 row in set (0.00 sec)

    Let’s delete the city from the City table using subquery

    mysql> DELETE FROM City
    -> WHERE CountryCode IN
    -> (SELECT c.CountryCode
    -> FROM (SELECT * FROM City ) AS c
    -> WHERE c.Population < 100 );

    Query OK, 1 row affected (0.04 sec)

     

    DELETE Rows using Subquery MySQL

    —

    MySQL Tutorials

    MySQL Tutorials on this website:

    https://www.testingdocs.com/mysql-tutorials-for-beginners/

    For more information on MySQL Database:

    https://www.mysql.com/

    Related Posts

    Stellar Converter for Database Tool

    MySQL /

    Stellar Converter for Database

    Stellar MySQL Log Analyzer

    MySQL /

    Stellar Log Analyzer for MySQL

    Stellar Repair for MySQL

    MySQL /

    Stellar Repair for MySQL

    MySQL /

    How to secure your SQL Database: Tips and Tricks

    Stellar Converter for Database

    MySQL /

    Database Converter Tools

    ‹ MySQL Inline Views› MySQL Table Aliases

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com