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

    MySQL

    MySQL Correlated Subquery

    Overview

    In this tutorial, we will learn about correlated subquery with example. Correlated subquery contains subquery references in the outer query and cannot be evaluated independently of it.

    Example

    In this example, we will use the City and the Country tables from the world MySQL database. The table structures and the column names are as follows:

    mysql> DESC City;
    +————-+———-+——+—–+———+—————-+
    | Field | Type | Null | Key | Default | Extra |
    +————-+———-+——+—–+———+—————-+
    | ID | int | NO | PRI | NULL | auto_increment |
    | Name | char(35) | NO | | | |
    | CountryCode | char(3) | NO | MUL | | |
    | District | char(20) | NO | | | |
    | Population | int | NO | | 0 | |
    +————-+———-+——+—–+———+—————-+

     

    City Table Structure World Database

     

    mysql> DESC Country \G

     

    DESCRIBE Country World Table

    Correlated Subquery

    Let’s count the number of cites for each country using a correlated subquery. This query uses a correlated subquery to count the number of cities in the City table which have a matching country code to those in the Country table. The outer query then selects the country names, continents and their respective city counts.

    mysql> SELECT Name,Continent,
                 (SELECT COUNT(*) FROM City
                 WHERE CountryCode=Country.Code)
                AS City_Count FROM Country;

    The subquery reference to the Country table from the outer query makes it a correlated subquery. Due to this reference, this subquery cannot be executed as a stand-along query.

    To show the output let’s filter the query to display a single country result. For example, to display the number of cities of the Canada country, we can use the following query:

    mysql> SELECT Name,Continent,
    -> (SELECT COUNT(*) FROM City
    -> WHERE CountryCode=Country.Code)
    -> AS City_Count FROM Country
    -> WHERE Name =’Canada’;
    +——–+—————+————+
    | Name | Continent | City_Count |
    +——–+—————+————+
    | Canada | North America | 49 |
    +——–+—————+————+
    1 row in set (0.00 sec)

     

    MySQL Correlated Subquery

    —

    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 Non-Correlated Subquery› MySQL Scalar Subquery

    Recent Posts

    • Running Tests in Parallel with Selenium Grid
    • Advanced Selenium Features
    • Locating Web Elements
    • Running the Test Script
    • Writing Your First Selenium Test Script
    • Getting Started with Selenium Automation Testing
    • Setting Up the Environment
    • How can you monitor the Quality Assurance Audit?
    • Leveraging LambdaTest with Appium 2.0
    • Appium 2.0 Plugins ListAppium 2.0 Plugins
    • Touch Actions and Multi-Touch Actions
    • Changes in Drivers and Classes
    • Appium Inspector
    • Capabilities in Appium 2.0
    • Appium 2.0 Driver ListAppium 2.0 Driver Installation & Management
    CyberLink Multimedia Software

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com