A left outer join returns all rows from your left table, plus matching rows from your right table

A left outer join is a way to combine data from two tables in SQL. It keeps every row from the table on the left side of the join, and adds columns from the right table only when there is a match. If there is no match, the columns from the right table show as blank (NULL).

The key difference from an inner join is that an inner join only returns rows where both tables have matching data. A left outer join returns everything from the left table regardless of whether the right table has a match. This makes it useful when you want to see all records from one table and fill in details from another table when they exist.

Key Takeaways

  • A left outer join keeps all rows from the left table and adds matching data from the right table when it exists.
  • Rows from the left table with no match in the right table will show NULL (blank) values for the right table's columns.
  • The syntax is SELECT * FROM left_table LEFT OUTER JOIN right_table ON left_table.id = right_table.id.
  • Use a left outer join when you need all records from one table but only want to add extra information from another table when available.

The basic syntax and how to read it

The structure of a left outer join looks like this:

SELECT * FROM left_table LEFT OUTER JOIN right_table ON left_table.id = right_table.id

Break this down: SELECT * means you want all columns. FROM left_table is your starting table — the one that will keep all its rows. LEFT OUTER JOIN right_table is the table you are adding data from. ON left_table.id = right_table.id is the condition that tells SQL how to match rows between the two tables. The ON clause looks for rows where the id column in the left table matches the id column in the right table.

You can also write LEFT JOIN instead of LEFT OUTER JOIN — they mean the same thing. Most people use the shorter version. You can replace the asterisk (*) with specific column names if you do not want all columns, and you can change the ON condition to match on any column that makes sense for your data.

A concrete example with real data

Imagine you have a customers table with customer IDs and names, and an orders table with order IDs and customer IDs. You want to see every customer and show their orders if they have any.

Your customers table has three rows: Customer 1 (Alice), Customer 2 (Bob), Customer 3 (Carol). Your orders table has two rows: Order A for Customer 1, Order B for Customer 1. Customer 2 and Customer 3 have no orders.

If you run SELECT customers.name, orders.order_id FROM customers LEFT OUTER JOIN orders ON customers.id = orders.customer_id, you get four rows back: Alice with Order A, Alice with Order B, Bob with NULL (no order), Carol with NULL (no order). The left outer join kept all three customers and added order information only where it existed.

If you had used an inner join instead, you would only get two rows — Alice with Order A and Alice with Order B. Bob and Carol would disappear because they have no matching orders. A left outer join prevents that loss of data.

When to use a left outer join instead of other joins

Use a left outer join when the left table represents your primary data and the right table is supplementary. For example, if you are pulling a list of all employees and want to show their department information, use a left outer join. Every employee stays in the result, and those without a department assignment show NULL.

Use an inner join when you only care about rows that exist in both tables. If you want to see only employees who have a department assignment, an inner join is faster and cleaner.

Use a right outer join when the right table is your primary data. This is less common because you can always rewrite it as a left outer join by swapping the table order. Use a full outer join when you want all rows from both tables, matching where possible — but note that not all database systems support full outer join (MySQL does not, but PostgreSQL and SQL Server do).

Handling NULL values in your results

When a left outer join finds no match, the columns from the right table contain NULL. NULL is not the same as zero or an empty string — it means the value is unknown or does not exist. If you want to display something other than blank, use the COALESCE or IFNULL function to replace NULL with a default value.

For example, SELECT customers.name, COALESCE(orders.order_id, 'No orders') FROM customers LEFT OUTER JOIN orders ON customers.id = orders.customer_id will show "No orders" instead of a blank cell for customers with no matches.

You can also filter out rows with NULL if you want to see only matches. Adding WHERE orders.order_id IS NOT NULL at the end turns your left outer join into the same result as an inner join — it removes all the unmatched rows from the left table. This is useful if you realize partway through that you actually wanted an inner join.

Common mistakes and how to avoid them

The most common mistake is getting the table order backwards. Remember: the table after FROM is the left table, and the table after LEFT OUTER JOIN is the right table. If you swap them, you change which table's rows are kept. Swapping the order and changing LEFT to RIGHT gives the same result, but it is confusing — stick with one direction.

Another mistake is using the wrong column in the ON clause. If you write ON left_table.customer_id = right_table.order_id, SQL will try to match customer IDs to order IDs, which makes no sense. Check that both sides of the ON condition refer to columns that actually relate to each other.

A third mistake is forgetting that NULL is not a value you can compare with =. If you want to find rows where the right table had no match, use IS NULL, not = NULL. Writing WHERE right_table.id = NULL returns zero rows because NULL comparisons always fail in SQL.

Performance considerations for large tables

Left outer joins can be slow on very large tables if the ON condition is not indexed. If you are joining on a column that is not a primary key or does not have an index, the database has to scan the entire right table for each row in the left table. Adding an index to the join column on the right table usually fixes this.

If you are joining many tables together, the order matters for performance. Most databases optimize left outer joins reasonably well, but you can help by joining the largest table first (as the left table) and smaller lookup tables on the right. Test your query with EXPLAIN or EXPLAIN PLAN to see how the database is executing it.

Frequently Asked Questions

What is the difference between LEFT OUTER JOIN and LEFT JOIN?

They are identical. OUTER is optional in the syntax. Most people write LEFT JOIN because it is shorter, but LEFT OUTER JOIN is equally correct and sometimes used for clarity.

Can I left outer join more than two tables?

Yes. You can chain multiple left outer joins together: SELECT * FROM table1 LEFT OUTER JOIN table2 ON table1.id = table2.id LEFT OUTER JOIN table3 ON table2.id = table3.id. Each join keeps all rows from the previous result and adds matching data from the next table.

Why do I get duplicate rows when I use a left outer join?

If the right table has multiple rows that match a single row in the left table, you get one result row for each match. If Customer 1 has three orders, a left outer join returns three rows for Customer 1 (one per order). This is correct behavior — use GROUP BY and aggregate functions if you want to combine them.

Is a left outer join slower than an inner join?

Not significantly. Both scan the same tables and use the same indexes. A left outer join may be slightly slower because it has to handle NULL values, but the difference is usually too small to notice unless you are working with millions of rows.

What happens if the ON condition matches zero rows?

The left table row still appears in the result, with NULL values for all columns from the right table. This is the defining feature of a left outer join — it keeps rows from the left table even when there is no match.