Write a query to delete duplicate records in sql server

Handling Duplicate Rows in SQL: A Step-by-Step Guide

Dealing with duplicate rows in a database table is a common challenge, and SQL provides various methods to address this issue. In this article, we'll explore a step-by-step guide on how to identify, handle, and remove duplicate rows from a table using SQL queries. The example table and queries provided demonstrate a practical approach to solving this problem.

Example Table

Let's start by examining an example table with duplicate rows:


<table class="table table-bordered">
    <thead style='background-color:#fafafa; color:#000000; border:1px solid #dddddd;'>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tr>
        <td>1</td>
        <td>A</td>
        <td>25</td>
    </tr>
    <tr>
        <td>2</td>
        <td>B</td>
        <td>26</td>
    </tr>
    <tr>
        <td>3</td>
        <td>C</td>
        <td>24</td>
    </tr>
    <tr>
        <td>2</td>
        <td>B</td>
        <td>26</td>
    </tr>
</table>

SQL Queries

Create and Populate the Table


Create Table t1
(
    id int,
    name char(10),
    age int
)

insert into t1
select 1, 'A', 25 union all
select 2, 'B', 26 union all
select 3, 'C', 24 union all
select 2, 'B', 26

Identify Duplicate Keys


Select id, name, count = count(*) 
into holdkey
from t1
Group by id, name 
Having count(*) > 1

Select Duplicate Rows


Select Distinct t1.*
into holddups
from t1, holdkey
Where t1.id = holdkey.id and t1.name = holdkey.name

Verify Unique Keys in holddups


Select id, name, count(*)
From holddups
Group by id, name

Delete Duplicate Rows from Original Table


Delete t1
From t1, holdkey
Where t1.id = holdkey.id

Explanation of the Markup Code

The HTML markup provided showcases a sample table with headers for 'id,' 'name,' and 'Age.' This table represents a scenario where duplicate rows need to be addressed.

The SQL queries demonstrate the creation of a table named t1 and the insertion of sample data. Subsequently, duplicate keys are identified and stored in a holding table (holdkey). Duplicate rows are then selected into another holding table (holddups) while ensuring uniqueness. The verification step ensures unique keys in holddups, and finally, duplicate rows are deleted from the original table (t1).

This step-by-step guide provides a practical approach to handle and remove duplicate rows from a database table using SQL queries. Adjust the queries based on your specific database schema and requirements.

Post a Comment

0 Comments