Skip to content
# Class: SimpleSelectQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:44

Represents a single SELECT statement with full clause support (WITH, JOIN, GROUP BY, etc.). Provides the fluent CTE management API used throughout packages/core/tests/models/SelectQuery.cte-management.test.ts.

Example ​

typescript
const query = SelectQueryParser.parse('SELECT id, email FROM users').toSimpleQuery();
const active = SelectQueryParser.parse('SELECT id FROM users WHERE active = true');

query
  .addCTE('active_users', active)
  .toUnionAll(SelectQueryParser.parse('SELECT id, email FROM legacy_users'));

Extends ​

Implements ​

Constructors ​

Constructor ​

new SimpleSelectQuery(params): SimpleSelectQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:65

Parameters ​

params ​
selectClause ​

SelectClause

fromClause? ​

FromClause | null

whereClause? ​

WhereClause | null

groupByClause? ​

GroupByClause | null

havingClause? ​

HavingClause | null

orderByClause? ​

OrderByClause | null

windowClause? ​

WindowsClause | null

limitClause? ​

LimitClause | null

offsetClause? ​

OffsetClause | null

fetchClause? ​

FetchClause | null

forClause? ​

ForClause | null

withClause? ​

WithClause | null

Returns ​

SimpleSelectQuery

Overrides ​

SqlComponent.constructor

Properties ​

kind ​

static kind: symbol

Defined in: packages/core/src/models/SimpleSelectQuery.ts:46

Overrides ​

SqlComponent.kind


__selectQueryType ​

readonly __selectQueryType: "SelectQuery" = 'SelectQuery'

Defined in: packages/core/src/models/SimpleSelectQuery.ts:47

Implementation of ​

SelectQuery.__selectQueryType


headerComments ​

headerComments: string[] | null = null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:48

Implementation of ​

SelectQuery.headerComments


withClause ​

withClause: WithClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:49


selectClause ​

selectClause: SelectClause

Defined in: packages/core/src/models/SimpleSelectQuery.ts:50


fromClause ​

fromClause: FromClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:51


whereClause ​

whereClause: WhereClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:52


groupByClause ​

groupByClause: GroupByClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:53


havingClause ​

havingClause: HavingClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:54


orderByClause ​

orderByClause: OrderByClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:55


windowClause ​

windowClause: WindowsClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:56


limitClause ​

limitClause: LimitClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:57


offsetClause ​

offsetClause: OffsetClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:58


fetchClause ​

fetchClause: FetchClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:59


forClause ​

forClause: ForClause | null

Defined in: packages/core/src/models/SimpleSelectQuery.ts:60


comments ​

comments: string[] | null = null

Defined in: packages/core/src/models/SqlComponent.ts:29

Implementation of ​

SelectQuery.comments

Inherited from ​

SqlComponent.comments


positionedComments ​

positionedComments: PositionedComment[] | null = null

Defined in: packages/core/src/models/SqlComponent.ts:32

Implementation of ​

SelectQuery.positionedComments

Inherited from ​

SqlComponent.positionedComments

Methods ​

toUnion() ​

toUnion(rightQuery): BinarySelectQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:118

Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side, using UNION as the operator.

Parameters ​

rightQuery ​

SelectQuery

The right side of the UNION

Returns ​

BinarySelectQuery

A new BinarySelectQuery representing "this UNION rightQuery"


toUnionAll() ​

toUnionAll(rightQuery): BinarySelectQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:129

Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side, using UNION ALL as the operator.

Parameters ​

rightQuery ​

SelectQuery

The right side of the UNION ALL

Returns ​

BinarySelectQuery

A new BinarySelectQuery representing "this UNION ALL rightQuery"


toInsertQuery() ​

toInsertQuery(options): InsertQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:138

Converts this query into an INSERT statement definition.

Parameters ​

options ​

InsertQueryConversionOptions

Returns ​

InsertQuery

Remarks ​

Calling this method may reorder the current SELECT clause to match the requested column order.

Implementation of ​

SelectQuery.toInsertQuery


