MySQL Non-Correlated Subquery
Non-Correlated Subquery
In this tutorial, we will learn about MySQL Non-Correlated Subquery with examples. A non-correlated subquery contains no references to the outer query, is not dependent on it, and can be evaluated as a completely separate SQL statement.
Example
In this example, we will use the world MySQL database and the City and the Country tables.
City Table
Country Table
This query uses a non-correlated subquery to list the names of all the cities in the City table
that correspond with countries in the Country table that are part of the continent of ‘South America’.
First, the inner query (subquery) finds all the country codes by continent. The outer query then selects the city names to go with each code in the City table. Since the subquery does not reference the City table (in the outer query), it is not a correlated subquery.
mysql>SELECT Name FROM City WHERE CountryCode IN
-> (SELECT Code FROM Country
-> WHERE Continent= ‘South America’)
Another aspect of this subquery that makes it non-correlated is that it can be run as a stand-along query:
mysql>SELECT Code FROM Country
->WHERE Continent= ‘South America’;
We can run the inner query itself of the non-correlated subquery (without the parenthesis) to see what it returns.
—
MySQL Tutorials
MySQL Tutorials on this website:
https://www.testingdocs.com/mysql-tutorials-for-beginners/
For more information on MySQL Database: