Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find people with same family name?

Tags:

sql

You have a table with 4 columns:

Primary key / name / surname / middle name

How to write Sql query to find people who has same family name?

1 / Ivan / Ivanov / Ivanovich

2 / Petr / Levinsky / Aleksandrovich

3 / Alex / Ivanov / albertovich

Should return Ivan and Alex

Thanks

like image 639
VextoR Avatar asked Apr 29 '11 08:04

VextoR


People also ask

How do I find someone with the same last name?

Websites like Genesreunited, Ancestry, My Heritage are some examples of sites that accept GEDCOM pedigree files, but there are many more. Rootsweb lists can be useful resources to find people searching the same surnames, places or subjects as you.

How can two people have the same last name?

One of the most common is through the parental shared name. As with a lot of traditions, most people share their last name with their father who got their father's name upon birth. This means the scenario where you share the last name with a cousin is via your father's brother's children.

How do I find all family members?

Use genealogy websites. Websites like Ancestry.com and FindAGrave.com are a good starting point, and some offer free trials. These sites have access to US census records, birth, marriage, death certificates, and other family trees that may intersect your own.

How do I find someone on Facebook by their last name?

Well, use Facebook. Facebook has a Family search option that lets you search Facebook members by their family name. Just type in a few character of your last name in the search box and it will show you a paginated list of all Facebook members that share the same last name.


2 Answers

In standard SQL you can simply join the table with itself:

select a.name, b.name
from t as a, t as b
where a.surname = b.surname and a.id < b.id

where t is your table and id is the primary key column.

This returns all distinct pairs of first names for every surname that has multiple entries.

You might want to add surname to the list of selected columns.

like image 72
NPE Avatar answered Oct 11 '22 23:10

NPE


If you want to find exactly names then you should firstly find all surnames that appear more than once and the find all names:

select name
  from t
  where surname in (select surname from t group by surname having count(surname) > 1);
like image 31
andr Avatar answered Oct 11 '22 23:10

andr