Duplicate rows in a database can create serious issues—whether it's inconsistent reporting, skewed analytics, or redundant transactions. In an ideal world, database tables would always have proper keys and constraints to avoid this. However, due to import errors, missing constraints, or manual entry, duplicates can and do occur.
Thankfully, SQL provides a number of effective methods for detecting and deleting duplicate rows from your tables. Whether you're working with a table that has unique constraints or one that doesn’t, this post will walk you through several reliable approaches to remove duplicate records using SQL.
Before diving into the solutions, it’s important to understand the consequences of duplicates:
It’s always recommended to enforce primary keys or unique indexes to prevent duplicates. But when prevention isn’t in place, these SQL strategies will help clean your data.
Before jumping into deletion, it's crucial to accurately identify duplicate rows in your table. Deleting the wrong records can result in data loss, so verification is a critical step.
SELECT column1, column2, COUNT(*) AS duplicate_count
FROM your_table
GROUP BY column1, column2
HAVING COUNT(*) > 1;
This query returns rows that appear more than once, allowing you to see which records are duplicated and how often they occur.
This method works when you want to identify duplicates based on a subset of columns (e.g., name and score), and then remove extra occurrences.
CREATE TABLE Students (
RegNo INT,
Name VARCHAR(50),
Marks INT
);
INSERT INTO Students VALUES
(1, 'Tom', 77),
(2, 'Lucy', 78),
(3, 'Frank', 89),
(4, 'Jane', 98),
(5, 'Robert', 78),
(3, 'Frank', 89),
(5, 'Robert', 78),
(4, 'Jane', 98);
SELECT Name, Marks, COUNT(*) AS count
FROM Students
GROUP BY Name, Marks
HAVING COUNT(*) > 1;
It will return rows that appear more than once. To delete them, you’ll need to use a temporary table or CTE (see next approach), because SQL doesn’t allow deleting from grouped results directly.

It is the most flexible method, especially for tables without a primary key or unique index.
WITH RankedRows AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY RegNo, Name, Marks ORDER BY RegNo) AS row_num
FROM Students
)
DELETE FROM RankedRows
WHERE row_num > 1;
The RANK() function also helps find duplicates. It's similar to ROW_NUMBER() but allows for ties.
CREATE TABLE Animals (
sno INT,
Animal_id INT,
Animal_name VARCHAR(50)
);
WITH RankedAnimals AS (
SELECT *,
RANK() OVER (PARTITION BY Animal_id, Animal_name ORDER BY sno DESC) AS rk
FROM Animals
)
DELETE FROM RankedAnimals WHERE rk > 1;
Self-joins are useful when there is no window function support (e.g., in some older systems).
DELETE A
FROM Students A
INNER JOIN Students B
ON A.Name = B.Name AND A.Marks = B.Marks
WHERE A.RegNo > B.RegNo;
A CTE provides a way to create a temporary result set. With it, we can perform more readable and modular deletes.
CREATE TABLE Employ_DB (
emp_no INT,
emp_name VARCHAR(50),
emp_address VARCHAR(100),
emp_eoj DATE
);
INSERT INTO Employ_DB VALUES
(11, 'Mohith', 'Tokyo', '2000-05-12'),
(12, 'Sana', 'Delhi', '2001-08-22'),
(11, 'Mohith', 'Tokyo', '2000-05-12');
WITH DuplicateEmployees AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY emp_no, emp_name ORDER BY emp_eoj) AS rn
FROM Employ_DB
)
DELETE FROM DuplicateEmployees
WHERE rn > 1;
It is similar to previous methods but tailored for structured business data.
If you're using SQL Server in an enterprise setting, SSIS provides visual tools to remove duplicates.
SSIS is powerful for ETL (Extract, Transform, Load) workflows and suits batch deduplication tasks.

While it's important to know how to delete duplicates, it's even more critical to prevent them from occurring in the first place.
Prevention requires thoughtful design and validation rules, but it saves time and effort in the long run.
Duplicate data is a silent threat to your database’s integrity. It causes reporting errors, inefficiencies, and inconsistent behavior across applications. Thankfully, SQL provides powerful tools like GROUP BY, ROW_NUMBER(), RANK(), and even self-joins to handle duplicates efficiently.
Whether you're working on a small side project or managing enterprise-level databases, understanding how to remove duplicate rows in SQL is a must-have skill. The method you choose depends on your database structure, available functions, and whether or not your table has a primary key.
Know the essential distinctions that separate CNNs from GANs as two dominant artificial neural network designs
Explore if AI can be an inventor, how copyright laws apply, and what the future holds for AI-generated creations worldwide
Explore the differences between Llama 3 and Llama 3.1. Compare performance, speed, and use cases to choose the best AI model.
From 24/7 support to reducing wait times, personalizing experiences, and lowering costs, AI in customer services does wonders
How logic and reasoning in AI serve as the foundation for smarter, more consistent decision-making in modern artificial intelligence systems
Learn AI and machine learning for free in 2025 with these top 10+ courses from leading platforms, universities, and tech experts
Get a clear understanding of supervised learning, including how it works, why labeled data matters, and where it's used in the real world—from healthcare to finance
AI Hallucinations happen when AI tools create content that looks accurate but is completely false. Understand why AI generates false information and how to prevent it
A lack of vision, insufficient AI expertise, budget and cost, privacy and security concerns are major challenges in AI adoption
Boost teacher productivity with AI-generated lesson plans. Learn how AI lesson planning tools can save time, enhance lesson quality, and improve classroom engagement. Discover the future of teaching with AI in education
Business professionals can now access information about Oracle’s AI Agent Studio integrated within Fusion Suite.
GANs and VAEs demonstrate how synthetic data solves common issues in privacy safety and bias reduction and data availability challenges in AI system development