Skip to content

Joins

The Art of Assembly: An Introduction to JOINs

Section titled “The Art of Assembly: An Introduction to JOINs”

In our last lesson on Normalization, we purposefully scattered our data across multiple tables for its own protection. We created a Customers table, a Products table, and an Orders table.

This is correct, but it creates a problem. The Orders table only contains a CustomerID. By itself, this is just a number; it is meaningless. The customer’s name, email, and other details are stored in the Customers table.

If you needed to produce a report showing the order date and the name of the customer who placed the order, you would be stuck. You have the date in one table and the name in another.

The JOIN is the mechanism that solves this problem. A JOIN clause combines rows from two or more tables based on a related column between them. It allows you to create a temporary, virtual table for the duration of your query that reassembles the normalized data into a useful, denormalized view.

To perform a meaningful join, you need two things:

  1. Two or more tables that you want to connect.
  2. A Join Condition: This is the rule that tells the database how to match rows from one table with rows from another. This condition is specified in the ON clause.

The most critical, reliable, and common join condition is the link between a PRIMARY KEY in one table and a FOREIGN KEY in another. This is the very reason we created these keys in the first place.

Basic Syntax:

SELECT
-- Columns from both tables
FROM
TableA
JOIN_TYPE -- (e.g., INNER JOIN, LEFT JOIN)
TableB
ON
TableA.PrimaryKey = TableB.ForeignKey; -- The Join Condition

We will explore each of these in great detail, but first, you must have a map of the territory. The different types of JOIN determine how the database handles rows that do not have a matching counterpart in the other table.


This is the most common type of join. It returns only the rows where the join condition is met in both tables. If a row in one table has no match in the other, it is excluded from the result.

  • Business Question: “Show me all customers who have placed orders.” or “List all products that have been sold.”

  • Mermaid Diagram (Venn):

    INNER JOIN

    Matching rows only

    Table A: Customers

    Table B: Orders


2. LEFT JOIN (or LEFT OUTER JOIN): All from the Left
Section titled “2. LEFT JOIN (or LEFT OUTER JOIN): All from the Left”

This join returns all rows from the left-hand table, and the matched rows from the right-hand table. If there is no match for a row from the left table, the columns from the right table will be filled with NULL.

  • Business Question: “Show me all customers, and if they have placed any orders, show those too.” This is the primary tool for finding things that are “missing” or “unmatched.”

  • Mermaid Diagram (Venn):

    LEFT JOIN

    All from A + Matches

    Table A: Customers

    Table B: Orders


3. RIGHT JOIN (or RIGHT OUTER JOIN): All from the Right
Section titled “3. RIGHT JOIN (or RIGHT OUTER JOIN): All from the Right”

This is the mirror image of a LEFT JOIN. It returns all rows from the right-hand table, and the matched rows from the left. If a row from the right has no match, the columns from the left will be NULL.

  • Interview Insight: In practice, RIGHT JOIN is used far less frequently than LEFT JOIN. Any RIGHT JOIN can be rewritten as a LEFT JOIN by simply swapping the order of the tables. Most developers find LEFT JOIN more intuitive to read.

  • Mermaid Diagram (Venn):

    RIGHT JOIN

    All from B + Matches

    Table A: Customers

    Table B: Orders


This join returns all rows when there is a match in either the left or the right table. It’s essentially a LEFT JOIN and a RIGHT JOIN combined. If a row from one table has no match, the other table’s columns will be NULL.

  • Interview Gold: MySQL does not have a native FULL OUTER JOIN keyword. This is a critical piece of knowledge. You must emulate it by performing a LEFT JOIN and a RIGHT JOIN and combining the results with a UNION operator.

  • Business Question: “Show me every single customer and every single order, linking them up where a relationship exists.”

  • Mermaid Diagram (Venn):

    FULL OUTER JOIN

    All rows from both

    Table A: Customers

    Table B: Orders


This join matches every row from the first table with every row from the second table. It does not use an ON clause. If Table A has 100 rows and Table B has 100 rows, the CROSS JOIN will produce 100 * 100 = 10,000 rows.

  • Interview Gold: This is often the result of an accidental mistake: writing a JOIN but forgetting the ON clause. In most scenarios, a CROSS JOIN is incorrect. However, it has niche uses, such as generating all possible combinations of data for a report (e.g., matching every employee with every training course to create a checklist).

  • Mermaid Diagram (Venn):

    CROSS JOIN

    Every row with every row

    Table A: Customers

    Table B: Orders