-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
9 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`. | ||
|
||
|
@@ -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. | ||
|
||
|
@@ -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: | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters