---
title: "SQL Alias Formatting Process"
description: "The \"As -> StringChunk\" flow describes how Drizzle transforms a configured query builder into a named subquery expression within a SQL statement. This process is triggered when a user calls the `.a..."
last_updated: "2026-07-02T09:35:18.996479+00:00"
canonical_url: "https://www.doc0.app/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/technical/how-it-works/sql-alias-formatting-process-3"
---

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

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

- [drizzle-orm/src/singlestore-core/query-builders/select.ts](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/singlestore-core/query-builders/select.ts)
- [drizzle-orm/src/singlestore-core/dialect.ts](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/singlestore-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 -> StringChunk" flow describes how Drizzle transforms a configured query builder into a named subquery expression within a SQL statement. This process is triggered when a user calls the `.as()` method on a `SingleStoreSelect` instance. The end result is a `Subquery` object wrapped in a `SelectionProxyHandler` that can be seamlessly embedded into larger queries, ensuring SQL syntax safety and structural integrity.

### 1. `as` method invocation
The execution starts in the `SingleStoreSelectQueryBuilderBase` class. The `.as()` method is called with an alias string. It initializes the subquery creation by extracting all tables involved in the original query (the main table and any joined tables) to maintain correct table references in the scope of the new alias.

Sources: [drizzle-orm/src/singlestore-core/query-builders/select.ts:912-917](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/singlestore-core/query-builders/select.ts#L912-L917)

### 2. `getSQL` call for query construction
Within `.as()`, the builder calls its own `getSQL()` method. This method triggers the dialect to build the actual SQL string representation of the current select configuration, including filters, joins, and limits.

Sources: [drizzle-orm/src/singlestore-core/query-builders/select.ts:903-905](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/singlestore-core/query-builders/select.ts#L903-L905)

### 3. Dialect build process
The `SingleStoreDialect.buildSelectQuery` method is invoked. It assembles the complete SQL `SELECT` structure. If the query includes common table expressions (CTEs), it calls `buildWithCTE` to properly format the `WITH` clause before the primary `SELECT` query.

Sources: [drizzle-orm/src/singlestore-core/dialect.ts:299-314](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/singlestore-core/dialect.ts#L299-L314)

### 4. CTE formatting
`buildWithCTE` processes the `withList` configuration. It iterates through registered subqueries, creating `SQL` objects for each CTE, and joins them using the `sql` template literal function, ensuring the resulting SQL conforms to SingleStore syntax expectations.

Sources: [drizzle-orm/src/singlestore-core/dialect.ts:111-123](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/singlestore-core/dialect.ts#L111-L123)

### 5. `sql` tag and chunk creation
The `sql` function is responsible for parsing template strings and parameters. It segments the query into an array of `SQLChunk` objects. When it encounters raw strings in the template, it instantiates `StringChunk` to represent fixed SQL parts.

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

### 6. `StringChunk` instantiation
The `StringChunk` class acts as the final wrapper for literal SQL fragments. It stores the SQL string as a list of components, which the engine later joins during the `toQuery` phase to generate the final executable string and parameter binding set.

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)

> [!TIP]
> The `.as()` method is crucial for subquery reuse. By wrapping the result in a `SelectionProxyHandler`, Drizzle allows you to continue writing type-safe code as if the alias were a standard table.

```mermaid
sequenceDiagram
    participant S as SelectBuilder
    participant D as Dialect
    participant Q as SQL Module
    S->>S: as(alias)
    S->>S: getSQL()
    S->>D: buildSelectQuery()
    D->>D: buildWithCTE()
    D->>Q: sql()
    Q->>Q: new StringChunk()
```

Sources: [drizzle-orm/src/singlestore-core/query-builders/select.ts:912-922](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/singlestore-core/query-builders/select.ts#L912-L922), [drizzle-orm/src/singlestore-core/dialect.ts:299-314](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/singlestore-core/dialect.ts#L299-L314), [drizzle-orm/src/sql/sql.ts:485-494](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/sql/sql.ts#L485-L494)

```mermaid
flowchart TD
    A[Call .as] --> B[Get SQL]
    B --> C[Dialect Build]
    C --> D[CTE Logic]
    D --> E[SQL Template]
    E --> F[StringChunk]
```

Sources: [drizzle-orm/src/singlestore-core/query-builders/select.ts:912-920](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/singlestore-core/query-builders/select.ts#L912-L920), [drizzle-orm/src/singlestore-core/dialect.ts:299-350](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/singlestore-core/dialect.ts#L299-L350), [drizzle-orm/src/sql/sql.ts:485-500](https://github.com/blade47/drizzle-orm/blob/main/drizzle-orm/src/sql/sql.ts#L485-L500)

### Key Observations
*   **Boundary Crossing:** This flow moves from high-level Query Builder logic (`select.ts`) through Dialect-specific transformation (`dialect.ts`) into core SQL AST manipulation (`sql.ts`).
*   **Failure Points:** The primary failure point is during alias generation if the same alias is reused or if invalid columns are referenced, which are typically caught during the building or execution phase rather than the instantiation phase.
*   **Performance:** By using `StringChunk` and the `sql` template system, Drizzle avoids costly string concatenations for complex queries, deferring query generation until `toQuery()` is invoked.

## Sitemap

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