Re: SQL question
Posted in 1997
Alexander Leites wrote:
>
> Hello, Matt.
>
> >select fname,count(*) from customer
> >group by fname
> >where count(*) > 1>
[SNIP]
> Try following:
>
> select fname,count(*) from customer cust_tbl1
> where (select count(*) from customer cust_tbl2
> where cust_tbl1.fname = cust_tbl2.fname) > 1
> group by fname>
Better:
select fname, count(*)
from customer
group by fname
having count(*) > 1;
This is MUCH faster than a correlated subquery, especially that one).
(BTW the syntax guide shows HAVING clauses must precede GROUP BY
clauses,
the manual is wrong, GROUP BY comes first!)
N.B.- ANY correlated subquery can be rewritten as a simple query or
simple join! (Indeed ANY non-trivial SQL SELECT can be written
two other ways! -- Kagel's Law)
Art S. Kagel