TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2617 7.07K
๐Ÿ“Š Interviewer: How do you remove duplicate records in SQL?

๐Ÿ‘‹ Me: We can remove duplicates using DISTINCT, GROUP BY, or delete duplicate rows using ROW_NUMBER().


โœ… 1๏ธโƒฃ Using DISTINCT (to fetch unique values)

SELECT DISTINCT column_name
FROM employees;


๐Ÿ‘‰ Returns unique records but does not delete duplicates.


โœ… 2๏ธโƒฃ Using GROUP BY (to identify duplicates)

SELECT name, COUNT(*)
FROM employees
GROUP BY name
HAVING COUNT(*) > 1;


๐Ÿ‘‰ Helps find duplicate records.


โœ… 3๏ธโƒฃ Delete Duplicates Using ROW_NUMBER() (Most Important โญ)
(Keeps one record and deletes others)

DELETE FROM employees
WHERE id IN (
SELECT id FROM (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY name, salary
ORDER BY id
) AS rn
FROM employees
) t
WHERE rn > 1
);


๐Ÿง  Logic Breakdown:

- DISTINCT โ†’ shows unique records
- GROUP BY โ†’ identifies duplicates
- ROW_NUMBER() โ†’ removes duplicates safely


โœ… Use Case: Data cleaning, ETL processes, data quality checks.

๐Ÿ’ก Tip: Always take a backup before deleting duplicate records.

๐Ÿ’ฌ Tap โค๏ธ for more!
  • โค 17
More from @sqlspecialist
  1. Oct 9, 2026โ€œHere, the data is sorted by the second column in descending order and the first five rowsโ€ฆ
  2. Oct 9, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 6 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  4. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  5. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  6. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’