Introduction to SQL Server
John MacKintosh
Instructor
+-----------+-------------------+
| artist_id | name |
|-----------+-------------------|
| 1 | AC/DC |
| 2 | Accept |
| 3 | Aerosmith |
| 4 | Alanis Morissette |
| 5 | Alice In Chains |
+-----------+-------------------+
artist_id
+----------+-------------------------+-----------+
| album_id | title | artist_id |
|----------+-------------------------+-----------|
| 1 | For Those About To Rock | 1 |
| 2 | Balls to the Wall | 2 |
| 3 | Restless and Wild | 2 |
| 4 | Let There Be Rock | 1 |
| 5 | Big Ones | 3 |
+----------+-------------------------+-----------+
album_id
artist_id
?artist
table+-----------+-------------------+
| artist_id | name |
|-----------+-------------------|
| 1 | AC/DC |
| 2 | Accept |
| 3 | Aerosmith |
| 4 | Alanis Morissette |
| 5 | Alice In Chains |
+-----------+-------------------+
album
table+----------+-------------------------+-----------+
| album_id | title | artist_id |
|----------+-------------------------+-----------|
| 1 | For Those About To Rock | 1 |
| 2 | Balls to the Wall | 2 |
| 3 | Restless and Wild | 2 |
| 4 | Let There Be Rock | 1 |
| 5 | Big Ones | 3 |
+----------+-------------------------+-----------+
artist_id
: Foreign key to artist
artist
table+-----------+-------------------+
| artist_id | name |
|-----------+-------------------|
| 1 | AC/DC |
| 2 | Accept |
| 3 | Aerosmith |
| 4 | Alanis Morissette |
| 5 | Alice In Chains |
+-----------+-------------------+
artist_id
= 1album
table+----------+-------------------------+-----------+
| album_id | title | artist_id |
|----------+-------------------------+-----------|
| 1 | For Those About To Rock | 1 |
| 2 | Balls to the Wall | 2 |
| 3 | Restless and Wild | 2 |
| 4 | Let There Be Rock | 1 |
| 5 | Big Ones | 3 |
+----------+-------------------------+-----------+
artist_id
= 1+----------+-------------------------+-----------+-------------+
| 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
tableartist
tableartist_id
columnSELECT
album_id,
title,
album.artist_id,
name AS artist_name
FROM album
INNER JOIN artist ON artist.artist_id = album.artist_id
WHERE album.artist_id = 1;
+----------+-------------------------+-----------+-------------+
| album_id | title | artist_id | artist_name |
|----------+---------------------------------------+-----------|
| 1 | For Those About To Rock | 1 | AC/DC |
| 4 | Let There Be Rock | 1 | AC/DC |
+----------+-------------------------+-----------+-------------|
SELECT
table_A.columnX,
table_A.columnY,
table_B.columnZ
FROM table_A
INNER JOIN table_B ON table_A.foreign_key = table_B.primary_key;
SELECT
album_id,
title,
album.artist_id,
name AS artist_name
FROM album
INNER JOIN artist on artist.artist_id = album.artist_id;
+----------+---------------------------------------+-----------+
| 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 |
+----------+---------------------------------------+-----------+
album
and artist
SELECT
table_A.columnX,
table_A.columnY,
table_B.columnZ, table_C columnW
FROM table_A
INNER JOIN table_B ON table_B.foreign_key = table_A.primary_key
INNER JOIN table_C ON table_C.foreign_key = table_B.primary_key;
Introduction to SQL Server