# Joins

{% hint style="info" %}
Since solving any reasonable SQL problem requires a combination of all the topics covered here, hence it becomes difficult to seggregate problems based on one topic alone. So for SQL we are creating a dedicated [**Problems**](/sql/problems.md) section. Theoritical and Basic questions will still be under their dedicated sections.
{% endhint %}

Joins are best explained using Venn diagrams

<figure><img src="/files/qwzqmFkidnPKF2BMmmaL" alt=""><figcaption><p>TSQL JOIN Types, <a href="https://stevestedman.com/2015/05/tsql-join-types-poster-version-4-1/">Reference</a></p></figcaption></figure>

Remember that in case of multiple joins each single join produces a **single derived table** that is then joined to the next table and so on.

## Questions

<details>

<summary>Address of People</summary>

**Reference -** [**Leetcode**](https://leetcode.com/problems/combine-two-tables/)

<pre><code><strong>Table: Person
</strong>
| Column Name | Type    |
|-------------|---------|
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |

PersonId is the primary key column for this table.

Table: Address


| Column Name | Type    |
|-------------|---------|
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |

AddressId is the primary key column for this table.
</code></pre>

Write a SQL query for a report that provides the following information for each person in the Person table, regardless of if there is an address for each of those people:

FirstName, LastName, City, State

**Answer**

```sql
select a.FirstName, a.LastName, b.City, b.State
from Person a
left join Address b
on a.PersonId = b.PersonID
```

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.thedatascienceinterviewproject.com/sql/joins.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
