Rakuten Interview Question

sort array, join sql queries