select b.code, name, grain, fruit
from tc1_birds b left join (
          select code, r, min(grain) grain, min(fruit) fruit
          from (
             select code, grain, null fruit, rank() over (partition by code order by grain) r
             from tc1_grains
             union all
             select code, null grain, fruit, rank() over (partition by code order by fruit) r
             from tc1_fruits
               ) food
          group by code, r) food on b.code=food.code
order by b.code, r