Add lesson 4: JOINs explained with Venn diagrams (sort of)
3 changed files with 38 additions and 0 deletions
lessons/04-joins-explained-with-venn-diagrams-sort/README.md ADDED
| @@ -0,0 +1,21 @@ | ||
| 1 | +# Lesson 4: JOINs explained with Venn diagrams (sort of) | |
| 2 | + | |
| 3 | +This lesson covers jOINs explained with Venn diagrams (sort of). You'll read a short explanation, try three small exercises and finish with a quiz. | |
| 4 | + | |
| 5 | +Full lesson: [https://glibcode.net/courses/sql-essentials/lessons/4](https://glibcode.net/courses/sql-essentials/lessons/4) | |
| 6 | + | |
| 7 | +## Files | |
| 8 | + | |
| 9 | +- `joins.sql`: INNER and LEFT JOINs between customers and orders | |
| 10 | + | |
| 11 | +## Run it | |
| 12 | + | |
| 13 | +```sh | |
| 14 | +sqlite3 shop.db < ../../setup/schema.sql | |
| 15 | +sqlite3 shop.db < ../../setup/seed.sql | |
| 16 | +sqlite3 shop.db < joins.sql | |
| 17 | +``` | |
| 18 | + | |
| 19 | +## Exercises | |
| 20 | + | |
| 21 | +See [exercises.md](exercises.md). Ask in the forum once you've tried. | |
lessons/04-joins-explained-with-venn-diagrams-sort/exercises.md ADDED
| @@ -0,0 +1,5 @@ | ||
| 1 | +# Exercises: lesson 4 | |
| 2 | + | |
| 3 | +1. Rewrite the main part so it runs twice with two different inputs, without copying the code. | |
| 4 | +2. Combine this lesson with the previous one: use something from both in a small program of your own. | |
| 5 | +3. Run `joins.sql` and change one value. Write down what you expect, then run it again and compare. | |
lessons/04-joins-explained-with-venn-diagrams-sort/joins.sql ADDED
| @@ -0,0 +1,12 @@ | ||
| 1 | +SELECT c.name, o.ordered_at, o.total | |
| 2 | +FROM customers AS c | |
| 3 | +JOIN orders AS o ON o.customer_id = c.id | |
| 4 | +WHERE o.total > 100 | |
| 5 | +ORDER BY o.ordered_at DESC | |
| 6 | +LIMIT 10; | |
| 7 | + | |
| 8 | +-- Customers who never ordered: LEFT JOIN and look for the missing side | |
| 9 | +SELECT c.name | |
| 10 | +FROM customers AS c | |
| 11 | +LEFT JOIN orders AS o ON o.customer_id = c.id | |
| 12 | +WHERE o.id IS NULL; | |