Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Drizzle ORM · Module

.as

Drizzle ORM’s .as() helper assigns an explicit alias to a selected field or SQL expression. It is useful for naming computed fields clearly, especially in complex queries.

How it works

The .as() helper assigns an explicit alias to a selected field, making computed expressions easier to identify in query results. It is especially useful for complex SQL expressions where the generated field name would otherwise be unclear.

Syntax

sql<T>`expression`.as('alias_name')

Parameters

NameTypeDescription
alias_namestringThe string to use as the selected field's alias.

Returns

A SQL expression representing the aliased field.

Key points

  • Use .as() when a selected field needs a deliberate, explicit name.
  • Complex queries benefit from explicit aliases because custom fields become easier to identify.
  • The helper accepts the desired alias string directly.
  • Choose descriptive aliases so query outputs communicate what each custom field represents.
  • A SQL template expression can be followed by .as() to name its selected result.

Common mistakes

  • The generic type supplied to sql<T> affects TypeScript typing only; it does not transform the value returned at runtime.

Example: Alias a SQL expression

import { sql } from "drizzle-orm";

const lowerName = sql<string>`lower(${users.name})`.as("lower_name");

Creates a SQL expression whose selected result is named lower_name.

When to use it

Name computed fields

A query selects a computed SQL value such as a lowercased column and needs a predictable result property.

Clarify query results

A complex projection contains custom expressions whose generated names are unclear.

Interview questions

QWhat problem does .as() solve?

.as("name") gives a selected column or SQL expression an explicit result alias. For example, a lowercased name can be selected as lower_name.

Follow-up: How would you alias a computed expression? Use sql<T> for the expression and call .as() on the resulting SQL object.

QWhen is .as() particularly useful?

It makes custom selected fields clearer and more readable by controlling the name exposed in the query result. This is useful when an expression does not have a meaningful default field name.

Follow-up: What should be checked when using a typed SQL expression? Verify that the TypeScript type matches the database value because sql<T> does not perform runtime mapping.

QHow do you alias a SQL expression?

A SQL expression can be aliased with a call such as sql<string>0lower(${users.name}).as("lower_name"). The generated SQL uses the corresponding AS alias.

Follow-up: How can the alias be used in a larger query? Select the aliased expression as a field in the query projection and reference the resulting property by its chosen name.

  • sql`` operator
  • sql<T>
  • SQL Select

Source: Magic sql`` operator - Drizzle ORM. Checked 2026-10-16.