toUpdateQuery() ​

toUpdateQuery(options): UpdateQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:147

Converts this query into an UPDATE statement definition.

Parameters ​

options ​

UpdateQueryConversionOptions

Returns ​

UpdateQuery

Remarks ​

The conversion may reorder the SELECT list so that primary keys and updated columns align with the target table.

Implementation of ​

SelectQuery.toUpdateQuery


toDeleteQuery() ​

toDeleteQuery(options): DeleteQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:156

Converts this query into a DELETE statement definition.

Parameters ​

options ​

DeleteQueryConversionOptions

Returns ​

DeleteQuery

Remarks ​

The SELECT clause may be reordered to ensure primary keys and comparison columns appear first.

Implementation of ​

SelectQuery.toDeleteQuery


toMergeQuery() ​

toMergeQuery(options): MergeQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:165

Converts this query into a MERGE statement definition.

Parameters ​

options ​

MergeQueryConversionOptions

Returns ​

MergeQuery

Remarks ​

This method may reorder the SELECT clause to align with the specified MERGE column lists.

Implementation of ​

SelectQuery.toMergeQuery


toIntersect() ​

toIntersect(rightQuery): BinarySelectQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:176

Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side, using INTERSECT as the operator.

Parameters ​

rightQuery ​

SelectQuery

The right side of the INTERSECT

Returns ​

BinarySelectQuery

A new BinarySelectQuery representing "this INTERSECT rightQuery"


toIntersectAll() ​

toIntersectAll(rightQuery): BinarySelectQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:187

Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side, using INTERSECT ALL as the operator.

Parameters ​

rightQuery ​

SelectQuery

The right side of the INTERSECT ALL

Returns ​

BinarySelectQuery

A new BinarySelectQuery representing "this INTERSECT ALL rightQuery"


toExcept() ​

toExcept(rightQuery): BinarySelectQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:198

Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side, using EXCEPT as the operator.

Parameters ​

rightQuery ​

SelectQuery

The right side of the EXCEPT

Returns ​

BinarySelectQuery

A new BinarySelectQuery representing "this EXCEPT rightQuery"


toExceptAll() ​

toExceptAll(rightQuery): BinarySelectQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:209

Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side, using EXCEPT ALL as the operator.

Parameters ​

rightQuery ​

SelectQuery

The right side of the EXCEPT ALL

Returns ​

BinarySelectQuery

A new BinarySelectQuery representing "this EXCEPT ALL rightQuery"


toBinaryQuery() ​

toBinaryQuery(operator, rightQuery): BinarySelectQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:221

Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side, using the specified operator.

Parameters ​

operator ​

string

SQL operator to use (e.g. 'union', 'union all', 'intersect', 'except')

rightQuery ​

SelectQuery

The right side of the binary operation

Returns ​

BinarySelectQuery

A new BinarySelectQuery representing "this [operator] rightQuery"


appendWhereRaw() ​

appendWhereRaw(rawCondition): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:231

Appends a new condition to the query's WHERE clause using AND logic. The condition is provided as a raw SQL string which is parsed internally.

Parameters ​

rawCondition ​

string

Raw SQL string representing the condition (e.g. "status = 'active'")

Returns ​

void


appendWhere() ​

appendWhere(condition): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:242

Appends a new condition to the query's WHERE clause using AND logic. The condition is provided as a ValueComponent object.

Parameters ​

condition ​

ValueComponent

ValueComponent representing the condition

Returns ​

void


appendHavingRaw() ​

appendHavingRaw(rawCondition): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:260

Appends a new condition to the query's HAVING clause using AND logic. The condition is provided as a raw SQL string which is parsed internally.

Parameters ​

rawCondition ​

string

Raw SQL string representing the condition (e.g. "count(*) > 5")

Returns ​

void


appendHaving() ​

appendHaving(condition): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:271

Appends a new condition to the query's HAVING clause using AND logic. The condition is provided as a ValueComponent object.

Parameters ​

condition ​

ValueComponent

ValueComponent representing the condition

Returns ​

void


