All ArticlesSchemaVis logo

SQL JOINs Demystified: Inner, Outer, Left, Right and Cross

A
Akshat Kotpalliwar
7 min read

In relational databases, data is distributed across multiple normalized tables. To assemble meaningful information, you must combine data from these tables. This is achieved using SQL JOIN operations. A JOIN clause combines rows from two or more tables based on a related column between them.

Understanding the mechanical differences between join types is essential for writing accurate and performant SQL queries.

INNER JOIN

An INNER JOIN returns only the rows where there is a match in both tables based on the join condition. It is the most common and generally the fastest join operation.

SELECT customers.name, orders.order_date FROM customers INNER JOIN orders ON customers.id = orders.customer_id;

If a customer has no orders, they will not appear in the result set. If an order has a null customer_id, it will also be excluded.

LEFT JOIN (LEFT OUTER JOIN)

A LEFT JOIN returns all rows from the left table (the one specified before the JOIN keyword), and the matched rows from the right table. If there is no match, the result will contain NULL values for the right table's columns.

SELECT customers.name, orders.order_date FROM customers LEFT JOIN orders ON customers.id = orders.customer_id;

This query returns every customer. If a customer has placed orders, the order_date is shown. If a customer has never placed an order, they still appear in the results, but the order_date will be NULL.

RIGHT JOIN (RIGHT OUTER JOIN)

A RIGHT JOIN is the conceptual mirror of a LEFT JOIN. It returns all rows from the right table, and the matched rows from the left table. Unmatched rows from the left table will result in NULLs.

In practice, RIGHT JOIN is rarely used because any query using it can be rewritten as a LEFT JOIN simply by reversing the order of the tables. Sticking to LEFT JOIN improves query readability across teams.

FULL OUTER JOIN

A FULL OUTER JOIN returns all rows when there is a match in either the left or right table. It is essentially a combination of a LEFT JOIN and a RIGHT JOIN. Where there is no match, the missing side will contain NULL values.

This join is computationally expensive and used less frequently, typically for reconciling missing data between two distinct datasets. Note: MySQL does not natively support FULL OUTER JOIN; you must use a UNION of a LEFT and RIGHT join.

CROSS JOIN

A CROSS JOIN returns the Cartesian product of the two tables. This means it joins every row of the left table with every row of the right table. It does not use an 'ON' condition.

SELECT colors.name, sizes.name FROM colors CROSS JOIN sizes;

If you have 10 colors and 5 sizes, a CROSS JOIN produces 50 rows (every possible combination). Use this with extreme caution on large tables, as the result set size grows exponentially (N * M).

Performance Considerations

  • Always ensure that the columns used in your ON clauses (typically foreign keys) are indexed.
  • Filter data as early as possible. Applying a WHERE clause to reduce the dataset before the database engine performs the join can drastically improve execution time.

Related Topics

SQLJOININNER JOINLEFT JOINDatabase QuerySQL ServerPostgreSQL

Visualize your schemas effortlessly

Stop writing manual DDL or guessing relationships. Drop your SQL file into SchemaVis and instantly generate an interactive diagram.

Try SchemaVis for Free