create table statuses (id int primary key, title text);
insert into statuses values (1, 'Completed'), (2, 'Not Completed');
create table problems (id int primary key, title text);
insert into problems values (1, 'Problem 1'), (2, 'Problem2'), (3, 'Problem3');
create table tasks (id int, problem_id int references problems, status_id int references statuses);
insert into tasks values (1, 1, 1), (2, 1, 2), (3, 2, 1);
with problem_status as (
select
problems.*,
max(status_id) status_id
from problems
left join tasks on tasks.problem_id = problems.id
group by problems.id, problems.title
) select problem_status.id, problem_status.title, statuses.title
from problem_status
left join statuses on statuses.id = problem_status.status_id
order by problem_status.id;