Getting Started
Database Drivers
Schema Integrations
Seed Generators
Utilities and Testing
The following files were used as context for generating this wiki page:
The as() method in Drizzle ORM is a fundamental utility used to convert a query builder (such as a select statement) into a Subquery object, allowing the result of that query to be treated as a table alias for use in further joins or sub-select operations.
When a user calls .as('alias') on a query builder, the system triggers a sequence that evaluates the current state of the query, compiles it into a structured SQL representation, and ultimately wraps the result in a proxy that manages column aliasing. This flow ensures that complex nested queries are correctly encapsulated and scoped within the final executed SQL.
as Method InvocationThe process starts in GelSelectQueryBuilderBase.as(alias). This method gathers the tables currently used by the query and prepares to wrap the existing configuration. It acts as the orchestrator for turning the builder into a subquery.
Sources: drizzle-orm/src/gel-core/query-builders/select.ts:987-998
getSQLInside as(), the builder calls this.getSQL() to convert its internal config object (containing with, joins, where, etc.) into an SQL class instance. This represents the raw structure of the statement being aliased.
Sources: drizzle-orm/src/gel-core/query-builders/select.ts:978-980
getSQL() delegates the actual compilation to the dialect.buildSelectQuery(config) method. This is where Drizzle translates the JavaScript configuration object into an SQL instance by assembling parts like with, select, from, and joins.
Sources: drizzle-orm/src/gel-core/dialect.ts:336-443
As part of the query construction, buildSelectQuery invokes buildWithCTE(withList). This method iterates through any Common Table Expressions defined in the query, formatting them as "alias" as (sql) chunks to be prefixed to the main query.
Sources: drizzle-orm/src/gel-core/dialect.ts:115-127
The compilation process relies on the sql function, which accepts chunks (strings, identifiers, or other SQL instances) to produce the final SQL object that can be safely embedded into other queries.
Sources: drizzle-orm/src/sql/sql.ts:485-495
StringChunkDuring query generation, various parts of the SQL string are represented as StringChunk objects. This class encapsulates static strings, allowing them to be concatenated efficiently during the final serialization to the database dialect's syntax.
Sources: drizzle-orm/src/sql/sql.ts:89-101
Sources: drizzle-orm/src/gel-core/query-builders/select.ts:987-998, drizzle-orm/src/gel-core/dialect.ts:336-443, drizzle-orm/src/sql/sql.ts:485-495
Sources: drizzle-orm/src/gel-core/query-builders/select.ts:987-998, drizzle-orm/src/gel-core/dialect.ts:115-127
Note
The as method returns a Proxy. This is critical because it allows the subquery to dynamically resolve column names when the subquery is referenced in an ON or WHERE clause of an outer query.
Important
The StringChunk class is only responsible for the literal segments of the SQL. Variables and identifiers are handled separately through Param and Name classes within the SQL instance.
SQL engine.as itself is straightforward, the subsequent query execution checks if referenced columns exist in the scope. If the subquery references an external table not included in the alias, the Dialect will throw a descriptive error during build-time.SQL object is lightweight. Because Drizzle constructs a tree of objects rather than concatenating strings immediately, query construction remains performant even for deeply nested subqueries.