with total as (select *from users a left join edges b on a.user_id = b.user_a_idunionselect *from users a left join edges b on a.user_id = b.user_b_id)select user_id,count(user_b_id) as num_friendsfrom totalgroup by 1order by 2 desc , user_id asc ; union 을 사용했지만 union all 을 사용하는 것이 좀 더 성능상으로 좋기에 다시 한번 작성해보았다. with total as (select user_a_id as user_id ,count(user_b_id) as cnt from edges g..