UNION is used to combine the result from multiple
SELECT statements into a single result set.

@param SelectQuery $query the query to combine

@return self
357104
public function union(SelectQuery $query): self
    {
        $this->union[] = ['', $query->build(false)];

        return $this;
    }
1334208
public function union(): self
    {
        $query = new self;
        $query->clauses = $this->clauses->add(
            new Clause\UnionClause
        );

        return $query;
    }
795417
protected function _selectQueryTranslator($query)
    {
        $limit = $query->clause('limit');
        $offset = $query->clause('offset');

        if ($limit && $offset === null) {
            $query->modifier(['_auto_top_' => sprintf('TOP %d', $limit)]);
        }

        if ($offset !== null && !$query->clause('order')) {
            $query->order($query->newExpr()->add('(SELECT NULL)'));
        }

        if ($this->_version() < 11 && $offset !== null) {
            return $this->_pagingSubquery($query, $limit, $offset);
        }

        return $this->_transformDistinct($query);
    }
1030500
public static function render(Query\Union $query)
    {
        return Compiler::withDb($query->getDb(), function () use ($query) {
            return Compiler::expression(array(
                Arr::join(' UNION ', Arr::map(function (Query\Select $select) {
                    return Compiler::braced(Select::render($select));
                }, $query->getSelects())),
                Compiler::word('ORDER BY', Direction::combine($query->getOrder())),
                Compiler::word('LIMIT', $query->getLimit()),
            ));
        });
    }
1029292
public function unionAll(SelectQuery $query)
    {
        $instance = $this->selectQueryInstance();

        $instance->selectQuery()->unionAll($query);

        return $instance;
    }
98376
public function whereNotExists(SelectQuery $sql)
    {
        $this->wConditions = null;

        $this->exists = [
            'sql'    => $sql,
            'negate' => true,
        ];

        return $this;
    }
213978
public function withQuery(array $query = [])
    {
        // Set Query in class.
        $this->query = collect($this->query)->merge($query)->all();

        return $this;
    }
951212
public function mergeBindings(self $query)
    {
        $this->bindings = array_merge_recursive($this->bindings, $query->bindings);

        return $this;
    }
621076
public function withQuery($query)
    {
        $new = clone $this;
        try {
            $new->setQuery($query);
        } catch (InvalidArgumentException $ex) {
            throw new InvalidArgumentException('Invalid query provided.', null, $ex);
        }

        return $new;
    }
680512
protected function compileUnions(Builder $query)
	{
		$sql = '';

		foreach ($query->unions as $union)
		{
			$sql .= $this->compileUnion($union);
		}

		return ltrim($sql);
	}
164406
public static function mergeQuery($uri, $query)
    {
        return $uri->withQuery((string) (new Query(self::filterUri($uri)->getQuery()))->merge($query));
    }
198312
protected function addJoinTable(Select $query)
    {
        $relationTable = $this->getRelationTable();
        $table = $this->getParentTableName();
        $pmk = $this->getParentPrimaryKey();
        $rfk = $this->getRelatedForeignKey();
        $query->join(
            $relationTable,
            "rel.{$rfk} = {$table}.{$pmk}",
            null,
            'rel'
        );
        return $this;
    }
1215011
public function configureQuery(SelectQuery $query, bool $loadColumns = true, array $outerKeys = []): SelectQuery
    {
        if (!empty($this->options['using'])) {
            //Use pre-defined query
            return parent::configureQuery($query, $loadColumns, $outerKeys);
        }

        if ($this->isJoined()) {
            $query->join(
                $this->getMethod() == self::JOIN ? 'INNER' : 'LEFT',
                $this->pivotTable() . ' AS ' . $this->pivotAlias())
                ->on(
                    $this->pivotKey(Record::THOUGHT_INNER_KEY),
                    $this->parentKey(Record::INNER_KEY)
                );
        } else {
            $query->innerJoin(
                $this->pivotTable() . ' AS ' . $this->pivotAlias())
                ->on(
                    $this->pivotKey(Record::THOUGHT_OUTER_KEY),
                    $this->localKey(Record::OUTER_KEY)
                )->where(
                    $this->pivotKey(Record::THOUGHT_INNER_KEY),
                    new Parameter($outerKeys)
                );

            $this->configureWindow($query, $this->options['orderBy'], $this->options['limit']);
        }

        //When relation is joined we will use ON statements, when not - normal WHERE
        $whereTarget = $this->isJoined() ? 'onWhere' : 'where';

        //Pivot conditions specified in relation schema
        $this->setWhere(
            $query,
            $this->pivotAlias(),
            $whereTarget,
            $this->schema[Record::WHERE_PIVOT]
        );

        //Additional morphed conditions
        if (!empty($this->schema[Record::MORPH_KEY])) {
            $this->setWhere(
                $query,
                $this->pivotAlias(),
                'onWhere',
                [$this->pivotKey(Record::MORPH_KEY) => $this->targetRole()]
            );
        }

        //Pivot conditions specified by user
        $this->setWhere($query, $this->pivotAlias(), $whereTarget, $this->options['wherePivot']);

        if ($this->isJoined()) {
            //Actual data is always INNER join
            $query->join(
                $this->getMethod() == self::JOIN ? 'INNER' : 'LEFT',
                $this->getTable() . ' AS ' . $this->getAlias()
            )->on(
                $this->localKey(Record::OUTER_KEY),
                $this->pivotKey(Record::THOUGHT_OUTER_KEY)
            );
        }

        //Where conditions specified in relation definition
        $this->setWhere($query, $this->getAlias(), $whereTarget, $this->schema[Record::WHERE]);

        //User specified WHERE conditions
        $this->setWhere($query, $this->getAlias(), $whereTarget, $this->options['where']);

        return parent::configureQuery($query, $loadColumns);
    }
846593
final protected function createSelectQuery(Query $query)
    {
        $select = $this->getDatabase()->select('node', 'n')->fields('n', ['nid'])->addTag('node_access');

        if ($this->addGroupby()) {
            $select->groupBy('n.nid');
        }

        return $select;
    }
343696
public function unionAllRaw(string $sql, string ...$params)
    {
        $instance = $this->selectQueryInstance();

        $instance->selectQuery()->unionAllRaw($sql, ...$params);

        return $instance;
    }
669021
private function setContentObjectWordsSelectQuery(SelectQuery $query)
    {
        $query->select('word_id')
            ->from($this->dbHandler->quoteTable('ezsearch_object_word_link'))
            ->where($query->expr->eq($this->dbHandler->quoteColumn('contentobject_id'), ':contentId'));
    }
718382
def compile_select(self, query):
        
        sql = super(MySqlQueryGrammar, self).compile_select(query)

        if query.unions:
            sql = '(%s) %s' % (sql, self._compile_unions(query))

        return sql