SQL Duplicate Rows

BIS
2 min readFeb 8, 2020

--

How to select duplicate rows in a table?

Assume DUMMY_TABLE exists and KEY_COLUMN exists

DUMMY_TABLE has COLUMN_1 and COLUMN_2

SELECT KEY_COLUMN, COLUMN_1, COLUMN_2 from DUMMY_TABLE
GROUP BY KEY_COLUMN, COLUMN_1, COLUMN_2
HAVING COUNT(*) > 1

The important point to note is HAVING because unlike WHERE HAVING filters on aggregation functions.

By executing the above example if any rows are returned then we have duplicate rows. It is simple to find out where the table leak has taken place and then what needs to be done.

Our query will return the list of duplicate rows.

How SQL query works as follow:

GROUP BY clause groups the rows into groups by values KEY_COLUMN, COLUMN_1 and COLUMN_2 columns.

The COUNT(*) returns the number of occurrence for each group (KEY_COLUMN, COLUMN_1, COLUMN_2)

Final HAVING clause only retains DUPLICATE GROUPS. A GROUP having more than one COUNT.

In some databases, ROW_NUMBER() function can be used to find duplicate rows based on KEY_COLUMN, COLUMN_1 and COLUMN_2.

In reality, a UNIQUE constraint is used for a uniqueness for each row with one or many columns of a table. Normally constraint is broken if someone drops it for cleanup or for maintenance. When this happens then we need to find duplicate rows and provide manual delete function, stored procedure or nested SQL query.

Please see the next article for how to delete duplicate rows.

Please visit Business Integration Software to see various products using databases especially Online Exam Software.

Online Test Software

Exam Software

Examination Software

Question Bank Software

Online Exam Software

Online Test System

Online Examination Software

Online Assessment System

Assessment Management Software

Multiple Choice Question Software

Examination Management Software

Exam Management Software

Examination Management System

Exam Management System

Online Examination System

MCQ Software

Online Assessment Software

Test Management Software

Online Assessment Software

Exam System

Examination System

RHEED Software

MLM Software

Service Management Software

Online Timesheet Software

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

BIS
BIS

Written by BIS

Specialist in Cloud Native development

No responses yet

Write a response