Использование distinct on
:
select distinct on (s.id) s.*, sa.name as artist_name
from songs s left join
artists_songs ars
on ars.song_id = s.id left join
artists a
on a.id = ars.artist_id
order by s.id, sa.name;
Если вы хотите заказать конкретный заказ, вы можете использовать подзапрос:
select s.*
from (select distinct on (s.id) s.*, sa.name as artist_name
from songs s left join
artists_songs ars
on ars.song_id = s.id left join
artists a
on a.id = ars.artist_id
order by s.id, sa.name
) s
order by artist_name