Есть таблички:
cosmonauts:
id,
full_name(text),
birthdate(datetime),
country_id,
can_be_pilot(true/false),
rank(number)
tasks:
id,
title(text),
description(text),
deadline(datetime)
cosmonauts_schedule:
id,
cosmonaut_id,
task_id,
starts_at(datetime)
Нужно выбрать того, кто умеет пилотировать корабль, и у кого есть полностью свободный от задач день в течении недели с сегодняшнего дня.
select id
from cosmonauts c
join cosmonauts_schedule cs on cs.cosmonaut_id = c.id
join tasks t on t.id = cs.task_id
where can_be_pilot = true and