Rakuten interview question

sort array, join sql queries