It has been suggested elsewhere including here
http://www.toadworld.com/KNOWLEDGE/KnowledgeXpertforOracle/tabid/648/TopicID/TSQ4
19/Default.aspx
that a select with “WHERE EXISTS” is more efficient than a select
DISTINCT.
That’s swell, but I find in my minimal experience that it also selects
duplicates. Did I stray somewhere?
I believe these two queries are reasonable adaptations of the examples given at
the ToadWorld site.
– Select ORGs that have employees
SELECT DISTINCT
f . ftvorgn_orgn_code org ,
f . ftvorgn_title title
FROM
ftvorgn f , – Table of ORGs
pebempl p – Table of employees
WHERE
f . ftvorgn_orgn_code = p. pebempl_orgn_code_home
ORDER BY 1 ;
– Select ORGs that have employees
SELECT
f . ftvorgn_orgn_code org ,
f . ftvorgn_title title
FROM
ftvorgn f – Table of ORGs
WHERE EXISTS
(select
‘ORGs that have employees’
from
pebempl p – Table of employees
where
p. pebempl_orgn_code_home = f . ftvorgn_orgn_code )
ORDER BY 1 , 2
The first query returns 228 unique rows; the second 533, the majority of them
being duplicates.
What gives?
Dan
Daniel B Madvig
Computer Technologies
Northwestern College & Northwestern Media
3003 Snelling Ave.
St. Paul, MN 55113
www.nwc.edu
651.631.5323