SQL

How to Delete Duplicate Rows in SQL?

intro

Deleting duplicate rows in relational database management systems isn’t always an easy task. Here’s how to delete duplicate rows in SQL!

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
mariadb MARIADB
THE MARIADB DATABASE
MySQL logo MySQL
THE MYSQL DATABASE
PostgreSQL logo POSTGRESQL
THE POSTGRESQL DATABASE

In this guide, you’ll learn everything you need to know to delete duplicate rows in SQL. We’ll cover practical techniques and reliable approaches you can use across different database systems.

Let’s dive in!

Introduction

Think about a time when you had to delete duplicate rows in a database management system: how did you go about it?

We all know that to delete rows, we can use the DELETE SQL statement like so:

Copy
        
1 DELETE FROM demo_table WHERE column = 'Value';

But what happens once we have a lot of rows to delete? What if we have a lot of duplicate rows to take care of? Let’s find out!

Understanding Duplicate Rows

First of all, what do we mean by saying “delete duplicate rows in SQL”? Everything’s quite simple: a “duplicate row” concerns multiple rows that have exactly the same values. Suppose we run a forum that has threads and posts:

Posts from an imaginary forum
Posts from an imaginary forum

Forums have threads and posts associated with them; threads and posts are crafted by users and the bigger your forum is, the higher the probability of your forum having quite a couple of duplicate records.

To delete records, all you need to do is issue a DELETE FROM statement like the one specified above. In reality, such a statement is likely to include the SQL LIKE clause that matches all rows starting with a specific string or an integer, but regardless, it will remove any rows that match the specified criteria:

Deleting all threads starting with Pokemon
Deleting all threads starting with “Pokemon”

Here, we ran an SQL query:

Copy
        
1 DELETE FROM `posts_af` WHERE `subject` LIKE 'Pokemon%';

Telling our database that:

  1. Our table is posts_af.
  2. We’re deleting anything based on the subject column.
  3. We want to delete all rows that are associated with the subject starting with “Pokemon”.

Our database then proceeded to delete all rows matching this criteria. If we want to delete duplicates, we need to take a different approach:

  1. Identify duplicate rows applicable to our use case.
  2. Delete them.

Sounds simple enough, right? But a query like the one above wouldn’t work anymore because it doesn’t delete duplicate rows in SQL, it simply deletes rows based on a criteria!

How to Actually Delete Duplicate Rows in SQL

To delete duplicate rows we would employ an SQL query like so:

Copy
        
1 DELETE FROM table_name WHERE id NOT IN ( 2 SELECT * FROM ( 3 SELECT MIN(id) FROM table_name GROUP BY column_1, column_2 4 ) AS sub);

Here:

  1. We specify that we will delete data based on the ID column and will want the ID column to not include anything that succeeds the inner query (SELECT * FROM ...)
  2. Find the minimum ID for each group of duplicates based on column_1 and column_2 (using an SQL subquery).
  3. Delete records with ID not in the aforementioned group.

We can also delete duplicate rows using SQL window functions like so:

Copy
        
1 WITH cte AS ( 2 SELECT *, ROW_NUMBER() OVER 3 (PARTITION BY column_1, column_2 ORDER BY id) AS row_number FROM table_name) 4 DELETE FROM table_name WHERE id IN ( SELECT id FROM cte WHERE row_number > 1 5 );

Above:

  1. We define an SQL CTE (Common Table Expression) and call it cte (WITH cte).
  2. For each set of rows that have the same values, assign a number ordered by their ID (row_number) In other words, we assign a row number to each set of duplicates and call it row_number.
  3. Keep the first row in the group (WHERE row_number > 1)
  4. Delete all other (duplicate) rows from the table_name table.

Bear in mind that SQL window functions aren’t available in older versions of MySQL (MySQL ≤ 8.)

Moving Beyond Duplicate Rows

