> For the complete documentation index, see [llms.txt](https://cybersecurity-cloud-and-it-notes.gitbook.io/kyles-cybersecurity-cloud-and-it-gitbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cybersecurity-cloud-and-it-notes.gitbook.io/kyles-cybersecurity-cloud-and-it-gitbook/google-cybersecurity-professional-cert/4.-linux-and-sql-tool-of-the-trade/module-4/sql-joins/join-tables-in-sql.md).

# Join tables in SQL

Here’s a fun and ADHD-friendly breakdown of **joining tables in SQL**!

***

#### **🧩 Joining Tables: Combining Powers!**

Imagine you’ve got two puzzle pieces, and they fit perfectly together. That’s what **joining tables** does in SQL! You can combine info from two different tables to get a complete picture—like finding out which machines are vulnerable based on their operating system. Cool, right?

***

#### **🔗 How to Join Tables: The Basics**

SQL needs to know **which column** connects the two tables, and we tell it exactly where to look using a **join**.

1. If you have a column like **employee\_id** in both the **employees** and **machines** tables, you need to tell SQL **which** employee\_id you’re talking about.
2. Use this format:\
   `table_name.column_name`
   * **Example:** `employees.employee_id` or `machines.employee_id`.

***

#### **💡 Primary Key vs. Foreign Key**

* **Primary Key**: A column with **unique** values in each row (like a special ID).
  * **Example:** `employee_id` in the **employees** table.
* **Foreign Key**: A column that **links** to a primary key from another table.
  * **Example:** `employee_id` in the **machines** table.

***

#### **🔀 INNER JOIN: Matching Rows Only!**

An **INNER JOIN** gives you rows that match between two tables based on a **common column**. If there’s no match, it won’t include those rows.

* **Example:** Join the **employees** and **machines** tables to see which employees are using which machines:

```
SELECT username, office, operating_system
FROM employees
INNER JOIN machines
ON employees.employee_id = machines.employee_id;
```

* This query finds matching **employee\_id** values in both tables and gives you the **username, office, and operating system** for those matches!

***

#### **📝 Breaking Down the INNER JOIN Query**

* **INNER JOIN**: Tells SQL to match rows between two tables.
* **employees**: The first (or left) table.
* **machines**: The second (or right) table.
* **ON employees.employee\_id = machines.employee\_id**: This is how you tell SQL to match the two tables based on the **employee\_id** column.

***

#### **🤔 What About NULL Values?**

**NULL** means missing data. For example, some machines might not be assigned to any employee, so the **employee\_id** is missing. In an **INNER JOIN**, those rows won’t show up because they don’t have a match.

***

#### **💥 What Did We Just Do?**

* We **joined** the **employees** and **machines** tables to get a combined list of who’s using which machine.
* We used an **INNER JOIN** to match rows where the **employee\_id** exists in both tables.
* SQL only gave us the columns we **selected**—**username, office, and operating system**.

***

#### **🎯 Key Takeaways:**

* **INNER JOIN** only gives you rows that **match** between two tables.
* You need to specify **which columns** to match on, like `employee_id`.
* If there’s no match (NULL), the row won’t show up in the result.

***

Now you’re ready to **join tables** and unlock a whole new level of SQL power! 🎉 Next up, you’ll learn about other types of joins—stay tuned for more SQL magic! 🚀
