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 Convert Joins to Subqueries

Overview

In this tutorial, we will learn steps to Convert Joins to Subqueries with an example. We can replace a join statement with a subquery. Sometimes its easy to construct a subquery rather than using a join. However, subqueries will normally run slower than join statements.

Steps to convert a Subquery to a JOIN statement:

https://www.testingdocs.com/mysql-convert-subquery-to-join/

Example

In this example, we will use the Country and the CountryLanguage tables from the world MySQL database. The following query shows the JOIN query to know the languages spoken in a particular continent, for example, North America.

JOIN Statement

mysql> SELECT DISTINCT language
-> FROM Country AS c
-> JOIN CountryLanguage AS cl
-> ON cl.CountryCode=c.Code
-> WHERE c.Continent= ‘North America’;


+——————+
| language |
+——————+
| Dutch |
| English |
| Papiamento |
| Spanish |
| Creole English |
| Creole French |
| Garifuna |
| Maya Languages |
| Bajan |
| Chinese |
| Eskimo Languages |
| French |
| German |
| Italian |
| Polish |
| Portuguese |
| Punjabi |
| Ukrainian |
| Chibcha |
| Danish |
| Greenlandic |
| Cakchiquel |
| Kekchí |
| Mam |
| Quiché |
| Miskito |
| Haiti Creole |
| Hindi |
| Mixtec |
| Náhuatl |
| Otomí |
| Yucatec |
| Zapotec |
| Sumo |
| Arabic |
| Cuna |
| Embera |
| Guaymí |
| Nahua |
| Japanese |
| Korean |
| Tagalog |
| Vietnamese |
+——————+
43 rows in set (0.00 sec)

MySQL Join to Subquery

Subquery

Now let’s convert this to  subquery as shown below. The following subquery displays the same result as the above JOIN statement.

mysql> SELECT DISTINCT Language
             -> FROM CountryLanguage
             -> WHERE CountryCode IN
             -> (SELECT Code FROM Country WHERE Continent=’North America’);
+——————+
| Language |
+——————+
| Dutch |
| English |
| Papiamento |
| Spanish |
| Creole English |
| Creole French |
| Garifuna |
| Maya Languages |
| Bajan |
| Chinese |
| Eskimo Languages |
| French |
| German |
| Italian |
| Polish |
| Portuguese |
| Punjabi |
| Ukrainian |
| Chibcha |
| Danish |
| Greenlandic |
| Cakchiquel |
| Kekchí |
| Mam |
| Quiché |
| Miskito |
| Haiti Creole |
| Hindi |
| Mixtec |
| Náhuatl |
| Otomí |
| Yucatec |
| Zapotec |
| Sumo |
| Arabic |
| Cuna |
| Embera |
| Guaymí |
| Nahua |
| Japanese |
| Korean |
| Tagalog |
| Vietnamese |
+——————+
43 rows in set (0.01 sec)

 

MySQL Join to Subquery Example

 

 

—

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

DataGrip Desktop Shortcut

MySQL /

Launch DataGrip on Windows

DataGrip Download

MySQL /

Install DataGrip IDE on Windows 11

MySQL Workbench Windows 11

MySQL /

New MySQL Connection in Workbench

MySQL Command-line Client

MySQL /

Command-Line MySQL Client

Start MySQL Client

MySQL /

Start MySQL Client on Windows 11

‹ MySQL Convert Subquery to Join› MySQL EXISTS Operator

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com