Joining Tables: John Mackintosh
Joining Tables: John Mackintosh
I N T R O D U C T I O N TO S Q L S E R V E R
John MacKintosh
Instructor
Relational Databases
+-----------+-------------------+
| artist_id | name |
|-----------+-------------------|
| 1 | AC/DC |
| 2 | Accept |
| 3 | Aerosmith |
| 4 | Alanis Morissette |
| 5 | Alice In Chains |
+-----------+-------------------+
+-----------+-------------------+ +----------+-------------------------+-----------
| artist_id | name | | album_id | title | artist_id
|-----------+-------------------| |----------+-------------------------+-----------
| 1 | AC/DC | | 1 | For Those About To Rock | 1
| 2 | Accept | | 2 | Balls to the Wall | 2
| 3 | Aerosmith | | 3 | Restless and Wild | 2
| 4 | Alanis Morissette | | 4 | Let There Be Rock | 1
| 5 | Alice In Chains | | 5 | Big Ones | 3
+-----------+-------------------+ +----------+-------------------------+-----------
+-----------+-------------------+ +----------+-------------------------+-----------+
| artist_id | name | | album_id | title | artist_id |
|-----------+-------------------| |----------+-------------------------+-----------|
| 1 | AC/DC | | 1 | For Those About To Rock | 1 |
| 2 | Accept | | 2 | Balls to the Wall | 2 |
| 3 | Aerosmith | | 3 | Restless and Wild | 2 |
| 4 | Alanis Morissette | | 4 | Let There Be Rock | 1 |
| 5 | Alice In Chains | | 5 | Big Ones | 3 |
+-----------+-------------------+ +----------+-------------------------+-----------+
+----------+-------------------------+-----------+-------------+
| album_id | title | artist_id | artist_name |
|----------+---------------------------------------+-----------|
| 1 | For Those About To Rock | 1 | AC/DC |
| 4 | Let There Be Rock | 1 | AC/DC |
+----------+-------------------------+-----------+-------------|
+----------+---------------------------------------+-----------+
| album_id | title | artist_id | artist_name |
|----------+---------------------------------------+-----------|
| 1 | For Those About To Rock | 1 | AC/DC |
| 4 | Let There Be Rock | 1 | AC/DC |
| 2 | Balls To The Wall | 2 | Accept |
| 3 | Restless and Wild | 2 | Accept |
+----------+---------------------------------------+-----------+
John MacKintosh
Instructor
The rationale for LEFT and RIGHT joins
Why do we need LEFT and RIGHT joins?
+------------+----------+ +------------+----------+
| Patient_ID | Admitted | | Patient_ID | Admitted |
|------------+----------| |------------+----------|
| 1 | 1 | | 1 | 1 |
| 2 | 1 | | 3 | 1 |
| 3 | 1 | | 4 | 1 |
| 4 | 1 | +------------+----------+
| 5 | 1 |
+------------+----------+
+------------+----------+------------| +------------+----------+------------|
| Patient_ID | Admitted | Discharged | | Patient_ID | Admitted | Discharged |
|------------+----------|------------| |------------+----------|------------|
| 1 | 1 | 1 | | 1 | 1 | 1 |
| 3 | 1 | 1 | | 2 | 1 | NULL |
| 4 | 1 | 1 | | 3 | 1 | 1 |
+------------+----------+------------+ | 4 | 1 | 1 |
| 5 | 1 | NULL |
+------------+----------+------------+
+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1 | 1 | 1 |
| 2 | 1 | NULL |
| 3 | 1 | 1 |
| 4 | 1 | 1 |
| 5 | 1 | NULL |
+------------+----------+------------+
+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1 | 1 | 1 |
| 2 | 1 | NULL |
| 3 | 1 | 1 |
| 4 | 1 | 1 |
| 5 | 1 | NULL |
+------------+----------+------------+
LEFT JOIN (or RIGHT JOIN ): All rows from the main table plus matches from the joining table
John MacKintosh
Instructor
SELECT SELECT
album_id, album_id,
title, title,
artist_id artist_id
FROM album FROM album
WHERE artist_id IN (1, 3) WHERE artist_id IN (1, 4, 5)
+------------+-------------------------+------------| +------------+-------------------------+------------|
| album_id | title | artist_id | | album_id | title | artist_id |
|------------+-------------------------|------------| |------------+-------------------------|------------|
| 1 | For Those About To Rock | 1 | | 1 | For Those About To Rock | 1 |
| 4 | Let There Be Rock | 1 | | 4 | Let There Be Rock | 1 |
| 5 | Big Ones | 3 | | 6 | Jagged Little Pill | 4 |
+------------+----------+---------------------------+ | 7 | Facelift | 5 |
+------------+-------------------------+------------+