Skip to main content

Union

Combines multiple queries with UNION or UNION ALL. Extends Query directly.

Constructor

Union(queries: Query[], unionType?: 'all'): Union
new Union(queries: Query[], unionType?: 'all'): Union

Dual-callable. queries is an array of query objects (typically Select instances) to combine; unionType is either omitted (plain UNION) or 'all' (UNION ALL).

import { Select, Union } from '@sqb/builder';

const q1 = Select('id', 'name').from('table1');
const q2 = Select('id', 'name').from('table2');

Union([q1, q2]).generate().sql;
// select id, name from table1 UNION select id, name from table2

Union([q1, q2], 'all').generate().sql;
// select id, name from table1 UNION ALL select id, name from table2

Properties

KeyTypeReadonlyDescription
_typeSerializationType.UNION_QUERYYesDiscriminates this node during serialization.
_queriesQuery[]NoThe combined queries, set from the constructor.
_unionType'all' | undefinedNoSet from the constructor's unionType argument.

Plus everything inherited from Query (_comment, _params, EventEmitter).

Methods

Union adds no methods of its own beyond its constructor and serialization logic — it relies on Query's .generate(), .values(), and .comment().

See also