Poder supremo
A veces puedes querer "guardar" los resultados de una consulta para poder seguir trabajando con los datos. Puedes hacerlo creando una tabla temporal que permanezca en la base de datos hasta que se reinicie SQL Server. En este ejercicio final, seleccionarás la pista más larga de cada álbum y la añadirás a una tabla temporal que crearás como parte de la consulta.
Este ejercicio forma parte del curso
Introducción a SQL Server
Instrucciones del ejercicio
- Inserta datos mediante una declaración
SELECTen una tabla temporal llamada#maxtracks. - Une
albumconartistutilizandoartist_idytrackconalbumutilizandoalbum_id. - Ejecuta la declaración
SELECTfinal para recuperar todas las columnas de tu nueva tabla.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
SELECT album.title AS album_title,
artist.name as artist,
MAX(track.milliseconds / (1000 * 60) % 60 ) AS max_track_length_mins
-- Name the temp table #maxtracks
INTO ___
FROM album
-- Join album to artist using artist_id
INNER JOIN artist ON album.artist_id = artist.artist_id
-- Join track to album using album_id
___
GROUP BY artist.artist_id, album.title, artist.name,album.album_id
-- Run the final SELECT query to retrieve the results from the temporary table
SELECT album_title, artist, max_track_length_mins
FROM #maxtracks
ORDER BY max_track_length_mins DESC, artist;