?
Solved

SQL Query to display duplicates ?

Posted on 2016-11-22
6
Medium Priority
?
61 Views
Last Modified: 2016-11-23
Hi,
I have a table of Customers.  In some cases the names are the same for various customer ID's.  For example (see below),  Clinton is attached to 3 customers:
CUSTID  NAME
------|----------
39    | Carter   
40    | Reagan   
41    | Carter     
42    | Clinton  
44    | Clinton    
45    | Clinton

Open in new window

I would like to write a query that will give me a list of the duplicates (and only the duplicates) ordered by name, with a result something like:
CUSTID  NAME
------|----------
39    | Carter   
41    | Carter     
42    | Clinton  
44    | Clinton    
45    | Clinton 

Open in new window

Any suggestions?
0
Comment
Question by:Rob Rudloff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Accepted Solution

by:
mcmonap earned 1000 total points
ID: 41897936
In it's simplest form something like this:
SELECT
	*
FROM
	customers
WHERE
	[name] IN(
		SELECT
			[Name]
		FROM
			customers
		GROUP BY
			[name]
		HAVING
			COUNT(1) > 1)

Open in new window

0
 

Author Comment

by:Rob Rudloff
ID: 41898108
Ah yes -- I always forget about HAVING and then when I remember it, I can't seem to make it work.  (I'll read up on it)  
I will employ your suggestion.  Thanks!  
(I found another example using a join to the same table.  If I can get that to work, I'll post it here.)
0
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 1000 total points
ID: 41898227
Try this ...easiest one..

--

SELECT * FROM 
(
       SELECT * , COUNT(*) OVER (PARTITION BY name) rnk FROM customers
)k
WHERE rnk > 1

--

Open in new window


Hope it helps !
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:awking00
ID: 41899551
What dbms (and version) are you using?
0
 

Author Comment

by:Rob Rudloff
ID: 41899748
This is SQL Server 2008 R2.
0
 

Author Closing Comment

by:Rob Rudloff
ID: 41899752
Thanks!
I will work with both solutions, so that I can understand -- I am new-ish to SQL and want to learn from these examples you've posted.
0

Featured Post

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question