INNER JOIN (III) - 3つのテーブルを結合する
これまでに、album と track、そして album と artist のように、2つのテーブルを結合する方法を見てきました。この演習では、3つすべてのテーブルを結合して、より完全な結果セットを取得します。引き続き INNER JOIN を使いますが、複数回指定する必要があります。
ここでの注意点として、track と artist のどちらにも name 列があるため、列名の前に対象のテーブル名を付けてプレフィックスし、どのテーブルの列を選択するかを明示(qualify)する必要があります。
この演習はコースの一部です
SQL Server 入門
演習の手順
- 2か所とも
name列に正しいテーブル接頭辞を付けて明示してください。 albumとtrack、およびartistとalbumを結合するように、両方のINNER JOIN句を完成させてください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
SELECT track_id,
-- Enter the correct table name prefix when retrieving the name column from the track table
___.name AS track_name,
title as album_title,
-- Enter the correct table name prefix when retrieving the name column from the artist table
___.name AS artist_name
FROM track
-- Complete the matching columns to join album with track, and artist with album
INNER JOIN album on track.___ = album.album_id
INNER JOIN artist on album.artist_id = artist.___;