How to Select Every Row Where Column Value is NOT Distinct
This is significantly faster than the EXISTS
way:
SELECT [EmailAddress], [CustomerName] FROM [Customers] WHERE [EmailAddress] IN
(SELECT [EmailAddress] FROM [Customers] GROUP BY [EmailAddress] HAVING COUNT(*) > 1)
The thing that is incorrect with your query is that you are grouping by email and name, that forms a group of each unique set of email and name combined together and hence
aaron and [email protected]
christy and [email protected]
john and [email protected]
are treated as 3 different groups rather all belonging to 1 single group.
Please use the query as given below :
select emailaddress,customername from customers where emailaddress in
(select emailaddress from customers group by emailaddress having count(*) > 1)
select CustomerName,count(1) from Customers group by CustomerName having count(1) > 1