Table is:
| Id | 
Firstname | 
| 4 | 
abc | 
| 4 | 
def | 
| 4 | 
ghi | 
| 4 | 
jkl | 
| 4 | 
mno | 
Required output:
| Id | 
Name | 
| 4 | 
abc,def,ghi,jkl,mno | 
Query:
SELECT ID, 
    Name = STUFF(
                 (SELECT ',' + firstname FROM temp FOR XML PATH ('')), 1, 1, ''
               ) 
FROM temp GROUP BY ID
This query is working but I want to know the reason how it is working or if there is any other  way to get this done