Ascending result sorting
Sort a select query by an identifier or other column in ascending order.
Drizzle ORM · Function
asc creates an ascending ordering expression for a Drizzle column. Use it with .orderBy() for ordinary sorting or cursor-based pagination.
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.
asc(column)| Name | Type | Description |
|---|---|---|
column | Any Drizzle table column | The table column whose values should determine ascending result order. |
An ascending ordering expression suitable for use with .orderBy().
asc from the main drizzle-orm package and combine it with other query helpers when needed.asc(column) to .orderBy() to sort query results from lower values to higher values..orderBy() when the result needs a multi-column sort.asc or desc dynamically when the requested sort direction is represented by runtime input.asc for cursor-based pagination, the cursor column should be unique and sequential so ordering and cursor comparisons remain correct.asc should have indexes on the cursor columns for efficient execution.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.
Sort a select query by an identifier or other column in ascending order.
Combine several ascending expressions to create deterministic multi-column ordering.
Advance through records using a cursor and increasing column values.
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().
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.
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).
descorderBygtltcursor-based pagination