How to display the names of users from a specific group with a shortcode?

I have in the database a table with the user_id and corresponding group_id values, so each row contain the ID of the user that is a member of a group and the related group ID. The question is: How to display the names of users from a specific group, plus some metadata, with a shortcode, for example [group_members group_id=1]? With the bellow function, that I try to adapt for this without success, I can to display only the user ID or the group ID.

add_shortcode( 'group_members',  'group_members_shortcode_handler' );

function group_members_shortcode_handler ( $atts, $content = null ) {    

  global $wpdb;
  $querystr = "SELECT * FROM wp_groups_user_group";
  $users = $wpdb->get_results($querystr, OBJECT);

  foreach ($users as $user) {
    $output .= $user->group_id;
  }

 return $output;
}

Solutions Collecting From Web of "How to display the names of users from a specific group with a shortcode?"

Below is my own answer to the question. I will appreciate any constructive comment.

add_shortcode( 'group_members',  'group_members_shortcode_handler' );

function group_members_shortcode_handler ( $atts, $content = null ) {    

  global $wpdb;
  $querystr = "SELECT * FROM wp_groups_user_group";
  $users = $wpdb->get_results($querystr, OBJECT);

  $output='';
  foreach ($users as $user) {
    if($user->group_id == $atts['group_id']){
      $firstName = esc_html(get_user_meta($user->user_id, 'first_name', true));
      $lastName = esc_html(get_user_meta($user->user_id, 'last_name', true));
      $output .= '<li>' . $firstName . ' ' . $lastName . ' - ' . esc_html(get_user_meta($user->user_id, 'teaching_position', true)) . '</li>' . PHP_EOL;
    }
  }

  return $output;
}