Junções internas (III) – junção de 3 tabelas
Vimos como fazer a junção de duas tabelas: album
com track
e album
com artist
. Neste exercício, você deve fazer a junção de três tabelas para obter um conjunto de resultados mais completo. Você vai continuar usando INNER JOIN
, mas precisa especificar mais de uma.
Aqui, observe que, como track
e artist
contêm a coluna name
, você precisa usar qualify
para definir onde está selecionando as colunas, acrescentando o prefixo do nome da coluna com o nome da respectiva tabela.
Este exercício faz parte do curso
Introdução ao SQL Server
Instruções de exercício
- Qualifique a coluna
name
especificando o prefixo correto da tabela em ambos os casos. - Preencha as duas cláusulas
INNER JOIN
para fazer a junção dealbum
comtrack
eartist
comalbum
.
Exercício interativo prático
Experimente este exercício preenchendo este código de exemplo.
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.___;