---
title: "SQL Alias Formatting Process"
description: "The as to StringChunk flow is a core mechanism in Drizzle ORM that enables the conversion of a Query Builder instance into a reusable subquery. When you define a query and append .as('alias') to it..."
last_updated: "2026-07-02T09:35:19.037604+00:00"
canonical_url: "https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/technical/how-it-works/sql-alias-formatting-process-2"
---

<details>
<summary>Relevant source files</summary>

The following files were used as context for generating this wiki page:

- [drizzle-orm/src/mysql-core/query-builders/select.ts](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/mysql-core/query-builders/select.ts)
- [drizzle-orm/src/mysql-core/dialect.ts](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/mysql-core/dialect.ts)
- [drizzle-orm/src/sql/sql.ts](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/sql/sql.ts)
</details>

The `as` to `StringChunk` flow is a core mechanism in Drizzle ORM that enables the conversion of a Query Builder instance into a reusable subquery. When you define a query and append `.as('alias')` to it, Drizzle prepares the query's SQL structure to be embedded as a derived table or subquery within a larger statement.

This process transforms high-level query builder configurations into an internal SQL representation, ensuring that when the subquery is eventually used in a parent query, it is correctly wrapped and aliased to maintain query integrity.

### 1. `as`
The execution begins at the `as()` method within `MySqlSelectQueryBuilderBase`. This method takes the current query configuration, computes the set of used tables (for dependency tracking), and wraps the entire query in a `Subquery` object. It also applies a `SelectionProxyHandler` to ensure that column references within the subquery maintain the correct alias context.

Sources: [drizzle-orm/src/mysql-core/query-builders/select.ts:1041-1052](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/mysql-core/query-builders/select.ts#L1041-L1052)

### 2. `getSQL`
To build the subquery's internal SQL, the system calls `getSQL()`. This method delegates the heavy lifting to the `MySqlDialect`, which is responsible for translating the query builder's `config` object (containing joins, wheres, etc.) into a valid SQL string structure.

Sources: [drizzle-orm/src/mysql-core/query-builders/select.ts:1032-1034](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/mysql-core/query-builders/select.ts#L1032-L1034)

### 3. `buildSelectQuery`
The dialect's `buildSelectQuery` receives the query configuration. It orchestrates the assembly of various components, including the `SELECT` clause, `JOIN` clauses, and finally, any Common Table Expressions (CTEs) or set operators.

Sources: [drizzle-orm/src/mysql-core/dialect.ts:312-486](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/mysql-core/dialect.ts#L312-L486)

### 4. `buildWithCTE`
Within the query assembly, the dialect invokes `buildWithCTE` to process any defined Common Table Expressions. This ensures that if the query depends on external definitions, they are prepended correctly to the `WITH` clause of the resulting SQL object.

Sources: [drizzle-orm/src/mysql-core/dialect.ts:112-124](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/mysql-core/dialect.ts#L112-L124)

### 5. `sql`
The `sql` function is the factory that consumes the chunks produced by the builder. It takes the strings and query fragments (tables, columns, expressions) and assembles them into an `SQL` class instance, which stores an array of chunks representing the final query tree.

Sources: [drizzle-orm/src/sql/sql.ts:485-495](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/sql/sql.ts#L485-L495)

### 6. `StringChunk`
Finally, the `StringChunk` class acts as the leaf node in the SQL tree. It stores the raw string segments of the SQL statement. When the final `SQL` object is serialized (or passed to the driver), these chunks are joined to form the executable string.

Sources: [drizzle-orm/src/sql/sql.ts:89-101](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/sql/sql.ts#L89-L101)

> [!NOTE]
> The `as()` method is the entry point for creating subqueries. It internally calls `getSQL()` to ensure the generated SQL is current with any modifications made to the builder prior to the alias being assigned.

> [!TIP]
> Always verify that your subquery has an alias using `.as('name')`, as this is required for the database engine to correctly reference the derived table.

```mermaid
sequenceDiagram
    participant SB as SelectBuilder
    participant D as MySqlDialect
    participant S as SQL
    participant SC as StringChunk

    SB->>SB: as()
    SB->>SB: getSQL()
    SB->>D: buildSelectQuery()
    D->>D: buildWithCTE()
    D->>S: sql()
    S->>SC: StringChunk()
```
Sources: [drizzle-orm/src/mysql-core/query-builders/select.ts:1041-1052](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/mysql-core/query-builders/select.ts#L1041-L1052), [drizzle-orm/src/mysql-core/dialect.ts:312-486](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/mysql-core/dialect.ts#L312-L486), [drizzle-orm/src/sql/sql.ts:89-101](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/sql/sql.ts#L89-L101)

```mermaid
flowchart TD
    A[Start As] --> B[Get SQL]
    B --> C[Build Query]
    C --> D[Build CTE]
    D --> E[Create SQL Object]
    E --> F[Create StringChunk]
```
Sources: [drizzle-orm/src/mysql-core/query-builders/select.ts:1032-1052](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/mysql-core/query-builders/select.ts#L1032-L1052), [drizzle-orm/src/mysql-core/dialect.ts:112-486](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/mysql-core/dialect.ts#L112-L486), [drizzle-orm/src/sql/sql.ts:89-495](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/sql/sql.ts#L89-L495)

### Key Observations
* **Cross-Module Boundaries:** This flow transitions from the high-level `mysql-core` query builder, through the dialect-specific query assembly, into the low-level SQL structure representation.
* **Failure Points:** If the query builder has invalid state (e.g., conflicting set operators or missing columns), `buildSelectQuery` in the dialect layer is designed to throw descriptive errors.
* **Performance:** The conversion to `StringChunk` is deferred until necessary (lazy evaluation), which is efficient for building complex, nested queries without immediate string concatenation overhead.

## Sitemap

See the full [sitemap](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/llms.txt) for all pages in this wiki.
