MySQL
Joins
SQL
Database
Duplicate Content

MySQL Quick breakdown of the types of joins

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Joins are the core mechanism for combining related rows across tables in MySQL. Most query bugs around missing rows or duplicate rows come from choosing the wrong join type, not from syntax mistakes. A clear mental model of join behavior makes query output predictable and easier to debug.

Join Types And When To Use Them

INNER JOIN returns rows where the join predicate matches on both sides. Use it when unmatched rows are irrelevant.

LEFT JOIN returns all rows from the left table and matched rows from the right table. Use it when the left side is mandatory and right side data is optional.

RIGHT JOIN is the mirror of left join, but most teams avoid it for readability and instead swap table order and use left join.

CROSS JOIN produces a Cartesian product. It is useful for generating combinations, but it can explode row count quickly.

MySQL does not support FULL OUTER JOIN directly. To emulate it, combine a left join result with unmatched right side rows using UNION ALL.

Practical Schema And Query Examples

Suppose you have orders and customers.

sql
1CREATE TABLE customers (
2  customer_id INT PRIMARY KEY,
3  name VARCHAR(100) NOT NULL
4);
5
6CREATE TABLE orders (
7  order_id INT PRIMARY KEY,
8  customer_id INT,
9  total DECIMAL(10,2) NOT NULL,
10  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
11);
12
13INSERT INTO customers (customer_id, name) VALUES
14  (1, 'Ana'),
15  (2, 'Ben'),
16  (3, 'Cara');
17
18INSERT INTO orders (order_id, customer_id, total) VALUES
19  (101, 1, 55.00),
20  (102, 1, 20.00),
21  (103, 2, 99.00);

Get orders with customer names by using inner join.

sql
1SELECT o.order_id, c.name, o.total
2FROM orders AS o
3INNER JOIN customers AS c
4  ON c.customer_id = o.customer_id;

List all customers even if they never ordered by using left join.

sql
1SELECT c.customer_id, c.name, o.order_id, o.total
2FROM customers AS c
3LEFT JOIN orders AS o
4  ON o.customer_id = c.customer_id
5ORDER BY c.customer_id, o.order_id;

Customer Cara appears with NULL order columns, which is expected and useful for reporting.

Full Outer Join Emulation In MySQL

When analysts ask for every row from both sides including unmatched rows, emulate full outer join.

sql
1SELECT c.customer_id, c.name, o.order_id, o.total
2FROM customers AS c
3LEFT JOIN orders AS o
4  ON o.customer_id = c.customer_id
5UNION ALL
6SELECT c.customer_id, c.name, o.order_id, o.total
7FROM orders AS o
8LEFT JOIN customers AS c
9  ON c.customer_id = o.customer_id
10WHERE c.customer_id IS NULL;

The first query keeps all customers. The second query adds orders that have no matching customer row. The filter prevents duplicates for already matched rows.

Performance And Readability Guidance

Always join on indexed columns when possible. For one to many relationships, ensure the foreign key column is indexed, because MySQL can then avoid expensive scans. Review execution plans with EXPLAIN before and after changes.

Be explicit about selected columns rather than using SELECT *. This prevents accidental column collisions and makes downstream code less fragile. Consistent alias naming such as c for customers and o for orders makes larger queries easier to maintain.

Common Pitfalls

  • Expecting left join to remove duplicates. Duplicates often come from one to many relationships.
  • Filtering right table columns in WHERE after a left join, which can effectively turn it into an inner join.
  • Forgetting that MySQL has no native full outer join syntax.
  • Joining on non unique business names instead of stable keys.
  • Ignoring EXPLAIN and discovering performance regressions in production.

Summary

  • INNER JOIN keeps only matched rows.
  • LEFT JOIN keeps all left rows and optional right rows.
  • CROSS JOIN creates combinations and must be used carefully.
  • Full outer join behavior in MySQL requires a UNION ALL pattern.
  • Correct join type plus indexed keys prevents both logic bugs and slow queries.

Course illustration
Course illustration

All Rights Reserved.