Conceito introdutório

~12 min de leitura

Introduction to JOINs

Learn how to combine data from multiple tables based on relationships between them.

Intermediário

What are JOINs?

JOIN operations let you combine rows from two or more tables based on a related column between them. This is how you work with normalized databases where data is split across multiple tables.

Think of it like connecting puzzle pieces: A "customers" table has customer info, and an "orders" table has order details. JOINs let you combine them to see "which customer placed which order."

Why Do We Need JOINs?

📊

Data Normalization

Databases split data into multiple tables to avoid duplication. Instead of storing customer address in every order, we store it once in the customers table and reference it.

🔗

Relationships

Tables are connected through foreign keys (like customer_id in orders table references id in customers table). JOINs follow these relationships.

🎯

Comprehensive Views

Get complete information by combining related data. For example, see customer name alongside their order history, even though they're in different tables.

Efficient Queries

JOINs let you query across multiple tables in a single statement, which is much more efficient than making multiple separate queries.

Basic JOIN Syntax

SQL Syntax
SELECT table1.column, table2.column
FROM table1
JOIN table2
  ON table1.common_column = table2.common_column;

💡 Pro Tip: The ON clause specifies how tables are related. It's usually matching a primary key (id) in one table to a foreign key (customer_id) in another.

Visualize JOINs

See how different JOIN types work! Switch between INNER, LEFT, RIGHT, and FULL OUTER JOINs to understand which rows are included in the result.

INNER JOIN: Returns only rows with matching values in both tables

💡 Tip: The join columns (id in customers, customer_id in orders) are highlighted with colored badges. Rows with matching values connect between tables.

customers

id:1
name:Alice
id:2
name:Bob
id:3
name:Carlos
id:4
name:Diana
id:5
name:Emma

orders

order_id:101
customer_id:1
amount:1200
order_id:102
customer_id:1
amount:25
order_id:103
customer_id:2
amount:75
order_id:104
customer_id:3
amount:350
order_id:105
customer_id:3
amount:120
order_id:106
customer_id:4
amount:150
order_id:107
customer_id:99
amount:500
Matched rows (included)
Unmatched (included with NULL)
Excluded from result
SQL Query
SELECT *
FROM customers
INNER JOIN orders
  ON customers.id = orders.customer_id
Result: 6 row(s)

💡 Key Insights:

  • Emma (id: 5) has no orders - demonstrates LEFT JOIN (includes Emma with NULL)
  • Order 107 (customer_id: 99) has no matching customer - demonstrates FULL JOIN difference
  • INNER JOIN excludes both Emma and order 107
  • LEFT JOIN includes Emma but excludes order 107
  • FULL JOIN includes both Emma and order 107

Common Patterns

Return only rows that have matching values in both tables

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

Result: Shows customers who have placed orders (excludes customers with no orders)

Types of JOINs

INNER JOIN

Returns only rows where there's a match in both tables.

SELECT * FROM customers INNER JOIN orders ON customers.id = orders.customer_id

✅ Use when: You only want rows with matching data on both sides (e.g., customers who have orders)

LEFT JOIN (LEFT OUTER JOIN)

Returns all rows from the left table, plus matching rows from the right table. If no match, right side shows NULL.

SELECT * FROM customers LEFT JOIN orders ON customers.id = orders.customer_id

✅ Use when: You want all records from the first table, even if they don't have matches (e.g., all customers, including those without orders)

RIGHT JOIN (RIGHT OUTER JOIN)

Returns all rows from the right table, plus matching rows from the left table. If no match, left side shows NULL. (Less commonly used - can rewrite as LEFT JOIN)

SELECT * FROM orders RIGHT JOIN customers ON orders.customer_id = customers.id

✅ Use when: You want all records from the second table (rarely needed - prefer LEFT JOIN with tables swapped)

FULL OUTER JOIN

Returns all rows from both tables. Where there's a match, combines them. Where there's no match, shows NULL on the missing side.

SELECT * FROM customers FULL OUTER JOIN orders ON customers.id = orders.customer_id

✅ Use when: You want all records from both tables (e.g., all customers AND all orders, even orphaned ones)

Table Aliases & Best Practices

Using Table Aliases

Table aliases make JOINs more readable by shortening table names:

❌ Without aliases (verbose):

SELECT customers.name, orders.amount
FROM customers
JOIN orders
ON customers.id = orders.customer_id

✅ With aliases (clean):

SELECT c.name, o.amount
FROM customers c
JOIN orders o
ON c.id = o.customer_id

USING Clause Shortcut

When the join column has the same name in both tables, you can use USING instead of ON:

Standard ON syntax:

JOIN orders
ON customers.customer_id = orders.customer_id

Shorter USING syntax:

JOIN orders
USING (customer_id)

Common Mistakes to Avoid

❌ Forgetting the ON clause

SELECT * FROM customers JOIN orders;

This creates a CROSS JOIN (every customer paired with every order)! Add: ON customers.id = orders.customer_id

❌ Ambiguous column names

SELECT id FROM customers JOIN orders ON ...

Both tables have `id`! Specify: customers.id or orders.id

❌ Using INNER JOIN when you need LEFT JOIN

INNER JOIN -- Excludes customers without orders!

If you want ALL customers: LEFT JOIN orders

❌ Wrong join condition

ON customers.name = orders.customer_name

Join on IDs, not names! Names can be duplicated or misspelled: ON customers.id = orders.customer_id

✅ Correct JOIN with aliases and proper ON clause

SELECT c.name, o.amount, o.order_date FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.amount > 100;

Perfect! Clear aliases, correct join type, proper ON clause, additional WHERE filter.

Pratique a seguirComeçar os desafios →