Skip to main content

Deleting Duplicate Rows ๐Ÿงน

Mentor's Note: Duplicates are the "Dust" of your database. They happen because of bugs, missing constraints, or bad imports. Cleaning them is a vital skill for every Data Engineer. ๐Ÿ’ก


๐ŸŒŸ The Scenario: The Double Invite ๐Ÿ’Œโ€‹

Imagine you are sending wedding invitations.

  • The Problem: "Rahul Sharma" is accidentally in your list twice.
  • The Goal: You want to delete the extra copy but keep one original.
  • The Challenge: Since they are exactly the same, how do you tell the robot which one to throw away?

๐Ÿ’ป Method 1: Using ROWID (Oracle) ๐Ÿ†”โ€‹

Oracle gives every row a unique physical address called a ROWID.

-- Scenario: Keep the first copy, delete others
DELETE FROM students
WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM students
GROUP BY name, email -- Define what makes them a 'duplicate'
);

๐Ÿ’ป Method 2: Using CTE & ROW_NUMBER() (SQL Server / Postgre) ๐Ÿ“Šโ€‹

This is the most modern and readable way.

WITH CTE AS (
SELECT
id,
ROW_NUMBER() OVER (
PARTITION BY name, email
ORDER BY id
) AS occurrence
FROM students
)
DELETE FROM CTE WHERE occurrence > 1;

๐Ÿ’ป Method 3: Using a Temporary Table (Universal) ๐Ÿ“‘โ€‹

This works in every database and is very safe.

  1. Select distinct records into a new table.
    CREATE TABLE students_clean AS
    SELECT DISTINCT * FROM students;
  2. Drop the old table.
    DROP TABLE students;
  3. Rename the clean table.
    RENAME students_clean TO students;

๐ŸŽจ Visual Logic: The Sieve Methodโ€‹


๐Ÿ’ก Prevention Tipโ€‹

"The best way to handle duplicates is to prevent them! Always use UNIQUE CONSTRAINTS on your email or phone number columns so the database rejects the duplicate before it even gets in."


๐Ÿ“ˆ Learning Pathโ€‹

๐Ÿ“ Visit Us

๐Ÿซ VD Computer Tuition Surat

VD Computer Tuition
๐Ÿ“ Address
2/66 Faram Street, Rustompura
Surat โ€“ 395002, Gujarat, India
๐Ÿ“ž Phone / WhatsApp
+91 84604 41384
๐ŸŒ Website

Computer Classes & Tuition โ€” Areas We Serve in Surat

Adajanโ€ขAlthanโ€ขAmroliโ€ขAthwaโ€ขAthwalinesโ€ขBhagalโ€ขBhatarโ€ขBhestanโ€ขCanal Roadโ€ขChowkโ€ขCitylightโ€ขDumasโ€ขGaurav Pathโ€ขGhod Dod Roadโ€ขHaziraโ€ขJahangirpuraโ€ขKamrejโ€ขKapodraโ€ขKatargamโ€ขLimbayatโ€ขMagdallaโ€ขMajura Gateโ€ขMota Varachhaโ€ขNanpuraโ€ขNew Citylightโ€ขOlpadโ€ขPalโ€ขPandesaraโ€ขParle Pointโ€ขPiplodโ€ขPunaโ€ขRanderโ€ขRing Roadโ€ขRustampuraโ€ขSachinโ€ขSalabatpuraโ€ขSarthanaโ€ขSosyo Circleโ€ขUdhnaโ€ขVarachhaโ€ขVed Roadโ€ขVesuโ€ขVIP Road
๐Ÿ“ž Call Sir๐Ÿ’ฌ WhatsApp Sir