I've come across a strange query embedded in an application (yeah, great!):
SELECT PersonId
, Salutation
, Email
, Postcode
FROM vw_NewsletterSubscriptions
WHERE PersonId IN (SELECT PersonId FROM vw_NewsletterSubscriptions)
AND NewsletterTypeID=1
AND UnSubscribeDate Is NULL
GROUP BY PersonId
, Salutation
, Email
, Postcode
It's the SELECT in the WHERE clause that's got me. It seemed to be saying "select some data from this view where the data is in the view", which is a bit unnecessary. So I commented out that line in the WHERE clause:
SELECT PersonId
, Salutation
, Email
, Postcode
FROM vw_NewsletterSubscriptions
WHERE NewsletterTypeID=1
AND UnSubscribeDate Is NULL
GROUP BY PersonId
, Salutation
, Email
, Postcode
For completeness I ran both versions to check they are identical and they are not. The new version returns more rows - rows that were not in the first version. I've outer joined the two sets to see the difference but there's nothing obviously different about the extra rows.
I'm clearly missing something here. Can you explain what's going on, please?