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 Subquery

    Overview

    In this tutorial, we will learn about MySQL Subquery. A subquery is also called a nested query. A subquery is a query nested inside another SQL query. The subquery is enclosed within parenthesis inside the outer query.

    Syntax

    There are three locations in an SQL statement that a subquery can be placed.

    • Column
    • FROM clause
    • WHERE clause

    Subqueries are most commonly placed in the WHERE location in a query statement. The simple and basic subquery used with the WHERE clause is as follows:

    SELECT [columns_list]
    FROM table
    WHERE clause = ( Subquery )

    The subquery is executed and the subquery results are used by the outer query.

    The subquery can also fetch records from another different table. In some cases, it’s much easy to use subquery instead of joining the tables.

    Subquery Types

    https://www.testingdocs.com/mysql-subquery-categories/

    Subquery Placement

    https://www.testingdocs.com/mysql-subquery-placement-chart/

    Column designated subquery

    We can use the column designation part of a query to perform a subquery. The outer query will be run once for each row of the result retrieved from the subquery. Each result must be one column and a maximum of one row.

    Let’s perform a query that retrieves the countries that are located for example in the Southern Europe region, with the number of cities per each country. We can use a correlated subquery that calculates the number of cities per country as a column in the outer query.

    mysql> SELECT Country.Name,
    -> (SELECT COUNT(*) FROM City
    -> WHERE City.CountryCode=Country.Code)
    -> AS CityCount
    -> FROM Country
    -> WHERE Country.Region = ‘Southern Europe’;
    +——————————-+———–+
    | Name | CityCount |
    +——————————-+———–+
    | Albania | 1 |
    | Andorra | 1 |
    | Bosnia and Herzegovina | 3 |
    | Spain | 59 |
    | Gibraltar | 1 |
    | Greece | 8 |
    | Croatia | 4 |
    | Italy | 58 |
    | Macedonia | 1 |
    | Malta | 2 |
    | Portugal | 5 |
    | San Marino | 2 |
    | Slovenia | 2 |
    | Holy See (Vatican City State) | 1 |
    | Yugoslavia | 8 |
    +——————————-+———–+
    15 rows in set (0.00 sec)

    Column Desginated 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

    ‹ Database Users› MySQL Database Products

    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