As the title says, why do we even need a subquery here? Why can’t all of the code in the subquery just be a part of the main SELECT statement?
WITH playlist_info AS
(
SELECT
p.playlist_id,
p.name playlist_name,
t.name track_name,
(t.milliseconds / 1000) length_seconds
FROM playlist p
LEFT JOIN playlist_track pt ON pt.playlist_id = p.playlist_id
LEFT JOIN track t ON t.track_id = pt.track_id
)
SELECT
playlist_id,
playlist_name,
COUNT(track_name) number_of_tracks,
SUM(length_seconds) length_seconds
FROM playlist_info
GROUP BY 1, 2
ORDER BY 1;