code: protected function queryScalar($selectExpression, $db)
      {
          /* @var $modelClass ActiveRecord */
          $modelClass = $this->modelClass;
          if ($db === null) {
              $db = $modelClass::getDb();
          }
  
          if ($this->sql === null) {
              return parent::queryScalar($selectExpression, $db);
          }
  
          $command = (new Query())->select([$selectExpression])
              ->from(['c' => "({$this->sql})"])
              ->params($this->params)
              ->createCommand($db);
          $this->setCommandCache($command);
  
          return $command->queryScalar();
      }
docs: {@inheritdoc}