Here's a preview from my zine, Become a SELECT Star! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!
get the zine!
read the transcript!
example: LEFT JOIN + GROUP BY
This query counts how many items every client bought (including clients who didn’t buy anything):
SELECT name, COUNT (item) AS items_bought
FROM owners LEFT JOIN sales
ON owners.id = sales.client
GROUP BY name
ORDER BY items_bought DESC
FROM owners LEFT JOIN sales...
owners
| id | name |
|---|---|
| 1 | maher |
| 2 | rishi |
| 3 | chandra |
sales
| item | client |
|---|---|
| catnip | 1 |
| laser | 1 |
| tuna | 1 |
| tuna | 2 |
ON owners.id=sales.client
| id | name | item |
|---|---|---|
| 1 | maher | catnip |
| 1 | maher | laser |
| 1 | maher | tuna |
| 2 | rishi | tuna |
| 3 | chandra | NULL |
GROUP BY name
(same chart as previous, except the “maher” rows are circled, as are the “rishi” and “chandra” rows)
SELECT name, COUNT(item) AS items.bought
| name | items_bought |
|---|---|
| rishi | 1 |
| chandra | 0 |
| maher | 3 |
(COUNT(item) doesn’t count NULLs)
ORDER BY items_bought DESC
| name | items_bought |
|---|---|
| maher | 3 |
| rishi | 1 |
| chandra | 0 |