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

  • SEARCH
  • Home
  • 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 7444661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:33:42+00:00 2026-05-29T11:33:42+00:00

I am trying to find duplicates by comparing the first name and surname columns

  • 0

I am trying to find duplicates by comparing the first name and surname columns in a table. The first name can be a name or an initial.

Reading other posts I have managed to figure out how to get the duplicate surnames and list the first letter for first name. But I am unsure how to only show rows where there is a match of surname and the first letter of the first name.

SELECT * 
FROM table AS a
INNER JOIN (

SELECT LEFT( firstname, 1 ) , surname
FROM table
GROUP BY surname
HAVING COUNT( * ) > 1
) AS b ON a.surname = b.surname

id | firstname | surname
**************************
1  | joe       | bloggs
2  | j         | bloggs
3  | s         | bloggs
4  | f         | doe
5  | frank     | spencer

Currently this query would return

1  | joe       | bloggs
2  | j         | bloggs
3  | s         | bloggs

Result I would like would just contain the possible duplicates.

1  | joe       | bloggs
2  | j         | bloggs
  • 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-05-29T11:33:43+00:00Added an answer on May 29, 2026 at 11:33 am

    I don’t quite get what you want. Yor provided a query, your current table and the expected result.

    I’ve just created your table, run your query and got the expected result. What is wrong with this?

    SELECT  FROM table1 AS a
    INNER JOIN (
      SELECT surname FROM table1
      GROUP BY surname
      HAVING COUNT(*) > 1
    ) AS b ON a.surname = b.surname
    

    This effectively result in your expected result:

    joe | bloggs
    j   | bloggs
    

    Or am I missing something?

    After re-reading… are you expecting to get only this?

    j | bloggs
    

    If that is the case, use this:

    SELECT * FROM table1 AS a
    INNER JOIN (
      SELECT surname FROM table1
      GROUP BY surname
      HAVING COUNT(*) > 1
    ) AS b ON a.surname = b.surname
    WHERE CHAR_LENGTH(firstname) = 1
    

    Edit:

    After the expected result was properly explained I conclude the query should be:

    SELECT a.firstname, a.surname FROM t1 AS a
    INNER JOIN (
      SELECT LEFT(firstname, 1) AS firstChar, surname FROM t1
      GROUP BY surname, firstChar
      HAVING COUNT(surname) > 1
    ) AS b ON a.surname = b.surname AND b.firstChar = LEFT(a.firstname, 1)
    

    Working example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find duplicates in my users table (don't ask, it's a lot
Possible Duplicate: Comparing Two Arrays Using Perl I am trying to find elements that
I'm trying to find duplicate keys so that they can be addressed and made
I am trying to find rows that have duplicate values, but only based off
Trying to find a good name for a method swapping each coordinate X and
I'm trying to isolate duplicates in a 500MB database and have tried two ways
I have a problem adding strings to a TStringList. I've searched other posts but
Im trying to find a way to delete all duplicated entries in a table
I am trying to find a way to have my Entity Framework model in
I have a database storing multiple columns and I am trying to create efficient

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.