Link to home
Create AccountLog in
Avatar of N2012
N2012

asked on

SQL Query of records with two equal columns

I can't seem to be able to create/find a query that shows results (all records) that have the same First and Last Name (two separate fields).
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

You did not present any table structure or sample query, so this is maybe not what you need:
SELECT FirstName, LastName, COUNT(*)
  FROM YourTable
 GROUP BY FirstName, LastName
 HAVING COUNT(*) > 1

Open in new window

And if you would like to display whole rows containing above names use the following query:
SELECT Y.* 
  FROM YourTable Y
  JOIN (SELECT FirstName, LastName, COUNT(*) cnt
          FROM YourTable
           GROUP BY FirstName, LastName
          HAVING COUNT(*) > 1) D 
    ON D.FirstName = Y.FirstName AND D.LastName = Y.LastName
 ORDER BY Y.FirstName, Y.LastName

Open in new window

Avatar of N2012
N2012

ASKER

pcelba - thanks, but that gives me a count. I don't want a count if there are multiple records. I don't care about multiple records, but rather records that have two identical fields (firstname and lastname).

In other words, I want to show the records - and only those records - that have the FirstName identical to the LastName.
ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of N2012

ASKER

That's what I thought I tried before and got an error.
Works.  :)