Link to home
Start Free TrialLog in
Avatar of hyphenpipe
hyphenpipeFlag for United States of America

asked on

Need help with an SQL query

I need to query a table in my database that contains client records.  I would like to see just the people in this table who have the same first name AND same last name and thier distinct ID number.

In otherwords the results should look something like:

ID, last_name, first_name
-------------------------------
23 Jones, James
45 Jones, James
67 Stewart, Martha
122 Stewart, Martha

Thanks in advance!
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hello hyphenpipe,

Select ID,Last_Name, First_Name From MyTable Where Last_Name = First_Name

Should do that.

Regards,

TimCottee
Avatar of chapmandew
try this:

select id, first_name, last_name
from yourtablename t
join
(
select first_name, last_name
from yourtablename
group by first_name, last_name
) a on t.first_name = a.first_name and t.last_name = a.last_name
ASKER CERTIFIED SOLUTION
Avatar of chapmandew
chapmandew
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hyphenpipe

ASKER

I'm sorry, the first and last name don't have to be equal but their whole name, ie find all the clients whose name are James Jones, Martha Stewart, etc.
chapmandew - yours worked, I just needed to reformat it as:

select id, t.first_name, t.last_name
from yourtablename t
join
(
select first_name, last_name
from yourtablename
group by first_name, last_name
having count(*)> 1
) a on t.first_name = a.first_name and t.last_name = a.last_name