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

Drizzle ORM · Function

asc

asc creates an ascending ordering expression for a Drizzle column. Use it with .orderBy() for ordinary sorting or cursor-based pagination.

How it works

asc creates an ascending-order expression for a table column. Pass the resulting expression to a query builder’s orderBy method; multiple ascending or descending expressions can be supplied to define ordering across several columns.

Syntax

asc(column)

Parameters

NameTypeDescription
columnAny Drizzle table columnThe table column whose values should determine ascending result order.

Returns

An ascending ordering expression suitable for use with .orderBy().

Key points

  • Import asc from the main drizzle-orm package and combine it with other query helpers when needed.
  • Pass asc(column) to .orderBy() to sort query results from lower values to higher values.
  • Supply several ascending expressions to .orderBy() when the result needs a multi-column sort.
  • Choose asc or desc dynamically when the requested sort direction is represented by runtime input.
  • An ascending pagination call can pair the direction with a cursor value and page size.

Common mistakes

  • When using asc for cursor-based pagination, the cursor column should be unique and sequential so ordering and cursor comparisons remain correct.
  • Cursor-pagination queries ordered with asc should have indexes on the cursor columns for efficient execution.

Example: Ascending query results

import { asc } from "drizzle-orm";
import { users } from "./schema";

const rows = await db
  .select()
  .from(users)
  .orderBy(asc(users.id));

This selects users and orders the result by the id column in ascending order.

When to use it

Ascending result sorting

Sort a select query by an identifier or other column in ascending order.

Multi-column ordering

Combine several ascending expressions to create deterministic multi-column ordering.

Ascending cursor pagination

Advance through records using a cursor and increasing column values.

Interview questions

QWhat does asc do in Drizzle ORM?

asc(column) produces an ascending ordering expression for the supplied column. It is used with .orderBy(), such as .orderBy(asc(users.id)).

Follow-up: How would you support either direction? Choose between asc(column) and desc(column) based on a direction value before passing the expression to .orderBy().

QHow do you order by multiple columns ascending?

Import asc from drizzle-orm and pass it to .orderBy(). Multiple expressions can be provided, for example .orderBy(asc(users.firstName), asc(users.id)), to establish a multi-column order.

Follow-up: Why add a second column? It can provide deterministic tie-breaking when the first ordered column is not unique.

QHow is asc used with cursor-based pagination?

For cursor pagination, use asc(users.id) when the page advances toward larger IDs, and compare the next cursor with gt(users.id, cursor). The cursor should be unique and sequential for correct ordering and comparison.

Follow-up: How can descending pagination be implemented? Use desc(users.id) together with the corresponding reverse comparison, such as lt(users.id, cursor).

  • desc
  • orderBy
  • gt
  • lt
  • cursor-based pagination

Source: SQL Cursor-based pagination - Drizzle ORM. Checked 2026-10-16.