Joins

https://youtu.be/nivyaiCeWjs?si=HgD2tcSnN7KBZs4h

Here’s a complete guide based on the Decomplexify video on SQL Joins, covering everything from INNER JOIN to CROSS JOIN — using practical examples like chemical compounds, elements, and even animals and employees.


🧩 SQL Joins Explained Simply (Based on Decomplexify)

This guide explains how joins allow us to combine information from multiple tables in SQL to answer meaningful questions.


🔗 Why Use Joins?

Relational databases split data across tables. To answer real-world questions (e.g., "Which compounds contain hydrogen?"), we must combine data from:

  • A Compound table (e.g., Water)

  • An Element table (e.g., Hydrogen)

  • A Compound_Element_Details table (e.g., Water contains 2 H, 1 O)

To connect these, SQL joins come in handy.


🔍 INNER JOIN

🔸 What It Does:

Returns rows where matching data exists in both (or all) tables.

🧪 Example:

To find compounds that contain "Hydrogen":

  1. Join Compound_Element_Details with Compound on Compound_ID

  2. Join that result with Element on Element_Symbol

  3. Filter where Element_Name = 'Hydrogen'

SELECT c.Compound_ID, c.Compound_Name
FROM Compound_Element_Details ced
INNER JOIN Compound c ON ced.Compound_ID = c.Compound_ID
INNER JOIN Element e ON ced.Element_Symbol = e.Element_Symbol
WHERE e.Element_Name = 'Hydrogen';

✅ Key Benefit:

Only rows with complete matches in all tables appear in the result. If an element or compound doesn’t match, it’s excluded.


🔍 LEFT OUTER JOIN (a.k.a. LEFT JOIN)

🔸 What It Does:

Returns all rows from the left table, and matched rows from the right. If no match, NULLs are shown.

🌡 Example:

To find elements that do NOT belong to any compound:

SELECT e.Element_Name
FROM Element e
LEFT JOIN Compound_Element_Details ced 
ON e.Element_Symbol = ced.Element_Symbol
WHERE ced.Compound_ID IS NULL;

✅ Key Benefit:

Preserves unmatched rows from the left table (e.g., Helium).


🔍 RIGHT OUTER JOIN (a.k.a. RIGHT JOIN)

🔸 What It Does:

The reverse of LEFT JOIN — returns all rows from the right table, with matched rows from the left.

ℹ️ Practical Tip:

You rarely need RIGHT JOIN. You can rewrite a RIGHT JOIN as a LEFT JOIN by switching the tables.


🔍 FULL OUTER JOIN

🔸 What It Does:

Returns all rows from both tables — matched and unmatched. Unmatched rows show NULL for the missing parts.

🧪 Example:

If you do a FULL OUTER JOIN between Element and Compound_Element_Details, you’ll get:

  • Elements used in compounds ✅

  • Elements NOT used in any compound (e.g., Helium) ✅

  • Invalid entries (e.g., Element_Symbol 'Q' or 'X') ✅

✅ Key Benefit:

You get the complete picture, even if data is missing on either side.


🔍 CROSS JOIN (Cartesian Product)

🔸 What It Does:

Returns every possible pair of rows between two tables.

🧍‍♀️🦊 Example:

If you have 3 people and 3 animals, a cross join will return 9 combinations.

SELECT p.Name, a.Species
FROM Person p
CROSS JOIN Animal a;

No ON clause is required. Each row from the first table matches with every row from the second.


🔁 Self Join

🔸 What It Does:

Joins a table to itself, useful when rows in a table are related to other rows in the same table.

👩‍💼 Example: Employees and Approvers

Suppose each employee has an approver who is also an employee.

SELECT e.Employee_Name, ap.Employee_Name AS Approver
FROM Employee e
INNER JOIN Employee ap 
ON e.Approver_Employee_ID = ap.Employee_ID;
  • e and ap are aliases for two "versions" of the same table.

  • Olivia can approve her own requests (self-reference).


🧾 Summary of SQL Join Types

Join Type

Returns

INNER JOIN

Only rows with matching values in both tables

LEFT OUTER JOIN

All rows from the left table; NULLs for no-match from the right

RIGHT OUTER JOIN

All rows from the right table; NULLs for no-match from the left

FULL OUTER JOIN

All rows from both tables; unmatched rows get NULLs

CROSS JOIN

Every possible combination of rows (Cartesian Product)

SELF JOIN

Matches rows within the same table


✅ Best Practices

  • Use INNER JOIN for most situations where both sides must match.

  • Use LEFT JOIN when one side may not have matches, and you still want to keep its data.

  • Avoid RIGHT JOIN unless necessary — rewrite it as a LEFT JOIN.

  • Use FULL JOIN sparingly — only when unmatched rows from both sides are needed.

  • Use CROSS JOIN carefully — especially with large tables (can explode rows).

  • Use SELF JOIN for hierarchy or peer relationships (e.g., managers, approvals).


Let me know if you’d like this guide turned into a PDF cheat sheet or if you want to practice writing queries with mock data!

Updated on