Skip to content

Instantly share code, notes, and snippets.

@blaisethomas
Created March 25, 2015 08:17
Show Gist options
  • Save blaisethomas/a940d094bb7665d4306d to your computer and use it in GitHub Desktop.
Save blaisethomas/a940d094bb7665d4306d to your computer and use it in GitHub Desktop.
SQL madness daily, by L Thwaits
Aga works on a horse ranch in Malibu. She grooms eight horses, following this process for each:
Wire brush to get off the big dirt
Brush with softer brush to get the rest ofthe dust out
Brush the tail and mane
Hoof picking
She has an application that tracks which parts of the grooming process have been completed for each horse. Here are the tables:
horses
id | name
----+--------
1 | Angel
2 | Django
3 | Blaize
4 | Sky
5 | Bear
6 | Penny
7 | Julia
8 | Romeo
groomings
id | description
----+---------------------
1 | Big brush
2 | Small brush
3 | Groom mane and tail
4 | Hoof picking
horse_groomings
horse_id | grooming_id
----------+-------------
1 | 1
1 | 3
2 | 1
2 | 2
3 | 1
3 | 2
3 | 3
6 | 1
6 | 4
8 | 3
8 | 4
7 | 1
7 | 2
7 | 4
She wants to see what kinds of grooming have been done for each horse. Which SQL statement would she use?
@blaisethomas
Copy link
Author

A:
SELECT horses.name, groomings.description
FROM horse_groomings
ORDER BY 1;

B:
SELECT horses.name, groomings.description
FROM horses
INNER JOIN horse_groomings ON horses.id = horse_groomings.horse_id
INNER JOIN groomings ON groomings.id = horse_groomings.grooming_id
ORDER BY horses.name;

C:
FROM horses
SELECT horses.name, groomings.description
INNER JOIN horse_groomings ON horses.id = horse_groomings.horse_id
INNER JOIN groomings ON groomings.id = horse_groomings.grooming_id
ORDER BY 1;

😧
SELECT horses.name, groomings.description
FROM horses
INNER JOIN groomings ON groomings.id = horse_groomings.grooming_id
ORDER BY 1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment