MariaDB LEFT JOIN, RIGHT JOIN, and INNER JOIN Compared
In MariaDB, INNER JOIN returns pairs of rows that meet the join condition. Use LEFT JOIN or RIGHT JOIN when you need to keep unmatched rows from one table. For each unmatched row, the result fills the other table’s columns with NULL.
Choose a join by the rows you need
| Join type | Rows kept | Example use |
|---|---|---|
INNER JOIN |
Matching row pairs | Cities in registered countries |
LEFT JOIN |
All rows from the left table | Include countries without cities |
RIGHT JOIN |
All rows from the right table | Include cities without a registered country |
In FROM countries AS a LEFT JOIN cities AS b, countries is on the left. Position in the join expression defines the left and right sides independently of the SELECT list. An equality condition pairs rows that have equal join keys.
Create the practice tables
Connect to MariaDB, select a practice database, and run the SQL below in the same connection. You need permission to create temporary tables. City 13 refers to an unregistered country ID, 4, and city 12 is a real row whose name is NULL. Country A has two cities so you can see what happens when more than one row matches.
CREATE TEMPORARY TABLE countries (
country_id INT PRIMARY KEY,
country_name VARCHAR(30) NOT NULL
);
CREATE TEMPORARY TABLE cities (
city_id INT PRIMARY KEY,
country_id INT NOT NULL,
city_name VARCHAR(30) NULL,
INDEX (country_id)
);
INSERT INTO countries VALUES (1, 'A'), (2, 'B'), (3, 'C');
INSERT INTO cities VALUES
(10, 1, 'AA'), (11, 1, 'AB'), (12, 2, NULL), (13, 4, 'DD');
Compare INNER JOIN and LEFT JOIN
SELECT a.country_name, b.city_id, b.city_name
FROM countries AS a
INNER JOIN cities AS b ON a.country_id = b.country_id
ORDER BY b.city_id;
country_name city_id city_name
A 10 AA
A 11 AB
B 12 NULL
Run the two queries in order. The first returns A–10, A–11, and B–12. B–12 qualifies because the keys match, even though the city name is NULL. An inner join can return NULL in a selected column.
SELECT a.country_name, b.city_id, b.city_name
FROM countries AS a
LEFT JOIN cities AS b ON a.country_id = b.country_id
ORDER BY a.country_id, b.city_id;
country_name city_id city_name
A 10 AA
A 11 AB
B 12 NULL
C NULL NULL
The second query adds C–NULL. Country C has no matching city, so both city_id and city_name are NULL. A appears twice because it matches two city rows. If you need one row per country, decide how to aggregate the city data first.
Rewrite RIGHT JOIN as LEFT JOIN
Both queries return A–10, A–11, B–12, and NULL–13. To keep all cities using LEFT JOIN, put the cities table on the left. You can keep the same SELECT list. The ORDER BY in both queries makes the output order comparable.
SELECT a.country_name, b.city_id, b.city_name
FROM countries AS a
RIGHT JOIN cities AS b ON a.country_id = b.country_id
ORDER BY b.city_id;
SELECT a.country_name, b.city_id, b.city_name
FROM cities AS b
LEFT JOIN countries AS a ON a.country_id = b.country_id
ORDER BY b.city_id;
country_name city_id city_name
A 10 AA
A 11 AB
B 12 NULL
NULL 13 DD
Place conditions in ON or WHERE
To list all countries while attaching only city 10, put the city condition in ON. The first query returns A–10, B–NULL, and C–NULL. WHERE acts on the joined result, so the second query returns only A–10.
SELECT a.country_name, b.city_id
FROM countries AS a
LEFT JOIN cities AS b
ON a.country_id = b.country_id AND b.city_id = 10
ORDER BY a.country_id;
SELECT a.country_name, b.city_id
FROM countries AS a
LEFT JOIN cities AS b ON a.country_id = b.country_id
WHERE b.city_id = 10
ORDER BY a.country_id;
The last query finds C, the only country without a city. b.city_id is a primary key and cannot be NULL in a stored city row. Testing b.city_name instead would also find B, whose matching city has a missing name.
SELECT a.country_name
FROM countries AS a
LEFT JOIN cities AS b ON a.country_id = b.country_id
WHERE b.city_id IS NULL;
Join three tables and inspect the plan
The second join uses the city ID from the first join to look up a population row. Cities 10 and 11 remain, with populations of 1000 and 2000. Change the second join to LEFT JOIN to keep cities with missing population data. City 12 then appears with NULL for its population.
CREATE TEMPORARY TABLE city_population (
city_id INT PRIMARY KEY,
population INT NOT NULL
);
INSERT INTO city_population VALUES (10, 1000), (11, 2000);
SELECT a.country_name, b.city_id, p.population
FROM countries AS a
INNER JOIN cities AS b ON a.country_id = b.country_id
INNER JOIN city_population AS p ON b.city_id = p.city_id
ORDER BY b.city_id;
country_name city_id population
A 10 1000
A 11 2000
For real tables, check the types on both sides of the join and consider indexes that serve the join and filters. The example indexes cities.country_id. Run EXPLAIN to see candidate indexes (possible_keys) and the chosen index (key). The optimizer may choose to scan a table this small. Closing the connection removes the temporary tables.
EXPLAIN
SELECT a.country_name, b.city_name
FROM countries AS a
LEFT JOIN cities AS b ON a.country_id = b.country_id
WHERE a.country_id = 1;