innerJoinRaw() ​

innerJoinRaw(joinSourceRawText, alias, columns, resolver): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:289

Appends an INNER JOIN clause to the query.

Parameters ​

joinSourceRawText ​

string

The table source text to join (e.g., "my_table", "schema.my_table")

alias ​

string

The alias for the joined table

columns ​

The columns to use for the join condition (e.g. ["user_id"] or "user_id")

string | string[]

resolver ​

TableColumnResolver | null

Returns ​

void


leftJoinRaw() ​

leftJoinRaw(joinSourceRawText, alias, columns, resolver): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:299

Appends a LEFT JOIN clause to the query.

Parameters ​

joinSourceRawText ​

string

The table source text to join

alias ​

string

The alias for the joined table

columns ​

The columns to use for the join condition

string | string[]

resolver ​

TableColumnResolver | null

Returns ​

void


rightJoinRaw() ​

rightJoinRaw(joinSourceRawText, alias, columns, resolver): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:309

Appends a RIGHT JOIN clause to the query.

Parameters ​

joinSourceRawText ​

string

The table source text to join

alias ​

string

The alias for the joined table

columns ​

The columns to use for the join condition

string | string[]

resolver ​

TableColumnResolver | null

Returns ​

void


innerJoin() ​

innerJoin(sourceExpr, columns, resolver): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:318

Appends an INNER JOIN clause to the query using a SourceExpression.

Parameters ​

sourceExpr ​

SourceExpression

The source expression to join

columns ​

The columns to use for the join condition

string | string[]

resolver ​

TableColumnResolver | null

Returns ​

void


leftJoin() ​

leftJoin(sourceExpr, columns, resolver): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:327

Appends a LEFT JOIN clause to the query using a SourceExpression.

Parameters ​

sourceExpr ​

SourceExpression

The source expression to join

columns ​

The columns to use for the join condition

string | string[]

resolver ​

TableColumnResolver | null

Returns ​

void


rightJoin() ​

rightJoin(sourceExpr, columns, resolver): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:336

Appends a RIGHT JOIN clause to the query using a SourceExpression.

Parameters ​

sourceExpr ​

SourceExpression

The source expression to join

columns ​

The columns to use for the join condition

string | string[]

resolver ​

TableColumnResolver | null

Returns ​

void


toSource() ​

toSource(alias): SourceExpression

Defined in: packages/core/src/models/SimpleSelectQuery.ts:419

Parameters ​

alias ​

string

Returns ​

SourceExpression


appendWith() ​

appendWith(commonTable): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:429

Parameters ​

commonTable ​

CommonTable | CommonTable[]

Returns ​

void


appendWithRaw() ​

appendWithRaw(rawText, alias): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:448

Appends a CommonTable (CTE) to the WITH clause from raw SQL text and alias. If alias is provided, it will be used as the CTE name.

Parameters ​

rawText ​

string

Raw SQL string representing the CTE body (e.g. '(SELECT ...)')

alias ​

string

Optional alias for the CTE (e.g. 'cte_name')

Returns ​

void


overrideSelectItemExpr() ​

overrideSelectItemExpr(columnName, fn): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:465

Overrides a select item using a template literal function. The callback receives the SQL string of the original expression and must return a new SQL string. The result is parsed and set as the new select item value.

Example usage: query.overrideSelectItemRaw("journal_date", expr => greatest(${expr}, DATE '2025-01-01'))

Parameters ​

columnName ​

string

The name of the column to override

fn ​

(expr) => string

Callback that receives the SQL string of the original expression and returns a new SQL string

Returns ​

void


appendWhereExpr() ​

appendWhereExpr(columnName, exprBuilder, options?): void

Defined in: packages/core/src/models/SimpleSelectQuery.ts:489

Appends a WHERE clause using the expression for the specified column. If options.upstream is true, applies to all upstream queries containing the column. If false or omitted, applies only to the current query.

Parameters ​

columnName ​

string

The name of the column to target.

exprBuilder ​

(expr) => string

Function that receives the column expression as a string and returns the WHERE condition string.

