Home β€Ί Interview Questions β€Ί Let's go to SQL. Suppose a table has duplicate row…

Let's go to SQL. Suppose a table has duplicate rows. How would you remove the duplicates?

🟒 Easy Coding Fresher level
1Times asked
Jun 2026Last seen
Jun 2026First seen

πŸ’‘ Model Answer

You can remove duplicates by keeping only one copy of each row. One way is to create a temporary table with DISTINCT rows:

CREATE TABLE temp AS

SELECT DISTINCT * FROM original_table;

Then drop the original and rename:

DROP TABLE original_table;

ALTER TABLE temp RENAME TO original_table;

Alternatively, if you need to delete in place and the table has a primary key or unique identifier, use a DELETE with a window function:

DELETE FROM original_table

WHERE id NOT IN (

SELECT MIN(id)

FROM original_table

GROUP BY col1, col2, col3 -- columns that define uniqueness

);

This keeps the row with the smallest id for each duplicate group. The first method is simpler but requires extra space; the second works directly on the table.

This answer was generated by AI for study purposes. Use it as a starting point β€” personalize it with your own experience.

🎀 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers β€” invisible to screen sharing.

Get Assisting AI β€” Starts at β‚Ή500