How to query and merge records using MariaDB JOIN
In a relational database management system, you can compare data records in different tables with each other. This makes it possible to create connections and extract matching values from two different tables. This task is performed with JOIN
in MariaDB. The statement is used in combination with SELECT
and can be divided into different categories. We’ll present INNER JOIN
, LEFT OUTER JOIN
and RIGHT OUTER JOIN
in more detail below.
What’s the syntax for JOIN
?
To help you understand the various JOIN
statements in MariaDB, we’ll start by presenting the basic syntax of the statement. Here’s the syntax for INNER JOIN
:
Using SELECT
, you specify the column (or columns) you want to include. Replace the placeholder “table_1” with the first table and “table_2” with the second table that you want to join with the first. The INNER JOIN
operation compares each row in the first table with each row in the second table. Only matching records (those present in both tables) will be displayed in the result table. Non-matching records are excluded from the output.
- Enterprise-grade architecture managed by experts
- Flexible solutions tailored to your requirements
- Leading security in ISO-certified data centers
How to use INNER JOIN
in MariaDB
To demonstrate how INNER JOIN
works in MariaDB, let’s take a look at a straightforward example. We’ll use a database that has two tables. The first table is named “Customer List,” and the second is “Orders.” We can create the “CustomerList” table using MariaDB CREATE TABLE. This table includes the columns “Customer ID,” “LastName,” “FirstName,” “City,” and “CreationDate.” Here’s what the code looks like:
Now, let’s fill this table with some values. To do this, we use INSERT INTO
:
We then create the “Orders” table. This contains the columns “OrderID”, “ProductID”, “CustomerName” and “OrderDate”. The code looks like this:
We’re also going to fill this table with sample values:
Now, we’ll use INNER JOIN
for MariaDB to filter for customers who appear in the customer list and have placed an order listed in the Orders table. The corresponding code looks like this:
In this example, we focus on the last name in the customer list and the customer name in the orders. When these values match, they are included in the results. Since the customers Meyer and Rodman appear in both tables, the output would look like this:
Customer ID | Customer Name | Order ID | Product ID |
---|---|---|---|
3 | Meyer | 102 | 332 |
2 | Rodman | 104 | 191 |
LEFT OUTER JOIN
LEFT OUTER JOIN
in MariaDB works according to a similar principle and also uses an almost identical syntax. In contrast to INNER JOIN
, however, all data records from the first or left table (in our example “CustomerList”) are output and only the matching data records from the second or right table (“orders”). If there is no equivalent in the second table, the value is specified as NULL. Using the example from above, this is what the code looks like:
This gives us the following result:
Customer Name | Product ID |
---|---|
Smith | NULL |
Rodman | 191 |
Meyer | 332 |
Garcia | NULL |
Davis | NULL |
RIGHT OUTER JOIN
In MariaDB, RIGHT OUTER JOIN
operates in the opposite manner. Here, data from the second or right table is combined with matching values from the first or left table. If there is no match, the resulting value will be NULL. Below is the code:
This is what the output looks like:
Customer Name | Product ID |
---|---|
NULL | 247 |
Meyer | 332 |
NULL | 247 |
Rodman | 191 |
NULL | 499 |
In our Digital Guide you’ll find many useful tutorials and articles for MariaDB. For example, there’s a comparison of MariaDB and MySQL a tutorial on installing MariaDB and instructions on how to reset your MariaDB root password.