6

Here's the table in sql server.. and below is what I want to do.

TABLE:

Area (column 1) ----- Oregon California California California Oregon Washington Idaho Washington 

I want ALL the cities that are duplicates to be returned, but not the distinct ones. Just the duplicates. Is there a select statement that lets me return only duplicates?

3 Answers 3

22
Select State FROM Area GROUP BY State Having COUNT(*) > 1 

Try this

Update the query with your own column and table names

Sign up to request clarification or add additional context in comments.

Comments

1

While the GROUP BY .. HAVING approach is probably better here, this case - "more than one" - can also be answered with a JOIN assuming that there is a column (or set of columns) that form a Key. The requirement of a Key is that it must uniquely identify a record.

SELECT DISTINCT a1.State FROM AREA a1 JOIN AREA a2 ON a1.AreaId != a2.AreaId -- assume there is a Key to join on AND a1.State = a2.State -- and such that different Areas with same State 

2 Comments

@NISAR Then an appropriate Key is not being used (State can't be used as a key because it does not uniquely identify a record).
Thanks .. but your code take 93% query cost relative to this code takes 7% below.... "Select State FROM Area GROUP BY State Having COUNT(*) > 1"
-1

select count(area),area_id from [Table_Name] group by area_id having count(area) > 1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.