title: SQL SERVER: How to delete duplicate rows in one table having one-to-many mapping with other
body: <p>I have a SQL Server containing duplicate rows w.r.t a particular column of a table <code>tableObj</code> which I want to delete. The query I am using is:</p>
  
  <pre><code>DELETE tableObj 
  WHERE id NOT IN 
  (SELECT MIN(id)
  FROM tableObj
  GROUP BY column_name);  
  </code></pre>
  
  <p>This <code>column_name</code> is duplicate and I want to remove all the other rows containing the same column. Now as I execute this query, i get following error:</p>
  
  <pre><code>The DELETE statement conflicted with the REFERENCE constraint "some_value". The conflict occurred in database "UH", table "Test.otherTable", column 'column_name'.
  </code></pre>
  
  <p>This <code>otherTable</code> is with which there is one-to-mapping of <code>tableObj</code>. Now how do I delete these references as well along with duplicate deletion.</p>
  
  <p>This is how I find the duplicates:</p>
  
  <pre><code>SELECT column_name, COUNT(*)
  FROM tableObj
  GROUP BY column_name
  HAVING 
  COUNT(*) &gt; 1
  </code></pre>
  
  <p>which gives me result like:</p>
  
  <pre><code>column_name   (no-specific column) 
  12345              2
  23414              3
  ...........
  ...........
  </code></pre>
  
  <p>The mapping is persisted in the mapping table like: </p>
  
  <pre><code>tableObjID         otherTableId
  </code></pre>
  
  <p>The desired result after executing the <code>DELETE</code> query is to get(COUNT=1):</p>
  
  <pre><code>column_name   (no-specific column) 
  12345              1
  23414              1
  ...........
  ...........
  </code></pre>