Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8184105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:23:12+00:00 2026-06-07T01:23:12+00:00

I need to retrieve all information about records that are duplicates for specific fields.

  • 0

I need to retrieve all information about records that are duplicates for specific fields.

If I would use mysql I could solve in this way:

drop table if exists test;

create table test (
id int not null auto_increment primary key,
surname varchar(50),
firstname varchar(50),
sex char(1),
dob date,
pob varchar(50),
otherfield1 varchar(50),
otherfield2 varchar(50)
) engine = myisam;


insert into test (surname,firstname,sex,dob,pob,otherfield1,otherfield2)
values 
('smith','john','M','2000-01-01','rome','xxx','yyy'),
('black','jack','M','1990-12-30','milan','aaaaa','vvvv'),
('smith','john','M','2000-01-01','rome','zzz','aaaaa'),
('white','mike','M','1980-03-01','naples','zzz','other text'),
('white','mike','M','1980-03-01','naples','zzz','foo bar'),
('smith','ann','F','1992-03-05','turin','aaaaaaa','other text');


select * from test where (surname,firstname,sex,dob,pob) in (
select 
surname,firstname,sex,dob,pob
from test
group by surname,firstname,sex,dob,pob
having count(*) > 1
)  

and I’ll get

"id"    "surname"   "firstname" "sex"   "dob"            "pob"  "otherfield1"   "otherfield2"
"1" "smith"           "john"    "M" "2000-01-01"    "rome"     "xxx"    "yyy"
"3" "smith"           "john"    "M" "2000-01-01"    "rome"     "zzz"    "aaaaa"
"4" "white"           "mike"    "M" "1980-03-01"    "naples"   "zzz"    "other text"
"5" "white"           "mike"    "M" "1980-03-01"    "naples"   "zzz"    "foo bar"

However this method doesn’t work with mssql 2005:

create table #test (
id int identity,
surname varchar(50),
firstname varchar(50),
sex char(1),
dob datetime,
pob varchar(50),
otherfield1 varchar(50),
otherfield2 varchar(50)
)

insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('smith','john','M','2000-01-01','rome','xxx','yyy');
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('black','jack','M','1990-12-30','milan','aaaaa','vvvv');
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('smith','john','M','2000-01-01','rome','zzz','aaaaa');
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('white','mike','M','1980-03-01','naples','zzz','other text');
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('white','mike','M','1980-03-01','naples','zzz','foo bar');
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('smith','ann','F','1992-03-05','turin','aaaaaaa','other text');

select * from #test where (surname,firstname,sex,dob,pob) in (
select 
surname,firstname,sex,dob,pob
from #test
group by surname,firstname,sex,dob,pob
having count(*) > 1
) 

Thanks in advance.

EDIT

This is a possible solution I’ve found:

select t1.* from #test as t1
inner join (select 
            surname,firstname,sex,dob,pob
            from #test
            group by surname,firstname,sex,dob,pob
            having count(*) > 1) as t2
on t1.surname = t2.surname and t1.firstname = t2.firstname and t1.sex = t2.sex and t1.dob = t2.dob and t1.pob = t2.pob

but I’d like to know if there is a better way. I don’t like to join all these conditions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:23:13+00:00Added an answer on June 7, 2026 at 1:23 am
    SELECT * /*TODO: Just list desired columns*/
    FROM   (SELECT *,
                    Count(*) OVER (PARTITION BY surname,firstname,sex,dob,pob) AS Cnt
            FROM   #test) T
    WHERE  Cnt > 1 
    

    -Or

    SELECT *
    FROM   #test t1
    WHERE  EXISTS (SELECT *
                   FROM   #test t2
                   WHERE  t1.id <> t2.id
                          AND EXISTS (SELECT t1.surname,
                                             t1.firstname,
                                             t1.sex,
                                             t1.dob,
                                             t1.pob
                                      INTERSECT
                                      SELECT t2.surname,
                                             t2.firstname,
                                             t2.sex,
                                             t2.dob,
                                             t2.pob)) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need help with php code trying to retrieve all information from a table
Hi I'm new to Oracle 10g. I need retrieve the all the sublist from
I need to retrieve information from two separate models which are similar but not
I need to retrieve the date from a UIDatePicker (Preferably I would also like
I need to retrieve the name of the stored procedure that a crystal report
I need to track some information on users, but would like to retain it
I retrieve information from my database and it is all in a Cursor. Now
I have a class that I would like to store specific times in. The
I read books, articles, tutorials, and all that kind of stuff about the n-tier
How to get information about all network adapters in system? Name, Manufacturer, Location(PCI, slot2),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.