DelphiFAQ Home Search:

SQL: Find duplicate rows in a table (with a primary key)

 

comments23 comments. Current rating: 5 stars (12 votes). Leave comments and/ or rate it.

Question:

I have a table of city names and need to find duplicate entries. The table does have a primary key called CITY_ID, so the duplicates will have different CITY_ID values but identical CITY_NAME values.

Answer:

If you indeed have a primary key then you need two cursor instances (c1, c2) as the following example shows. The query requires that ID #1 is smaller than ID #2 otherwise all pairs would be returned twice (2,3) and (3,2) or, if you don't even require that c1 <> c2, .. well, try that out for yourself.

If you do not have a primary key defined, see the other tip mentioned in the 'See Also' box.

Note:
In Microsoft SQL-Server, you can use the HAVING clause, as shown at the bottom.

// return all pairs of city IDs that have the same city name

select c1.city_id, c2.city_id, c1.city_name
from cities c1, cities c2
where c1.city_id < c2.city_id and c1.city_name = c2.city_name


// version for Micrsoft's MSSQL Server
// make use of the HAVING clause
select city_name
from areas
group by city_name
having count(*) > 1

Comments:

You are on page 2 of 2, other pages: 1 [2]
2008-06-17, 04:23:14
anonymous from India  
rating
Very nice topic for freshers
2008-08-18, 01:12:00
anonymous from China  
Try this Code:

SELECT DISTINCT column1 from table
group by column1 HAVING count(column1) > 1
This groups your id's and only shows the id's that have more than 1 occurence in your table.

Hope this helps!


Keywords:
2008-09-09, 03:40:39
anonymous from Pakistan  
rating
Very Helpfull . Thanks
2008-10-14, 16:28:57
Geekette  
rating
this code has been so helpful to me, I have been converting data from one table into another DB. I use it just about every day!
Thanx
2008-10-15, 03:37:15
anonymous  
Hi, how do i list all my salesman-customers pairs who made the largest number of transactions together, whereby the customer has never complain against the salesman? That is, i have a lot of pairs, but i want to select all sellers to be distinct with their most popular customer
2009-02-04, 04:52:57
anonymous from Turkey  
great tip thanks
2009-05-28, 02:49:57
anonymous from Malaysia  
Dear Friends,

I just started programming with Delphi.
My objective is as below

I have a Table in MS ACCESS with say 10 fields.

I wants to fetch a data based on the search criteria below

The search is based on 3 or more fields.

The given value for one filed will not be the same as the value in the table . We need to fetch nearest value from the table . The search string for the other values are as same as the table value.

Please help how to fetch the data from the Table using ADO controls.

2009-06-27, 02:53:12
anonymous from United States  
Very good site
Thanku very much
You are on page 2 of 2, other pages: 1 [2]

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.