Add lesson about foreign keys

pull/8127/head
Kamran Ahmed 4 weeks ago
parent 54eed670f8
commit 5b1fd1e0b8
  1. 2
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/foreign-key-constraint.md
  2. 2
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/joins-in-queries.md
  3. 107
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/self-joins-and-usecases.md

@ -1,7 +1,7 @@
---
title: Foreign Key Constraint
description: Learn how foreign keys are used to enforce relationships between tables
order: 130
order: 140
type: lesson-challenge
setup: |
```sql

@ -448,4 +448,4 @@ The result from this query will be:
| Dress 4 | Green |
| Dress 4 | Yellow |
That's all for this lesson. Make sure to practice the examples and try out different types of JOINs. Future lessons will cover more examples and use cases for JOINs. For now, let's move on to the next lesson about foreign keys.
That's all for this lesson. Make sure to practice the examples and try out different types of JOINs. Future lessons will cover more examples and use cases for JOINs. Let's move on to the next lesson where we will learn about self joins and their use cases.

@ -0,0 +1,107 @@
---
title: Self Joins and Use Cases
description: Learn how to use self joins to query hierarchical and self-referential data
order: 130
type: lesson-challenge
setup: |
```sql
CREATE TABLE employee (
id INT PRIMARY KEY,
name VARCHAR(255),
manager_id INT
);
INSERT INTO employee (id, name, manager_id)
VALUES (1, 'John Smith', NULL), -- CEO
(2, 'Sarah Johnson', 1), -- Reports to John
(3, 'Michael Brown', 1), -- Reports to John
(4, 'Emily Davis', 2), -- Reports to Sarah
(5, 'James Wilson', 2), -- Reports to Sarah
(6, 'Lisa Anderson', 3); -- Reports to Michael
CREATE TABLE product (
id INT PRIMARY KEY,
name VARCHAR(255),
substitute_product_id INT
);
INSERT INTO product (id, name, substitute_product_id)
VALUES (1, 'iPhone 13', 2),
(2, 'iPhone 14', 3),
(3, 'iPhone 15', NULL),
(4, 'Galaxy S21', 5),
(5, 'Galaxy S22', 6),
(6, 'Galaxy S23', NULL);
```
---
Sometimes, you may need to join a table with itself. This might sound strange at first, but it's a powerful technique for working with hierarchical or self-referential data.
## What is a Self Join?
A self join is when you join a table to itself. This is useful when a table contains records that reference other records in the same table i.e. hierarchical data. Common examples include:
- Employee-manager relationships
- Product substitutes (e.g. a product may have a substitute)
- Family trees (e.g. a person having parent and children)
- Organization hierarchies
Let's understand this with some examples. Let's say that we have the following `employee` table where each employee has a `manager_id` that references the `id` of another employee:
| id | name | manager_id |
| --- | ------------- | ---------- |
| 1 | John Smith | NULL |
| 2 | Sarah Johnson | 1 |
| 3 | Michael Brown | 1 |
| 4 | Emily Davis | 2 |
| 5 | James Wilson | 2 |
| 6 | Lisa Anderson | 3 |
Each employee (except John, the CEO) has a manager, who is also an employee. Let's write some queries to understand self joins.
### Finding Employees and Their Managers
The query below will return employees and their managers:
```sql
SELECT
e.name AS employee,
m.name AS manager
FROM employee e
LEFT JOIN employee m ON e.manager_id = m.id;
```
Notice that we are using aliases (`e` and `m`) to distinguish between the two instances of the same table. Also, we are using `LEFT JOIN` to ensure that we include all employees, even those without a manager (i.e. John who has no manager).
The output of the query will be:
| employee | manager |
| ------------- | ------------- |
| John Smith | NULL |
| Sarah Johnson | John Smith |
| Michael Brown | John Smith |
| Emily Davis | Sarah Johnson |
| James Wilson | Sarah Johnson |
| Lisa Anderson | Michael Brown |
### Finding Employees who Report to Sarah
We can use a self join to find employees who report to Sarah:
```sql
SELECT
e.name AS employee,
m.name AS manager
FROM employee e
LEFT JOIN employee m ON e.manager_id = m.id
WHERE m.name = 'Sarah';
```
The output of the query will be:
| employee | manager |
| ------------ | ------------- |
| Emily Davis | Sarah Johnson |
| James Wilson | Sarah Johnson |
That's all for this lesson. Make sure to practice the examples and try out different types of self joins. Future lessons will cover more examples and use cases for self joins. Let's move on to the next lesson where we will learn about foreign keys.
Loading…
Cancel
Save