So, duplicate rows can be deleted either by using CTEs or by utilizing subqueries. With that being said, there are other ways that you can use to delete duplicates in your data set. One of them concerns Linux systems (or Windows infrastructure with Cygwin or alike.)

If you have a raw data file (exported from MySQL using SELECT ... INTO OUTFILE or similar), using sort and uniq with a flag or two may be way easier because uniq acts on raw data thus being faster.

Employ uniq with sort like so:

Copy
        
1 sort /directory/to/file_duplicates.txt | uniq > /directory/to/file_unique.txt

Here, we specify the file containing duplicate rows (/directory/to/file_duplicates.txt), remove duplicates and put unique values (values without duplicates) in a file called /directory/to/file_unique.txt.

The downside of this approach is that you’d need your files to be formatted with new lines (”n”). In other words, you need each record to start on a new line. If that’s not the case, uniq will not work correctly.

Dealing with Duplicate Rows in SQL Clients

Remember that there are tools beyond sort and uniq , too. You can rely on SQL clients not only to make your SQL queries look more pretty but also to drag and drop tables that you want to query inside of them, as well as export data in a variety of formats to help with sort operations.

DbVisualizer
DbVisualizer

One of the most popular SQL clients, DbVisualizer, also comes with a free 21-day trial where you can try all of the features within the tool free of charge. SQL clients won’t help you get rid of duplicate rows, but they will alleviate data management & SQL script issues stemming from your use of database management systems, no matter what kind of database management system you find yourself using. With 50+ supported data sources, you will surely find your database in the list!

Conclusion

Although many of us know how best to delete data in our database management system, the task of delete duplicate rows in SQL is still a mystery to some; as you saw from the examples given in this blog, though, the reality is a little different.

Yes, deleting duplicate rows in relational database management systems is a little more difficult than issuing a simple DELETE query and hoping for the best, but at the same time, if you utilize CTEs, inner joins, or even subqueries, you will achieve your task in no-time.

Deleting data may only be a part of a bigger task though, and to achieve other tasks, you may need to turn to renowned SQL clients like DbVisualizer. You’re in luck because DbVisualizer does offer a free 21 day trial for you to try all of its features, so after you’re done testing, do inform us how it went through Twitter/X.

FAQ

How to delete duplicate rows in SQL?

To delete duplicate rows in SQL, consider using subqueries, Common Table Expressions or CTEs, SQL queries with an INNER JOIN functionality, or, if nothing else works out, consider sorting data with a uniq flag as shown in this blog. Additionally, consider reading the documentation of your database management system of choice as well as blogs and books on database topics to keep yourself up to date.

Why should I rely on DbVisualizer?

DbVisualizer is a versatile SQL client supporting more than 50+ data sources from ClickHouse to SQL Server. Its extensive features will help you interact with your database and work with your data as you see fit.

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL vs NoSQL Databases: A Practical Guide to Choosing the Right Database for Your Use Case

author Leslie S. Gyamfi tags Database system NOSQL SQL 13 min 2026-09-14
title

SQL for Data Analytics: 5 Advanced Techniques You Should Know

author Lukas Vileikis tags MySQL SQL 5 min 2026-09-07
title

Understanding and Using the MOD Function in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2026-08-31
title

Ensuring HIPAA Compliance in a Changing Data Landscape

author Lukas Vileikis tags SQL 5 min 2026-08-24
title

What Is a Composite Key in SQL and When to Use It

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2026-08-17
title

Best Practices for Using Git with Your Database

author Lukas Vileikis tags SQL 6 min 2026-07-27
title

Understanding SQL Index Maintenance in Open Source Databases

author Lukas Vileikis tags SQL 6 min 2026-06-22
title

INSERT INTO … SELECT Statement: What You Need to Know

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2026-06-15
title

Parsing Data with SUBSTRING_INDEX: A Complete Guide

author Lukas Vileikis tags MARIADB MySQL SQL 5 min 2026-06-08
title

SQL DROP TABLE Statement: Everything You Need To Know

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2026-06-01

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.