Whose heart within her bosom is."
-- Shakespeare, As You Like It
Act V, Scene IV, Lines 110-112
JOIN ON and on, and on and on
Continuing down the SELECT hierarchy, we now find ourselves at ON and JOIN. I know of no way to just talk about ON without JOIN and vise-versa.
- GROUP BY
- WITH CUBE or WITH ROLLUP
- ORDER BY
Historically when SQL was first created there was only one kind of JOIN, which we now term as INNER JOIN. An INNER JOIN is used when you want to bring together two tables on a value. You can think of them as a Venn diagram.
Let's look at our data and figure out how to join them.
INSERT INTO playwrights
(name, birthYear, birthEra)
('Oscar Wilde', 1854, 'AD')
,('Euripides', 480, 'BC')
,('Aeschylus', 525, 'BC')
,('Sophocles', 497, 'BC')
,('William Shakespeare', 1564, 'AD')
,('Jacopone da Todi', 1230, 'AD')
INSERT INTO plays
('The Importance of Being Earnest', 1)
,('Women of Troy', 2)
,('Phoenician Women', 2)
,('Seven against Thebes', 3)
,('The Libation Bearers', 3)
,('The Eumenides', 3)
,('Prometheus Bound', 3)
,('Oedipus Tyrannus', 4)
,('Oedipus at Colonus', 4)
,('The Tempest', 5)
,('A Midsummer Night''s Dream', 5)
,('The Merchant of Venice', 5)
,('As You Like It', 5)
,('Romeo and Juliet', 5)
,('Julius Caesar', 5)
,('The Second Shepherds'' Play', 0)
,('Edward III', 0)
Plays and their playwrights
If we want to join the plays to their playwrights we would want to use an INNER JOIN, ON the playwrigthId, which will join the id on the playwrights to plays (I know this because I created the tables in that way).
p.name AS play
,a.name AS playwright
FROM plays AS p
INNER JOIN playwrights AS a
ON p.playwrightId = a.id
|The Importance of Being Earnest||Oscar Wilde|
|Women of Troy||Euripides|
|Seven against Thebes||Aeschylus|
|The Libation Bearers||Aeschylus|
|Oedipus at Colonus||Sophocles|
|The Tempest||William Shakespeare|
|A Midsummer Night's Dream||William Shakespeare|
|The Merchant of Venice||William Shakespeare|
|As You Like It||William Shakespeare|
|Romeo and Juliet||William Shakespeare|
|Julius Caesar||William Shakespeare|
(On SQL Fiddle)
Notice that the playwright Jacopone da Todi and the two plays The Second Shepherd's Play and Edward III are not returned. The playwright Jacopone da Todi does not have any matching plays in our plays table. Likewise, the two plays The Second Shepherd's Play and Edward III do not have any matching playwrights in our playwrights table and thus are not returned. This is what it means to do an INNER JOIN between to tables, the value in the ON clause must match in both sets.
Look at the Venn diagram again, the spot that is red in the middle that is the overlap between the two sets. That is what an INNER JOIN is.