The subquery has to do a join based on the outside id (which happens to be a self join - ie back to the same database table), then we group & order the results together.
select id,
count(outside_example.*),
array_to_string (
array(
select val from example inside WHERE inside.id=outside_example.id
), ','
)
FROM example outside_example
GROUP BY outside_example.id
ORDER BY outside_example.id
;