Kalpesh Patel
Kalpesh Patel

Reputation: 1678

Select Duplicate Entries from a database table

I have a two columns in a table say columnA and columnB. I want to select duplicate entries for columnA where columnB=xx or columnB=yy. For Example

columnA columnB
12      abc
12      pqr
11      abc
10      pqr
9       xyz

for above table I want to get 12 as a result. which is common for columnB=abc or columnB=pqr.

Please help me to build a sql query. I tried with count(*) but coudn't get the result.

Upvotes: 3

Views: 82

Answers (2)

Linger
Linger

Reputation: 15048

SQL Fiddle:

SELECT a.columnA
FROM MyTable a
INNER JOIN MyTable b ON a.columnA = b.columnA 
WHERE a.columnB = 'abc'
AND b.columnB = 'pqr';

Upvotes: 2

Nick
Nick

Reputation: 3643

SELECT columnA, count(columnA) 
FROM tableName 
GROUP BY columnA
HAVING count(columnA)>1

this will at least give you a list of all duplicated entries, followed by the count of dupes.

Upvotes: 0

Related Questions