Let's go to SQL. Suppose a table has duplicate rows. How would you remove the duplicates?
π‘ 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