Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
SQL Query to Delete Duplicate Rows
Next article icon

Delete Duplicate Rows in MS SQL Server

Last Updated : 06 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In MS SQL Server, managing duplicate rows is a common task that can affect the integrity and performance of a database. To address this issue, SQL Server provides several methods for identifying and deleting duplicate rows.

In this article, We will explore three effective approaches: using the GROUP BY and HAVING clause, Common Table Expressions (CTE) and the RANK() function.

How to Delete Duplicate Rows in SQL Server?

To Delete Duplicate Rows in MS SQL Server, we will use the below method that helps us to perform delete duplicate rows from the table as defined below:

  1. Using GROUP BY and HAVING Clause
  2. Using Common Table Expressions (CTE)
  3. Using RANK() Function

To delete duplicate rows in MS SQL Server use the following syntax:

WITH CTE AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY [column1], [column2], ...
ORDER BY [column1], [column2], ...) AS RowNumber
FROM [table_name]
)
DELETE FROM CTE
WHERE RowNumber > 1;

Different Ways To Find And Delete Duplicate Rows From A Table In SQL Server

Let us look at an example, and learn how to delete duplicate rows in MS SQL Server. First, let’s create a table and insert some duplicate rows in the table. Let us create a table named Geek:

CREATE TABLE Geek(
Name NVARCHAR(100) NOT NULL,
Email NVARCHAR(255) NOT NULL,
City NVARCHAR(100) NOT NULL
);

INSERT INTO Geek (Name, Email, City) VALUES
('Nisha', '[email protected]', 'Delhi'),
('Megha', '[email protected]', 'Noida'),
('Khushi', '[email protected]', 'Jaipur'),
('Khushi', '[email protected]', 'Jaipur'),
('Khushi', '[email protected]', 'Jaipur'),
('Hina', '[email protected]', 'Kanpur'),
('Hina', '[email protected]', 'Kanpur'),
('Misha', '[email protected]', 'Gurugram'),
('Misha', '[email protected]', 'Gurugram'),
('Neha', '[email protected]', 'Pilani');

1. Using GROUP BY and HAVING Clause

Problem Statement:

Remove duplicate rows from the Geek table, keeping only one entry for each unique combination of Name, Email, and City.

SQL Query:

-- Select distinct rows to find duplicates
SELECT Name, Email, City
FROM Geek
GROUP BY Name, Email, City
HAVING COUNT(*) > 1;

Output:

Name Email City
Khushi [email protected] Jaipur
Hina [email protected] Kanpur
Misha [email protected] Gurugram

Explanation:

  • The GROUP BY clause groups rows by Name, Email, and City.
  • The HAVING COUNT(*) > 1 clause filters groups where there are more than one occurrence, indicating duplicates.

2. Using Common Table Expressions (CTE)

Problem Statement:

Remove duplicate rows from the Geek table while retaining only the first occurrence of each unique combination of Name, Email, and City.

SQL Query:

WITH CTE AS (
SELECT
Name,
Email,
City,
ROW_NUMBER() OVER (PARTITION BY Name, Email, City ORDER BY (SELECT NULL)) AS rn
FROM Geek
)
-- Delete duplicates where the row number is greater than 1
DELETE FROM CTE
WHERE rn > 1;

This query does not produce a direct output but removes the duplicate entries.

Explanation:

  • The ROW_NUMBER() function assigns a unique sequential integer to rows within a partition of Name, Email, and City.
  • PARTITION BY specifies the columns used to identify duplicates.
  • ORDER BY (SELECT NULL) allows row numbers to be assigned arbitrarily.
  • The DELETE statement removes rows where rn > 1, keeping only the first occurrence.

3. Using RANK() Function

Problem Statement:

Remove duplicate rows from the Geek table while retaining only the row with the highest rank for each unique combination of Name, Email, and City.

SQL Query:

