Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
SQL

Get the count of Duplicate Records in SQL

- 11.06.18 | 14.04.20 - ErcanOPAK

First, Let’s have the table and its records:

DECLARE @TestTable TABLE (ID int, name varchar(10), email varchar(50))

INSERT @TestTable VALUES (1,'Zidane','1@abc.com')
INSERT @TestTable VALUES (2,'Henry','1@abc.com')
INSERT @TestTable VALUES (3,'Ronald','1@abc.com')
INSERT @TestTable VALUES (4,'Messi','2@abc.com')
INSERT @TestTable VALUES (5,'Ronaldo','1@abc.com')
INSERT @TestTable VALUES (6,'Henry','1@abc.com')

It’s easy to find duplicates with one field:

SELECT name, COUNT(email) 
FROM @TestTable
GROUP BY email
HAVING COUNT(email) > 1

If we need to find duplicate records regarding both columns (name and email):

SELECT
    name,email, COUNT(*) AS CountOf
    FROM @YourTable
    GROUP BY name,email
    HAVING COUNT(*)>1

OUTPUT:

name       email       CountOf
---------- ----------- -----------
Henry      1@abc.com   2
Ronaldo    1@abc.com   2

(2 row(s) affected)

 

Related posts:

Create, Alter, Drop and Execute SQL Server Stored Procedures

SQL “Parameter Sniffing Hell” — The REAL Fix Nobody Uses

Get a Tree view with SQL Query

Post Views: 131

Post navigation

How to use the second argument of Response.Redirect: True or False
Devexpress ASPxGridview Column Grouping in Code

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

December 2025
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  
« Nov    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (916)
  • Get the First and Last Word from a String or Sentence in SQL (798)
  • How to select distinct rows in a datatable in C# (773)
  • How to add default value for Entity Framework migrations for DateTime and Bool (773)
  • How to make theater mode the default for Youtube (602)
  • Add Constraint to SQL Table to ensure email contains @ (558)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (535)
  • Average of all values in a column that are not zero in SQL (505)
  • Find numbers with more than two decimal places in SQL (421)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (416)

Recent Posts

  • Visual Studio “F5 Takes Forever” — IIS Express Cache
  • WordPress Slow First Load — Autoloaded Options Table
  • Windows 11 Laptop Overheats — Turbo Boost Gone Wrong
  • Windows 11 Slow Context Menu — The Explorer Extension Trap
  • AJAX “Works Once, Then Fails” — Cached POST Response
  • JS Memory Leaks — Forgotten Event Listeners
  • HTML5 button Defaults to submit (Surprise!)
  • CSS opacity Breaks Child Elements — Use RGBA Instead
  • ASP.NET Core Health Checks That Actually Matter
  • ASP.NET Core “Slow Startup” — Reflection Scanning Trap

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (916)
  • Get the First and Last Word from a String or Sentence in SQL (798)
  • How to select distinct rows in a datatable in C# (773)
  • How to add default value for Entity Framework migrations for DateTime and Bool (773)
  • How to make theater mode the default for Youtube (602)

Recent Posts

  • Visual Studio “F5 Takes Forever” — IIS Express Cache
  • WordPress Slow First Load — Autoloaded Options Table
  • Windows 11 Laptop Overheats — Turbo Boost Gone Wrong
  • Windows 11 Slow Context Menu — The Explorer Extension Trap
  • AJAX “Works Once, Then Fails” — Cached POST Response

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2025 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com