搜索此博客

2013年2月23日星期六

Remove Duplicate Tuples in Database Tables

You have a database table Emp with data as follows:

EmpId FirstName LastName
1 Bob Lync
2 Sarah John
3 Bob Lync
4 John Doe
5 Stanly Jeff
6 Sarah John

With a single SQL query, how will you cleanup the database (eliminate redundant data from above table)

Solution: using group by, and we can find the first appearance of the empid of each user.

DELETE FROM emp 
WHERE empid NOT IN 
(SELECT MIN(empid)
FROM emp 
GROUP BY firstName , lastName);

没有评论:

发表评论