Delete duplicate records from SQL Server 2012 table with identity

Shammas

I am trying to replicate a scenario where I need to delete all duplicate rows from a table except one. But all rows have a unique identity column.

For making things easier, I created a small test table student and the script is as below.

create table student 
(
    id int,
    rollno int,
    name varchar(50),
    course varchar(50)
)
GO

insert into student values(1,1335592,'john','biology')
insert into student values(2,1335592,'john','biology')
insert into student values(3,1335592,'john','biology')
insert into student values(4,1335592,'john','biology')
insert into student values(5,1335593,'peter','biology')
insert into student values(6,1335593,'peter','biology')
insert into student values(7,1335593,'peter','biology')
GO

select * from student

This will generate the table as below.

id  rollno  name    course
1   1335592 john    biology
2   1335592 john    biology
3   1335592 john    biology
4   1335592 john    biology
5   1335593 peter   biology
6   1335593 peter   biology
7   1335593 peter   biology

I would like to keep the records with ID '1' and '5' in the result set and delete everything else. Is there any way to do this?.

All help will be greatly appreciated.

Thanks Shammas

Mukesh Kalgude

It is simple query

Delete from student 
where id not in (select min(id) 
                 from student 
                 group by rollno, name, course) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Primary Identity Column of table gets random value in sql server 2012

From Dev

SQL Server 2012 Delete From Small Table Very Slow

From Dev

sql server delete duplicate records from a table after checking if these records are there in dependent table

From Dev

Update all but one of duplicate records in table in SQL Server

From Dev

how to avoid inserting duplicate records in table type sql server

From Dev

SQL Server: How to allow duplicate records on small table

From Dev

Truncate Vs Delete and Table Locks SQL Server 2012

From Dev

Delete duplicate records from a Postgresql table without a primary key?

From Dev

Delete duplicate data and load into another table in SQL Server

From Dev

Delete Duplicate Records in table using SQL

From Dev

Oracle SQL query: Delete the oldest duplicate records from the table

From Dev

How to delete all duplicate records from sql

From Dev

How to delete multiple records from SQL table

From Dev

How to Delete Duplicate records from Table in SQL Server?

From Dev

sql server delete duplicate records from a table after checking if these records are there in dependent table

From Dev

Update all but one of duplicate records in table in SQL Server

From Dev

SQL Server: How to allow duplicate records on small table

From Dev

How to delete a filegroup from a partitioned table (SQL Server 2012)

From Dev

SQL Server - Remove/Delete Specific part of the records of a table

From Dev

Delete the non-duplicate row from actual Database Table in SQL Server 2008 R2

From Dev

How to delete duplicate records from a table in oracle

From Dev

SQL Server: table design for changing Identity records

From Dev

SQL server 2012 - Query on retrieving matching records from a table by cross checking with two other tables

From Dev

Combining duplicate records in SQL Server

From Dev

SQL Server 2012 Merge Records in OUTER APPLY related table

From Dev

Delete user-defined indexes for a table in SQL Server 2012

From Dev

Select Except the duplicate Records from the table in SQL Server

From Dev

Remove duplicate records from views in SQL Server

From Dev

SQL Delete duplicate rows in the table without primary key on SQL Server

Related Related

  1. 1

    Primary Identity Column of table gets random value in sql server 2012

  2. 2

    SQL Server 2012 Delete From Small Table Very Slow

  3. 3

    sql server delete duplicate records from a table after checking if these records are there in dependent table

  4. 4

    Update all but one of duplicate records in table in SQL Server

  5. 5

    how to avoid inserting duplicate records in table type sql server

  6. 6

    SQL Server: How to allow duplicate records on small table

  7. 7

    Truncate Vs Delete and Table Locks SQL Server 2012

  8. 8

    Delete duplicate records from a Postgresql table without a primary key?

  9. 9

    Delete duplicate data and load into another table in SQL Server

  10. 10

    Delete Duplicate Records in table using SQL

  11. 11

    Oracle SQL query: Delete the oldest duplicate records from the table

  12. 12

    How to delete all duplicate records from sql

  13. 13

    How to delete multiple records from SQL table

  14. 14

    How to Delete Duplicate records from Table in SQL Server?

  15. 15

    sql server delete duplicate records from a table after checking if these records are there in dependent table

  16. 16

    Update all but one of duplicate records in table in SQL Server

  17. 17

    SQL Server: How to allow duplicate records on small table

  18. 18

    How to delete a filegroup from a partitioned table (SQL Server 2012)

  19. 19

    SQL Server - Remove/Delete Specific part of the records of a table

  20. 20

    Delete the non-duplicate row from actual Database Table in SQL Server 2008 R2

  21. 21

    How to delete duplicate records from a table in oracle

  22. 22

    SQL Server: table design for changing Identity records

  23. 23

    SQL server 2012 - Query on retrieving matching records from a table by cross checking with two other tables

  24. 24

    Combining duplicate records in SQL Server

  25. 25

    SQL Server 2012 Merge Records in OUTER APPLY related table

  26. 26

    Delete user-defined indexes for a table in SQL Server 2012

  27. 27

    Select Except the duplicate Records from the table in SQL Server

  28. 28

    Remove duplicate records from views in SQL Server

  29. 29

    SQL Delete duplicate rows in the table without primary key on SQL Server

HotTag

Archive