As in the following example, you can obtain the SQL code in string type with the help of the $select->getSqlString() method in any model class and in a method where the $select object exists.
$sql = new Sql($this->adapter);
$select = $sql->select();
$select->columns([
'permId',
'route',
'method',
'action',
]);
$select->join(
['rp' => 'rolePermissions'],
'permissions.permId = rp.permId', [], $select::JOIN_INNER);
$select->join(
['r' => 'roles'],
'r.roleId = rp.roleId', ['roleKey','roleLevel'], $select::JOIN_LEFT);
echo $select->getSqlString($adapter->getPlatform());
die;
/*
SELECT `permissions`.`permId` AS `permId`, `permissions`.`route` AS `route`, `permissions`.`method` AS `method`,
`r`.`roleKey` AS `roleKey`, `r`.`roleLevel` AS `roleLevel` FROM `permissions` INNER JOIN `rolePermissions` AS `rp` ON
`permissions`.`permId` = `rp`.`permId` LEFT JOIN `roles` AS `r` ON `r`.`roleId` = `rp`.`roleId`
*/