title: Multiple conditions for AND and OR in WHERE clause SQL Server
body: <p>I am working in Microsoft SQL Server Management Studio. I have multiple tables I'm working with and multiple conditions (nearly 80) that I am querying on where I am trying to achieve a subset of joined data. </p>
  
  <p>The idea is that I need to grab data from a join, where that data is not in a different join based on a condition (i.e., <code>NOT IN</code>), then I need to filter on a bunch of conditions involving multiple columns and multiple conditions on each column. My problem (I think) is arising from the <code>OR</code> condition. I have a column, say <code>col5</code>, where if it doesn't satisfy any of the conditions in the <code>AND</code> condition, then I need to filter further on it (hence, the <code>OR</code> condition).</p>
  
  <p>What I have written so far is the following,</p>
  
  <pre><code>SELECT 
      handfulOfColumns
  FROM 
      table1 AS t1
  LEFT JOIN
      (SELECT *
       FROM table2
       WHERE col2 = 'thing0' or col2 = 'thing1') AS t2 ON t1.col1 = t2.col1
  WHERE 
      t1.col1 NOT IN (SELECT t1.col1
                      FROM table1 t1
                      LEFT JOIN table3 t3 ON t1.col1 = t3.col1
                      WHERE col3 LIKE '%thing3%')
      AND (col4 &gt; '#' AND col5 NOT LIKE 'thing4' 
           AND col5 != 'thing5' AND col5 NOT LIKE 'thing6' 
           AND col6 NOT LIKE 'thing7'
           --...
           --... add like 20 lines of conditions similar to above
           --...
           AND col34 NOT LIKE 'thing77' AND LEN(col35) &gt; '#')
       OR (col5 NOT LIKE '% %' AND col5 LIKE '%[a-z]%' 
           AND col5 NOT LIKE '%[0-9]%' 
           AND col5 NOT LIKE 'thing209' 
           AND col5 NOT LIKE 'thing210');
  </code></pre>
  
  <p>The reasoning for this is I have a lot of (messy) phone numbers I am dealing with in <code>col5</code>, and overall there is a lot of cleaning to be done on all the columns. Some of these phone numbers will have text entered instead, and depending on the context I may want to keep them or filter them out (i.e. cases where the phone number is entered as 'business name', or 'business', or 'alskfjalsdkjf', rather than '555-5555' or '555 5555' etc). If the data meets the criteria of the <code>AND</code> conditions (mainly filtering out variations of <code>col5 not like '%555-5555%'</code>), then I need to further check and filter for any non-numeric characters in <code>col5</code> through the <code>OR</code> condition.</p>
  
  <p>There are no error messages - instead the query takes a really long time to finish and gives me more rows of data than I started with which is the opposite of what should be happening. Based on the structure of <code>table1</code> and <code>table2</code>, there is potential to introduce duplicated rows by joining the two tables, but the amount of conditioning and sub-setting I am doing should give me far less than the original number of rows even with a few duplicates here and there.</p>
  
  <p>I am not sure where my logic is failing for this, any help is appreciated :)</p>