Normally you don't need to bother about the SQL for this. Instead you can use the functions in accesslib.php. Something like:
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$users = get_users_by_capability($context, 'mod/form:view');
Roles and permissions
SQL for fetching users with view capability needed
This discussion has been locked so you can no longer reply to it.