title: In SQL, how do you check for similar row values to another
body: <p>I'm using SQL Server (T-SQL). I have a column with the row values A, AB, ABC, AC. I want to remove any values that are contained with another row. In this case I’d be left with ABC and AC since A and AB and contained in the other two.</p>
  <p>My thought is to take each value of the column and use LIKE to search through the whole column and count the number of results returned If it is equal to 1 then it is not contained in other rows.</p>
  <p>Is that a good way to do it? I ask because I’m reluctant to use loops/cursors.</p>
  <p>Thanks</p>
  <p>Here is a code sample taken from the explanation above:</p>
  <pre><code>CREATE TABLE #t (words varchar(10))
  INSERT INTO #t 
  VALUES ('A'),('AB'),('ABC'),('AC')
  </code></pre>
  <p>Using cursors, I think I'd do something like:</p>
  <pre><code>DECLARE @branches TABLE (words varchar(10), n int)
  DECLARE @word VARCHAR(10)
  DECLARE cursor_word CURSOR
  
  FOR SELECT words FROM #t
  
  OPEN cursor_word;
  FETCH NEXT FROM cursor_word INTO @word
  WHILE @@FETCH_STATUS = 0
      BEGIN
          INSERT INTO @branches SELECT @word, COUNT(*) FROM #t WHERE words like CONCAT('%', @word ,'%')
          FETCH NEXT FROM cursor_word INTO @word
      END
  
  CLOSE cursor_word
  DEALLOCATE cursor_word
  
  SELECT * FROM @branches WHERE n = 1
  </code></pre>