For an interview, I had to write a SQL-Query that deletes BestellNummer and Type duplicates from a tabel.
I wasn't allowed to use a temp table and should do it in one query.
With the help of another question on stackoverflow, I came to this solution (T-SQL: Deleting all duplicate rows but keeping one):
DELETE FROM auftrag WHERE ID NOT IN
(
SELECT MIN(ID) FROM auftrag GROUP BY BestellNummer, Type
)
The auftrag table looked like this:
ID BestellNummer Type Number
0 123 O 1000
1 123 O 1001
2 123 E 1002
3 512 O 1003
4 512 O 1004
5 732 E 1005
The query now deletes ID 1 and 4, cause they are duplicates.
My question is, how does this query actually work? I can make out some bits, but I am a little confused of it.
It would be nice, if someone could give me a breakdown of how it works (: