How to Select Every Row Where Column Value is NOT Distinct

SqlSql ServerSql Server-2008

Sql Problem Overview


I need to run a select statement that returns all rows where the value of a column is not distinct (e.g. EmailAddress).

For example, if the table looks like below:

CustomerName     EmailAddress
Aaron            aaron@gmail.com
Christy          aaron@gmail.com
Jason            jason@gmail.com
Eric             eric@gmail.com
John             aaron@gmail.com

I need the query to return:

Aaron            aaron@gmail.com
Christy          aaron@gmail.com
John             aaron@gmail.com

I have read many posts and tried different queries to no avail. The query that I believe should work is below. Can someone suggest an alternative or tell me what may be wrong with my query?

select EmailAddress, CustomerName from Customers
group by EmailAddress, CustomerName
having COUNT(distinct(EmailAddress)) > 1

Sql Solutions


Solution 1 - Sql

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)

Solution 2 - Sql

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 aaron@gmail.com
christy and aaron@gmail.com
john and aaron@gmail.com

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)

Solution 3 - Sql

select CustomerName,count(1) from Customers group by CustomerName having count(1) > 1

Solution 4 - Sql

How about

SELECT EmailAddress, CustomerName FROM Customers a
WHERE Exists ( SELECT emailAddress FROM customers c WHERE a.customerName != c.customerName AND a.EmailAddress = c.EmailAddress)

Solution 5 - Sql

Just for fun, here's another way:

;with counts as (
    select CustomerName, EmailAddress,
      count(*) over (partition by EmailAddress) as num
    from Customers
)
select CustomerName, EmailAddress
from counts
where num > 1

Solution 6 - Sql

Rather than using sub queries in where condition which will increase the query time where records are huge.

I would suggest to use Inner Join as a better option to this problem.

Considering the same table this could give the result

SELECT EmailAddress, CustomerName FROM Customers as a 
Inner Join Customers as b on a.CustomerName <> b.CustomerName and a.EmailAddress = b.EmailAddress

For still better results I would suggest you to use CustomerID or any unique field of your table. Duplication of CustomerName is possible.

Solution 7 - Sql

SELECT        Title, Id
FROM            dbo.TblNews
WHERE        (Title IN
      (SELECT  Title 
FROM dbo.TblNews AS TblNews_1
GROUP BY Title
HAVING (COUNT(*) > 1)))
ORDER BY Title
  • sort in title

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGrasshopperView Question on Stackoverflow
Solution 1 - SqlSerj SaganView Answer on Stackoverflow
Solution 2 - SqlSeasonedView Answer on Stackoverflow
Solution 3 - SqlNisarView Answer on Stackoverflow
Solution 4 - SqlMarcView Answer on Stackoverflow
Solution 5 - SqlChadView Answer on Stackoverflow
Solution 6 - SqlNaveen KishanView Answer on Stackoverflow
Solution 7 - SqlmirazimiView Answer on Stackoverflow