How not to display an instance of a widget dynamically?

The description of your problem is very minimal but the obvious solution is simply to output widget data only when the query is not empty:

class cond_widget extends WP_Widget {

  public function __construct() {
    // widget actual processes  
    parent::__construct(
        'test', // Base ID
        'test', // Name
        array( 'description' => __( 'test', 'foo'))
    );
  }

  public function widget( $args, $instance ) {

    $qry = $wpdb->get_var('whatever');
    if (!empty($qry)) {
      echo $args['before_widget'];

      if (!empty($instance['title'])) {
        $title = $instance['title'];
        echo $args['before_title'].$title.$args['after_title'];
      }

      echo '<ul class="recent_pages">'; 

      echo '</ul>';
      echo $args['after_widget'];
    }
  }

}
add_action( 
  'widgets_init', 
  function(){
     register_widget( 'cond_widget' );
  }
);