Custom Query – Based on user input

While this is primarily a SQL question, there are WordPress components and Core has no efficient mechanism to pull this data that I am aware of. This has long been a problem in Core in my opinion. So:

global $wpdb;

// group name key
$meta_key = '_create_new_group';
if (!empty($_REQUEST['q'])) {
  // user input
  $search = $_REQUEST['q'].'%';

  $sql = "SELECT meta_value 
  FROM {$wpdb->postmeta} 
  WHERE meta_key = '{$meta_key}' 
  AND meta_value LIKE %s";
  $sql = $wpdb->prepare($sql,$search);
  // var_dump($sql);
  $groupnames = $wpdb->get_col($sql);
}

Notes:

  1. Don’t do anything if $_REQUEST['q'] isn’t set. That is the if
    (!empty($_REQUEST['q'])) {
    conditional
  2. You do not need to escape hard coded values, only the user supplied
    ones. In your case, that is $search and only $search.