Subject RE: [firebird-support] Re: SQL for master-detail
Author Leyne, Sean
Dan,

> > I am looking for all of the master records that have one or no
detail
> > records.
> >
> > Select Master_ID from MasterInfo where Master_ID (select Master_ID
> > from DetailInfo where [less then one record])
> >
> > Can this be done without a stored proc?
>
> Sure:
>
> Select * from MasterInfo m
> where
> (select count(*)
> from DetailInfo d
> where m.Master_ID = d.Master_ID) <= 1

The much better/faster approach is to use NOT EXISTS:

Select *
from
MasterInfo m
where
NOT EXISTS(
select 1
from DetailInfo d
where m.Master_ID = d.Master_ID
)


Sean