SQL aggregation function: STRING_AGG

STRING_AGG is an aggregate function that takes all expressions from rows and concatenates them into a single string.

FOR XML PATH is the previous version of the STRING_AGG function :

select a.NameSurname
-- previous version
, stuff((select ',' + x.Phone from CustomerPhone x where x.CustomerID=a.CustomerID FOR XML PATH('')), 1, 1, '')
-- new version
, (select STRING_AGG(x.Phone,',') from CustomerPhone x where x.CustomerID=a.CustomerID)
from Customer a    
where a.CustomerID = '78eb0107-36f6-49ce-8e78-d57a7f179dd1'

Here is the STRING_AGG :

select STRING_AGG(b.Phone,',') from Customer a
left join CustomerPhone b on a.CustomerID = b.CustomerID
where a.CustomerID = '78eb0107-36f6-49ce-8e78-d57a7f179dd1'

-- With additional column
select a.NameSurname, STRING_AGG(b.Phone,',') from Customer a
left join CustomerPhone b on a.CustomerID = b.CustomerID
where a.CustomerID = '78eb0107-36f6-49ce-8e78-d57a7f179dd1'
Group by a.CustomerID, a.NameSurname

-- Ordering 
select a.NameSurname, STRING_AGG(b.Phone,',') WITHIN GROUP (ORDER BY b.Phone desc) AS Result
from Customer a
left join CustomerPhone b on a.CustomerID = b.CustomerID
where a.CustomerID = '78eb0107-36f6-49ce-8e78-d57a7f179dd1'
Group by a.CustomerID, a.NameSurname

Performance Comparing :

https://sqlperformance.com/2016/12/sql-performance/sql-server-v-next-string_agg-performance

https://sqlperformance.com/2017/01/sql-performance/sql-server-v-next-string_agg-performance-part-2