Silverstone
Programmer
Hi all,
I recently submitted the below query, the goal of which was to get copies of duplicate records on my table:
SELECT *
FROM credpol.q7rl_legal_entity a
INNER JOIN
( SELECT prdid , COUNT ( * )
FROM credpol.q7rl_legal_entity
GROUP BY prdid ) AS tmp ( prdid,cnt )
ON a.prdid = tmp.prdid
WHERE tmp.cnt > 1 ;
I recieved a rather stroppy message from our Datawarehouse advising it wasn't efficient and to do the following:
SELECT *
FROM credpol.q7rl_legal_entity
where prdid in (SELECT prdid
FROM credpol.q7rl_legal_entity
group by 1
having count(*) > 1);
Can someone confirm this please? I was under the impression that when one does an "IN (SELECT......)" that the subquery runs for every row in the main table, however my query at the top does it in a derived table which i believed only runs once for the entire query, hence mine would be more efficient.
Naturally i could be wrong on this point hence i wanted to confirm it before moaning to our datawarehouse ;-)
On other question relating to the HAVING clause. I recall learning about it but do not recall the pro's & Con's when compared to WHERE. Can someone please give me a quick run down?
Thanks
Silverstone
I recently submitted the below query, the goal of which was to get copies of duplicate records on my table:
SELECT *
FROM credpol.q7rl_legal_entity a
INNER JOIN
( SELECT prdid , COUNT ( * )
FROM credpol.q7rl_legal_entity
GROUP BY prdid ) AS tmp ( prdid,cnt )
ON a.prdid = tmp.prdid
WHERE tmp.cnt > 1 ;
I recieved a rather stroppy message from our Datawarehouse advising it wasn't efficient and to do the following:
SELECT *
FROM credpol.q7rl_legal_entity
where prdid in (SELECT prdid
FROM credpol.q7rl_legal_entity
group by 1
having count(*) > 1);
Can someone confirm this please? I was under the impression that when one does an "IN (SELECT......)" that the subquery runs for every row in the main table, however my query at the top does it in a derived table which i believed only runs once for the entire query, hence mine would be more efficient.
Naturally i could be wrong on this point hence i wanted to confirm it before moaning to our datawarehouse ;-)
On other question relating to the HAVING clause. I recall learning about it but do not recall the pro's & Con's when compared to WHERE. Can someone please give me a quick run down?
Thanks
Silverstone