How to Find Duplicate Rows in Sql Table



In this Example we will see how to find duplicate multiple rows from Sql table. If you want to check there are multiple rows and record are more than you can use Sql query to find rows in tables.

First of all create table in Sql and insert multiple rows to find multiple when we execute query in Sql.

CREATE TABLE [dbo].[Customers](
      [CompanyName] [varchar](50) NOT NULL,
      [ContactName] [varchar](50) NOT NULL,
      [City] [varchar](50) NOT NULL,
      [Country] [varchar](50) NOT NULL,
      [CustomerID] [int] IDENTITY(1,1) NOT NULL,
 CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
(
      [CustomerID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Insert data in your customers table.

Dev
Chirag
Ahmedabad
India
1
ITInfo
Krunal
Rajkot
UK
2
Alashka
Chirag
Ahmedabad
India
22
Kokala
Manoj
Amreli
India
23
Shingoda
Ketan
Junagadh
India
24
Perfume
Minal
Alashka
USA
25
Dev
Chirag
Ahmedabad
India
26
Kirana Store
Manoj
Amreli
India
27






Now Create Query in Sql to Find Multiple rows.
    SELECT
    ContactName,City,Country
      , COUNT(*) AS CountOf
    FROM Customers
    GROUP BY ContactName,City,Country
    HAVING COUNT(*)>1

Select * from Customers   
    
Result

Related Posts

Previous
Next Post »

Thanks for comments.....