options? ​

Optional settings. If upstream is true, applies to upstream queries.

upstream? ​

boolean

Returns ​

void


setParameter() ​

setParameter(name, value): this

Defined in: packages/core/src/models/SimpleSelectQuery.ts:528

Sets the value of a parameter by name in this query.

Parameters ​

name ​

string

Parameter name

value ​

SqlParameterValue

Value to set

Returns ​

this

Implementation of ​

SelectQuery.setParameter


toSimpleQuery() ​

toSimpleQuery(): SimpleSelectQuery

Defined in: packages/core/src/models/SimpleSelectQuery.ts:537

Returns this SimpleSelectQuery instance (identity function).

Returns ​

SimpleSelectQuery

This SimpleSelectQuery instance

Implementation of ​

SelectQuery.toSimpleQuery


addCTE() ​

addCTE(name, query, options?): this

Defined in: packages/core/src/models/SimpleSelectQuery.ts:572

Adds a CTE (Common Table Expression) to the query.

Parameters ​

name ​

string

CTE name/alias (must be non-empty)

query ​

SelectQuery

SelectQuery to use as CTE

options? ​

CTEOptions

Optional configuration

Returns ​

this

Throws ​

When name is empty or whitespace-only

Throws ​

When CTE with same name already exists

Example ​

typescript
// Basic CTE
query.addCTE('active_users', 
  SelectQueryParser.parse('SELECT * FROM users WHERE active = true')
);

// PostgreSQL MATERIALIZED CTE (forces materialization)
query.addCTE('expensive_calc', expensiveQuery, { materialized: true });

// PostgreSQL NOT MATERIALIZED CTE (prevents materialization)
query.addCTE('simple_view', simpleQuery, { materialized: false });

Remarks ​

  • MATERIALIZED/NOT MATERIALIZED is PostgreSQL-specific syntax
  • Other databases will ignore the materialized hint
  • CTE names must be unique within the query
  • Method supports fluent chaining

Implementation of ​

CTEManagement.addCTE


removeCTE() ​

removeCTE(name): this

Defined in: packages/core/src/models/SimpleSelectQuery.ts:613

Removes a CTE by name from the query.

Parameters ​

name ​

string

CTE name to remove

Returns ​

this

Throws ​

When CTE with specified name doesn't exist

Example ​

typescript
query.addCTE('temp_data', tempQuery);
query.removeCTE('temp_data'); // Removes the CTE

// Throws CTENotFoundError
query.removeCTE('non_existent');

Remarks ​

  • Throws error if CTE doesn't exist (strict mode for safety)
  • Use hasCTE() to check existence before removal if needed
  • Method supports fluent chaining

Implementation of ​

CTEManagement.removeCTE


hasCTE() ​

hasCTE(name): boolean

Defined in: packages/core/src/models/SimpleSelectQuery.ts:655

Checks if a CTE with the given name exists in the query. Optimized with O(1) lookup using internal cache.

Parameters ​

name ​

string

CTE name to check

Returns ​

boolean

true if CTE exists, false otherwise

Example ​

typescript
query.addCTE('user_stats', statsQuery);

if (query.hasCTE('user_stats')) {
  console.log('CTE exists');
}

query.removeCTE('user_stats');
console.log(query.hasCTE('user_stats')); // false

Remarks ​

  • Performs case-sensitive name matching
  • Returns false for queries without any CTEs
  • Useful for conditional CTE operations
  • O(1) performance using internal cache

Implementation of ​

CTEManagement.hasCTE


getCTENames() ​

getCTENames(): string[]

Defined in: packages/core/src/models/SimpleSelectQuery.ts:690

Returns an array of all CTE names in the query.

Returns ​

string[]

Array of CTE names in the order they were defined

Example ​

typescript
const query = SelectQueryParser.parse('SELECT * FROM data').toSimpleQuery();

// Empty query
console.log(query.getCTENames()); // []

// Add CTEs
query.addCTE('users', userQuery);
query.addCTE('orders', orderQuery);

console.log(query.getCTENames()); // ['users', 'orders']

