← Back to Curriculum

Joining Tables

šŸ“š Lesson 4 of 15 ā±ļø 45 min

Joining Tables

45 min

The `JOIN` clause combines rows from two or more tables based on related columns, enabling you to query data from multiple tables in a single query. JOINs are fundamental to relational databases, which store data across multiple normalized tables. Understanding JOINs is essential for working with relational databases effectively. JOINs enable you to combine related data without duplicating information.

`INNER JOIN` returns records that have matching values in both tables. Only rows with matching keys in both tables appear in results. INNER JOIN is the most common join type and is often the default when you say 'JOIN'. Understanding INNER JOIN enables you to combine related data from multiple tables. INNER JOIN excludes rows without matches in either table.

`LEFT JOIN` (or LEFT OUTER JOIN) returns all records from the left table and matching records from the right table. If there's no match, NULL values appear for right table columns. LEFT JOIN preserves all rows from the left table, making it useful for finding records that may or may not have related data. Understanding LEFT JOIN enables you to include all records from one table regardless of matches.

`RIGHT JOIN` (or RIGHT OUTER JOIN) returns all records from the right table and matching records from the left table. RIGHT JOIN is less common than LEFT JOIN but serves similar purposes. In practice, you can often rewrite RIGHT JOINs as LEFT JOINs by swapping table order. Understanding RIGHT JOIN helps you work with queries that use it.

`FULL OUTER JOIN` returns all records when there's a match in either table. If there's no match, NULL values appear for columns from the table without a match. FULL OUTER JOIN combines the behavior of LEFT and RIGHT JOINs. Understanding FULL OUTER JOIN enables you to find all records from both tables, regardless of matches.

`CROSS JOIN` returns the Cartesian product of two tables—every row from the first table combined with every row from the second table. CROSS JOINs can produce very large result sets and are rarely used intentionally. Understanding CROSS JOIN helps you avoid accidental Cartesian products. Best practices include using explicit JOIN syntax, understanding join conditions, and choosing the appropriate join type for your needs.

Key Concepts

  • JOIN combines rows from multiple tables based on related columns.
  • INNER JOIN returns only matching records from both tables.
  • LEFT JOIN returns all left table records and matching right table records.
  • RIGHT JOIN returns all right table records and matching left table records.
  • FULL OUTER JOIN returns all records from both tables.

Learning Objectives

Master

  • Using INNER JOIN to combine related data
  • Using LEFT JOIN to include all records from one table
  • Understanding different join types and when to use them
  • Writing join conditions correctly

Develop

  • Relational database thinking
  • Understanding how to combine data from multiple tables
  • Designing efficient join queries

Tips

  • Use explicit JOIN syntax (INNER JOIN, LEFT JOIN) instead of comma-separated tables.
  • Always specify join conditions—forgetting them causes Cartesian products.
  • Use table aliases to make queries more readable.
  • Understand which join type you need—INNER vs LEFT makes a big difference.

Common Pitfalls

  • Forgetting join conditions, causing Cartesian products (huge result sets).
  • Using wrong join type, excluding or including unintended rows.
  • Not using table aliases, making queries hard to read.
  • Joining on wrong columns, producing incorrect results.

Summary

  • JOINs combine data from multiple tables based on relationships.
  • INNER JOIN returns only matching records.
  • LEFT JOIN preserves all left table records.
  • Understanding join types enables effective data combination.
  • JOINs are essential for working with relational databases.

Exercise

Write a query to select order details by joining the `Orders` and `Customers` tables on the `CustomerID` field.

SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

Code Editor

Output