selectu.id, u.name,-- every post, as an array (selectcoalesce(jsonb_agg(jsonb_build_object('id', p.id,'title', p.title ) order byp.id), '[]')from posts pwherep.user_id=u.id ) as posts,-- the latest post, or null (select jsonb_build_object('id', p.id, 'title', p.title)from posts pwherep.user_id=u.idorder byp.published_atdescnullslastlimit1 ) as latest_postfrom users u