title: How to combine two SQL queries where queries are joined by union
body: <p>Can anyone please help me in writing a single query joining these two queries.
  I am using IBM DB2.</p>
  
  <pre><code>(SELECT 
    TABLE1.COLS,TBLE2.COLS,TABLE3.COLS 
   FROM 
    TABLE1,TABLE2,TABLE3,TABLE_PROB  
   WHERE 
    TABLE_PROB.COL=TABLE1.COL,OTHER_CLAUSE ) 
  UNION      
  (SELECT 
    TABLE1.COLS,TBLE2.COLS,TABLE3.COLS 
   FROM 
    TABLE1,TABLE2,TABLE3,TABLE_PROB1  
   WHERE TABLE_PROB1.COL=TABLE1.COL,OTHER_CLAUSE )
  </code></pre>
  
  <p>The two queries before and after <code>union</code> are same except that instead of "TABLE_PROB" it is changed to "TABLE_PROB1". There are no columns is to be selected from both the tables, they are only used to filter in the <code>where</code> clause.</p>
  
  <p>Can anyone tell me how to combine both into a single query. </p>
  
  <p>This query can be considered for the following scenario.
   There are few employee details table which contains details of all employees. 
     "TABLE_PROB" contains list of contract employees and "TABLE_PROB1" contains list of permanent employees. I need to get the details of both the contract and not contract employees based on few criteria.</p>
  
  <p>Since the query has big Whereclause and select clause firing two queries by using union,increases the cost of the query. So I need to merge it by making a single query.
  Thanks for the help in advance. </p>