// Use for validation
const expectedCTEs = ['users', 'orders', 'products'];
const actualCTEs = query.getCTENames();
const missingCTEs = expectedCTEs.filter(name => !actualCTEs.includes(name));

Remarks ​

  • Returns empty array for queries without CTEs
  • Names are returned in definition order
  • Useful for debugging and validation
  • Names reflect actual CTE aliases, not table references
  • Performance: O(n) but avoids redundant array mapping

Implementation of ​

CTEManagement.getCTENames


replaceCTE() ​

replaceCTE(name, query, options?): this

Defined in: packages/core/src/models/SimpleSelectQuery.ts:733

Replaces an existing CTE or adds a new one with the given name.

Parameters ​

name ​

string

CTE name to replace/add (must be non-empty)

query ​

SelectQuery

SelectQuery to use as CTE

options? ​

CTEOptions

Optional configuration

Returns ​

this

Throws ​

When name is empty or whitespace-only

Example ​

typescript
const query = SelectQueryParser.parse('SELECT * FROM final_data').toSimpleQuery();
const oldQuery = SelectQueryParser.parse('SELECT id FROM old_table');
const newQuery = SelectQueryParser.parse('SELECT id, status FROM new_table WHERE active = true');

// Add initial CTE
query.addCTE('data_source', oldQuery);

// Replace with improved version
query.replaceCTE('data_source', newQuery, { materialized: true });

// Safe replacement - adds if doesn't exist
query.replaceCTE('new_cte', newQuery); // Won't throw error

// Chaining replacements
query
  .replaceCTE('cte1', query1, { materialized: false })
  .replaceCTE('cte2', query2, { materialized: true });

Remarks ​

  • Unlike addCTE(), this method won't throw error if CTE already exists
  • Unlike removeCTE(), this method won't throw error if CTE doesn't exist
  • Useful for upsert-style CTE operations
  • MATERIALIZED/NOT MATERIALIZED is PostgreSQL-specific
  • Method supports fluent chaining
  • Maintains CTE order when replacing existing CTEs

Implementation of ​

CTEManagement.replaceCTE


getKind() ​

getKind(): symbol

Defined in: packages/core/src/models/SqlComponent.ts:15

Returns ​

symbol

Implementation of ​

SelectQuery.getKind

Inherited from ​

SqlComponent.getKind


accept() ​

accept<T&gt;(visitor): T

Defined in: packages/core/src/models/SqlComponent.ts:19

Type Parameters ​

T ​

T

Parameters ​

visitor ​

SqlComponentVisitor<T&gt;

Returns ​

T

Implementation of ​

SelectQuery.accept

Inherited from ​

SqlComponent.accept


toSqlString() ​

toSqlString(formatter): string

Defined in: packages/core/src/models/SqlComponent.ts:23

Parameters ​

formatter ​

SqlComponentVisitor<string&gt;

Returns ​

string

Implementation of ​

SelectQuery.toSqlString

Inherited from ​

SqlComponent.toSqlString


addPositionedComments() ​

addPositionedComments(position, comments): void

Defined in: packages/core/src/models/SqlComponent.ts:37

Add comments at a specific position

Parameters ​

position ​

"before" | "after"

comments ​

string[]

Returns ​

void

Implementation of ​

SelectQuery.addPositionedComments

Inherited from ​

SqlComponent.addPositionedComments


getPositionedComments() ​

getPositionedComments(position): string[]

Defined in: packages/core/src/models/SqlComponent.ts:56

Get comments for a specific position

Parameters ​

position ​

"before" | "after"

Returns ​

string[]

Implementation of ​

SelectQuery.getPositionedComments

Inherited from ​

SqlComponent.getPositionedComments


getAllPositionedComments() ​

getAllPositionedComments(): string[]

Defined in: packages/core/src/models/SqlComponent.ts:66

Get all positioned comments as a flat array in order (before, after)

Returns ​

string[]

Implementation of ​

SelectQuery.getAllPositionedComments

Inherited from ​

SqlComponent.getAllPositionedComments

Released under the MIT License.