How to Use MySQL Joins on Alibaba Cloud MySQL Database

By Francis Ndungu, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

MySQL is one of the most popular relational database management systems (RDBMS). It is secure, scalable and has high performance with around-the-clock uptime guarantee for many websites and applications.

Apart from its comprehensive transactions support, reduced cost of ownership and flexibility associated with open source technology, MySQL supports different types of joins.

A Structured Query Language (SQL) join is used to fetch data from multiple tables in just a single statement. Such queries reduce the server overhead and eliminate the need for sending data multiple times from the application to the MySQL server.

This optimizes the speed and performance of the front-end application and the server. Joins allow developers to craft normalized databases where facts are only represented once. Normalization is the key to reducing data redundancy and storage space. Without joins, normalization would not work.

Another great advantage of joins is the ability to perform filters, computations and sorting prior to displaying the results of an SQL statement. From a programmer’s perspective, joins make SQL statements easier to read and maintain.

In this tutorial, we will go over the different MySQL joins that you can implement on your application to craft better, faster, optimized and high performing databases on Alibaba Cloud.


To follow along with our tutorial, you will require the following in order to test the syntax and run our clear MySQL join examples:

  1. A valid Alibaba Cloud account. You can signup now to get free credit and test over 40 Alibaba Cloud articles.
  2. An Alibaba Cloud ECS instance running MySQL server or an ApsaraDB for MySQL instance
  3. A username and password for your MySQL server

Types of MySQL Joins

A join occurs when two or more tables are joined in an SQL statement. We are going to discuss three types of MySQL joins in this guide:

  1. Inner join
  2. Left join
  3. Right join

The syntax of the join command is shown below:

The join condition should follow after the ON keyword. To better understand how MySQL joins works, we will create a test database and populate it with some sample data. For the sake of simplicity, we are going to use a hypothetical store known as mystore.

This store sells products to multiple customers who can register on any of the 5 offices that are spread across the country. Each store has a unique store_id while customers are identified by a customer_id. A customer can only belong to one office.

Orders are recorded in sales table that captures the customer_id, sales _id and amount for the sale. We have prepared the SQL commands for creating this database to make it easy for you.

So login to your MySQL server and enter the command below to create the database:

Then run the command below to switch to the database:

We will create the offices table first. So run the command below:

Then, insert the following sample data by entering the below SQL statements one by one:

You can confirm the presence of the data by running the command below:

You should see an output similar to the one shown below:

Next, we are going to create the customers table using the syntax below:

Just like we did with the offices table, run the commands below one by one to populate the customers table with sample data:

To confirm the presence of records in the customers table, run the query below:

The customers’ list shown be displayed as shown below:

After creating the offices and customers table, we can now create the sales table by running the command below:

We will populate the sales table with some sample orders. So, run the commands below on the MySQL server:

Run the query below to confirm the presence of data in the sales table:

An output similar to the one below should be displayed:

With the sample data in place, we can go ahead and discuss the 3 types of MySQL joins that we listed above and illustrate the same by executing some examples.

MySQL Inner Join

This is the simplest form of MySQL join. When an inner join is executed it only returns records that meet the join condition.

For instance, if we run the inner join statement against the offices and customers table with the office_id as the matching column, MySQL will only return the customers who have a matching office_id from the offices table. Similarly, only the offices with associated customers will be returned.

The SQL syntax for the inner join should look like this:


As you can see above, some records were omitted from the offices and customers table because they don’t satisfy the join condition.

To be specific, two offices(Guangzhou and Hongkong) were not displayed because they don’t have any associated customer. On the other hand, Richard Roe and Joe Bloggs records were skipped on the output as the customers were not set with an office_id that exists on the offices table.

The traditional way of writing this statement is shown below:

We can also represent MySQL inner join visually using the diagram below. The area shaded with red shows the records that meet the join condition:

MySQL Left Join

MySQL left join returns all records from the left table with the associated records from the right table that match the join condition.

However, unlike an inner join, records from the left table are returned irrespective of whether they have a matching row on the right or not.

Since all records on the left table are returned, any left side record that does not have associated records on the right displays NULL values.

To put this into perspective, if we run a left join against the offices and customers table, the list of all offices will be returned irrespective of whether there is a customer associated with that office_id or not.

The syntax looks as follows:


As you can see above, Guangzhou and Hongkong do not have associated customers, hence NULL values are displayed on the right side of the output.

MYSQL left join can be illustrated by the diagram below in a pictorial manner. The area shaded in red displays the records that are returned:

MySQL Right Join

MySQL right join would respond just in the exact opposite manner as a left join. This query returns all data from the right side table and the matching records from the left side table. However, records from the right table are returned irrespective of whether there is a matching record on the left table.

To examine this behaviour, let us run a right join statement against our offices and customers table:


As you can see from the output above, the right join query was able to return all customers(right side) irrespective of whether the customers are associated with an office based on the office_id field. Records from the left side table with NULL values show customers without a matching office.

Here is a pictorial representation of the right join:

Joining 3 or More Tables with MySQL

The maximum number of tables that can be referenced in a single join is 61. However, for better performance, you should try to limit the number to around a dozen.

Just like we did with two tables, we are going to join 3 tables using the syntax below:

So, let’s run the command above on all the 3 tables:

The above statement should return a list of offices together with the associated data that meets the two different join conditions.

Sample output:

As you can see above, only one record met the two conditions that we set for the inner join.

We can run the same query on the 3 tables using the left join command to examine the records:

Sample output:

To make the output clear to read, we can run the query without repeating duplicate columns used on the join condition like this:



In this tutorial, we covered the different types of MySQL joins and how you can apply them on your database hosted on Alibaba Cloud ApsaraDB for MySQL or Elastic Compute Service (ECS) instances. We have included the basic syntax, examples and pictorial representation to help you understand how SQL inner, left and right Joins work.

We believe the information gained from this guide will help you to write better and optimum SQL queries involving multiple tables to improve the speed of your web application or website.


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store