Introduction to Joins

In this lesson, you'll learn about SQL JOINs, a fundamental concept for combining data from multiple tables. You'll understand how to connect related information stored in different tables to perform powerful data analysis. By the end of this lesson, you'll be able to retrieve comprehensive information using various types of JOINs.

Learning Objectives

  • Define what a JOIN is and why it's used.
  • Understand the different types of JOINs: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
  • Write SQL queries using INNER JOIN to retrieve related data from two or more tables.
  • Identify the appropriate JOIN type based on the desired result set.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to JOINs

Databases often store related information in separate tables. For example, a database for an online store might have a 'Customers' table and an 'Orders' table. The 'Orders' table might contain a 'customer_id' column that links back to the 'Customers' table. A JOIN allows you to combine data from these related tables into a single result set. Think of it like merging two spreadsheets based on a shared column.

Key Concepts:
* Relational Databases: Databases designed to store data with relationships.
* Primary Key: A unique identifier for each row in a table (e.g., customer_id in Customers table).
* Foreign Key: A column in one table that refers to the primary key of another table (e.g., customer_id in Orders table).

INNER JOIN

The INNER JOIN is the most common type of JOIN. It returns only the rows where there is a match in both tables based on the join condition (usually matching primary and foreign keys).

Syntax:

SELECT column_names
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

Example:
Let's imagine two tables: Customers and Orders.

Customers Table:
| customer_id | customer_name | email |
|-------------|---------------|-------------------|
| 1 | Alice | alice@email.com |
| 2 | Bob | bob@email.com |
| 3 | Charlie | charlie@email.com |

Orders Table:
| order_id | customer_id | order_date | total_amount |
|----------|-------------|------------|--------------|
| 101 | 1 | 2023-11-15 | 50.00 |
| 102 | 2 | 2023-11-16 | 75.00 |
| 103 | 1 | 2023-11-17 | 25.00 |

Query:

SELECT c.customer_name, o.order_id, o.order_date, o.total_amount
FROM Customers c
INNER JOIN Orders o
ON c.customer_id = o.customer_id;

Result:
| customer_name | order_id | order_date | total_amount |
|---------------|----------|------------|--------------|
| Alice | 101 | 2023-11-15 | 50.00 |
| Bob | 102 | 2023-11-16 | 75.00 |
| Alice | 103 | 2023-11-17 | 25.00 |

Notice that the result only includes customers who have placed orders.

Other JOIN Types (Brief Overview)

While INNER JOIN is crucial, understanding the other types is also important:

  • LEFT JOIN: Returns all rows from the 'left' table (the table listed before LEFT JOIN) and matching rows from the 'right' table. If there's no match in the right table, it fills the columns from the right table with NULL values.
  • RIGHT JOIN: Similar to LEFT JOIN, but returns all rows from the 'right' table and matching rows from the 'left' table. NULL values are used if there's no match in the left table.
  • FULL OUTER JOIN: Returns all rows from both tables. If there's no match in either table, the missing columns are filled with NULL. (Note: Not all SQL databases support FULL OUTER JOIN directly).

We'll cover these in detail in a later lesson.

Progress
0%