Skip to content

Commit

Permalink
add crosslinks
Browse files Browse the repository at this point in the history
  • Loading branch information
grpatel committed Apr 26, 2024
1 parent 3ae03ed commit 09ba455
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions content/03-types/02-relational/07-what-are-joins-in-sql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ JOINs allow you to combine related data from multiple tables in relational datab

## How data is structured in relational databases

Before diving into JOINs, it’s important to understand the fundamentals of relational databases. Relational databases provide a way to structure and organize information. In relational databases, data is structured into _tables_, with _columns_ (attributes) specifying _data types_ (think strings, integers, etc.) and _rows_ containing records (the values for each of the table’s columns).
Before diving into JOINs, it’s important to understand the fundamentals of [relational databases](/intro/database-glossary#relational-database). Relational databases provide a way to structure and organize information. In relational databases, data is structured into _tables_, with _columns_ (attributes) specifying _data types_ (think strings, integers, etc.) and _rows_ containing records (the values for each of the table’s columns).

Let’s demonstrate this concept with a table holding customer information. In the table below, the columns, or customer attributes, are `name`, `age`, and `email address` and the rows represent each customer’s `name`, `age`, and `email_address`.

Expand All @@ -24,7 +24,7 @@ Let’s demonstrate this concept with a table holding customer information. In t
| Adrian | 28 | [email protected] |
| Synniva | 25 | [email protected] |

A relational database has the ability to define relationships, or connections, between different tables using _primary_ and _foreign keys_.
A relational database has the ability to define relationships, or connections, between different tables using [_primary_](/intro/database-glossary#primary-key) and [_foreign keys_](/intro/database-glossary#foreign-key).

It is common for tables in a relational database to have a column known as the _primary key_ which uniquely identifies each row. To see how this works in practice, let’s use our previous example: The primary key for the `customers` table would be a column of unique IDs for each customer, called `customer_id`. In other words, no customer would share the same ID as another.

Expand All @@ -36,7 +36,7 @@ A _foreign key_ is used to create a relationship between tables by referencing t

## What are JOINs?

A useful feature of relational databases is the concept of _JOINs_, a type of SQL operation that combines relevant data from distinct tables often based on the primary and foreign key.
A useful feature of relational databases is the concept of [_JOINs_](/intro/database-glossary#join), a type of SQL operation that combines relevant data from distinct tables often based on the primary and foreign key.

The basic syntax of a _JOIN_ operation is as follows:

Expand Down Expand Up @@ -102,7 +102,7 @@ The most common form of JOIN operations you will encounter are `INNER JOIN`, `LE

#### `INNER JOIN`

The `INNER JOIN` is the default JOIN and returns rows from both tables only where there is a match. Here is the associated query for the `customers` and `orders` table:
The [`INNER JOIN`](/intro/database-glossary#inner-join) is the default JOIN and returns rows from both tables only where there is a match. Here is the associated query for the `customers` and `orders` table:

```sql
SELECT
Expand All @@ -127,7 +127,7 @@ When depicted as a Venn diagram, an `INNER JOIN` represents the overlapping regi

#### `LEFT JOIN`

A `LEFT JOIN` returns all rows found using the `INNER JOIN` and all records from the first table. Here is the associated query for the `customers` and `orders` table:
A [`LEFT JOIN`](/intro/database-glossary#left-join) returns all rows found using the `INNER JOIN` and all records from the first table. Here is the associated query for the `customers` and `orders` table:

```sql
SELECT
Expand All @@ -153,7 +153,7 @@ When depicted as a Venn diagram, a `LEFT JOIN` represents the entire left circle

#### `RIGHT JOIN`

A `RIGHT JOIN` returns all rows found using the `INNER JOIN` and all records from the second table*.* Here is the associated query for the `customers` and `orders` table:
A [`RIGHT JOIN`](/intro/database-glossary#right-join) returns all rows found using the `INNER JOIN` and all records from the second table*.* Here is the associated query for the `customers` and `orders` table:

```sql
SELECT
Expand Down Expand Up @@ -249,6 +249,7 @@ To learn more about database-specific details of JOIN operations, check out thes
Lateral JOINs offer a different syntax to combining tables compared to traditional JOINs. In a `LATERAL JOIN`, the second table is presented as a _subquery_, and the JOIN criteria is defined within the `WHERE` clause of the subquery.

According to the [PostgreSQL](https://www.postgresql.org/docs/9.3/sql-select.html#SQL-FROM) docs:

> “The `LATERAL` key word can precede a sub-`SELECT FROM` item. This allows the sub-`SELECT` to refer to columns of `FROM` items that appear before it in the `FROM` list. (Without `LATERAL`, each sub-`SELECT` is evaluated independently and so cannot cross-reference any other `FROM` item.)”
In other words, a `LATERAL JOIN` is like a foreach loop, where PostgreSQL iterates over each row in a result set and uses each row to evaluate the subquery.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors: ['justinellingwood']

Splitting related data into separate tables can be beneficial from the standpoint of consistency, flexibility, and certain types of performance. However, you still need a reasonable way of reintegrating records when the relevant information spans multiple tables.

In relational databases, _joins_ offer a way to combine the records in two or more tables based on common field values. Different types of joins can achieve different results depending on how unmatched rows should be handled. In this guide, we'll discuss the various types of joins that PostgreSQL offers and how you can use them to combine table data from multiple sources.
In relational databases, [_joins_](/types/relational/what-are-joins-in-sql) offer a way to combine the records in two or more tables based on common field values. Different types of joins can achieve different results depending on how unmatched rows should be handled. In this guide, we'll discuss the various types of joins that PostgreSQL offers and how you can use them to combine table data from multiple sources.

## What are joins?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors: ['justinellingwood']

Though it's often useful to separate data into discrete tables for performance and consistency purposes, you often need to consult data from multiple tables to answer certain requests. _Joining_ tables is a way of combining the data from various tables by matching each record based on common field values.

There are a few different types of joins, which offer various ways of combining table records. In this article, we'll cover how MySQL implements joins and discuss the scenarios in which each is most useful.
There are a few different types of [joins](/types/relational/what-are-joins-in-sql), which offer various ways of combining table records. In this article, we'll cover how MySQL implements joins and discuss the scenarios in which each is most useful.

## What are joins?

Expand Down

0 comments on commit 09ba455

Please sign in to comment.