WITH RankedCTE AS (
SELECT
Name,
Email,
City,
RANK() OVER (PARTITION BY Name, Email, City ORDER BY (SELECT NULL)) AS rnk
FROM Geek
)
-- Delete duplicates where the rank is greater than 1
DELETE FROM Geek
WHERE EXISTS (
SELECT 1
FROM RankedCTE
WHERE RankedCTE.Name = Geek.Name
AND RankedCTE.Email = Geek.Email
AND RankedCTE.City = Geek.City
AND RankedCTE.rnk > 1
);

Output:

This query does not produce a direct output but removes the duplicate entries.

Explanation:

  • The RANK() function assigns a rank to each row within a partition of Name, Email, and City.
  • Rows with the same rank will have the same value within the partition.
  • The DELETE statement removes rows where rnk > 1, ensuring only the highest-ranked row (first occurrence) is kept.

Conclusion

Deleting duplicate rows in SQL Server is crucial for maintaining clean and accurate datasets. The methods outlined—using GROUP BY with HAVING, Common Table Expressions (CTE), and the RANK() function—offer versatile solutions for different scenarios. Whether you need to identify duplicates, remove them while retaining the first occurrence, or prioritize rows based on ranking, SQL Server provides robust tools to achieve these goals



Next Article
SQL Query to Delete Duplicate Rows

K

khushboogoyal499
Improve
Article Tags :
  • Databases
  • SQL
  • DBMS-SQL
  • SQL-Server

Similar Reads

  • How to Delete Duplicate Rows in PL/SQL?
    Inconsistencies and inefficiencies in data management are frequently caused by duplicate rows in a database table. Eliminating duplicate rows is a typical PL/SQL activity to maintain data integrity and improve database performance. This article will guide you on how to remove duplicated rows in PL/S
    4 min read
  • How to Delete Duplicate Rows in MySQL?
    Duplicate rows are a common problem in MySQL databases. Duplicate rows can cause problems with data accuracy and integrity. They can also make it difficult to query and analyze data. Ways to Delete Duplicate Rows in MySQLThere are a few different ways to delete duplicate rows from tables in MySQL: U
    4 min read
  • How to delete duplicate rows in SQLite?
    SQLite is an open-source and serverless database system that does not require any server to perform various queries also it is widely used in the development of embedded software like television and mobile phones Sometimes it might happen that we by mistake insert multiple times similar data into ta
    3 min read
  • Find Duplicates in MS SQL Server
    Finding duplicate values in a database is a common task when managing data integrity. In SQL, several methods can be employed to identify and handle duplicate entries. In this article, We will explore two effective techniques for locating duplicates using SQL queries: the GROUP BY clause and the ROW
    4 min read
  • SQL Query to Delete Duplicate Rows
    Duplicate rows in a database can cause inaccurate results, waste storage space, and slow down queries. Cleaning duplicate records from our database is an essential maintenance task for ensuring data accuracy and performance. Duplicate rows in a SQL table can lead to data inconsistencies and performa
    6 min read
  • Delete Database in MS SQL Server
    Prerequisite – Introduction of MS SQL Server and Create Database in MS SQL Server System databases can't be deleted, only user databases could be deleted. Data and log files will automatically be deleted from disk with database deletion. To delete a database, the below methods could be used – SQL Se
    1 min read
  • How to Delete Duplicate Rows in MariaDB
    Duplicate rows in a database can lead to data inconsistencies and inefficiencies. In MariaDB, we can remove duplicate rows using various methods to ensure data integrity and optimize database performance. In this article, We will explore different techniques to identify and delete duplicate rows in
    4 min read
  • Distinct clause in MS SQL Server
    The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values and sometimes we only want to list the different (distinct) values. Consider a simple database and we will be discussing distinct clauses in MS SQL Server.
    3 min read
  • Delete Action in MS SQL Server
    Prerequisite - Foreign key in MS SQL Server A column can be inserted in a child table only if it is a part of the parent table in case of a foreign key. The foreign key allows us to perform other actions called Referential Actions. Referential actions allow to update or delete a row in case of the p
    2 min read
  • SQL Server INSERT Multiple Rows
    SQL Server is a relational Database Management System(RDBMS). It offers various features for creating, and managing databases with its efficient tools. It can handle both small-scale and large-scale industry database applications. INSERT Statement in SQL ServerThe Insert statement is a command of th
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences