postgresql7.1
select
t.template_id,
t.name,
t.description,
t.filename,
t.resource_dir,
case when m.template_id = null then ' ' else 'checked' end as checked
from portal_templates t
left outer join portal_available_template_map m
on (t.template_id = m.template_id)
where
t.type = 'layout' and
(m.portal_id = :default_portal_id or m.portal_id is null)
order by t.name