How to implement UNION with custom field through Drupal Database API

I had encountered a problem recently and want to share with you. There was a scenario where I wanted to implement UNION with custom fields for table1 and table2 to identify which records belongs to which table. Following is the solution for the same:

Problem : I want to fetch 45 results from a single table which will include search results as well as other remaining results to complete my 45 count.

  $filter = 'test';

  //this is for search results
  $search_results = db_select('table1', 'tb1')
        ->fields ('tb1')
        ->condition('tb1.name', '%' . db_like($filter) . '%', 'LIKE'); //filter results
  $search_results->addExpression(':is_search', 'is_search', array(':is_search' => 'yes'));

  //this is without search results
  $other_results = db_select('table2', 'tb2')
        ->fields ('tb2')
        ->condition('tb2.name', '%' . db_like($filter) . '%', 'NOT LIKE'); //Only approved entries
  $other_results->addExpression(':is_search1', 'is_search', array(':is_search1' => 'no'));

  $search_results->union($other_results, 'UNION');

  $clubed_results = db_select($search_results,'cr')->fields('cr');
  $clubed_results = $clubed_results->extend('PagerDefault');
  $clubed_results->limit(45);

  $results = $clubed_results->execute()->fetchAll();

This will return all the results with name containing keyword test and the other results followed by the search results.

Hope this helps :)