Basic table selection
Read complete rows from a schema table.
Drizzle ORM · Module
.from() sets the table or query source for a Drizzle select statement. It works with projections, filters, joins, ordering, pagination, subqueries, and prepared statements while preserving type-safe results.
.from() identifies the table or query source for a Drizzle select statement. It composes with selections, joins, filters, ordering, pagination, subqueries, and prepared statements while preserving inferred result types.
db.select(selection?).from(source)| Name | Type | Description |
|---|---|---|
source | table or aliased subquery | The table or aliased subquery that supplies rows to the select builder. |
A composable select query builder whose executed result contains rows inferred from the selected source and projection.
db.select().from(table), which retrieves rows from the supplied table..select() when the query should return a chosen subset or renamed projection of the source columns..where() after .from() to restrict returned rows with Drizzle filter operators..limit() and .offset() after the source and ordering clauses to implement paginated reads..from()..from() alone changes the column-selection rule: Drizzle's select builder explicitly lists columns rather than emitting select *..orderBy() is required before .limit() to make the result ordering explicit.import { drizzle } from 'drizzle-orm/node-postgres';
import { eq } from 'drizzle-orm';
import { users } from './schema';
const db = drizzle(process.env.DATABASE_URL!);
const result = await db
.select()
.from(users)
.where(eq(users.id, 42));
console.log(result);Builds and executes a type-safe select query whose source table is users and whose rows match the specified condition.
Read complete rows from a schema table.
Return only selected fields and restrict records with predicates.
Use a derived query as the source for an outer select.
Fetch a bounded window of ordered records.
.from() do in a Drizzle select query?.from() supplies the source table for a select query, such as db.select().from(users). The builder can then add conditions, joins, ordering, and pagination while retaining type-safe results.
Follow-up: How can a derived source be used? Build a subquery, give it an alias with .as(), and pass that aliased query to .from().
.from()?A select query can use .from() with a table and compose a filter through .where(), for example .from(users).where(eq(users.id, 42)). The selected fields remain inferred from the schema or selection object.
Follow-up: How would you return only chosen fields? Pass a selection object to .select(), mapping output names to table columns before calling .from().
.from() use a subquery instead of a table?.from() can receive a subquery source, allowing one query to be embedded in another. The subquery should be aliased so its fields can be referenced by the outer query.
Follow-up: How can repeated executions avoid rebuilding SQL? Prepare the composed query with .prepare(name) and execute it with placeholder values.
select()selectDistinct()where()orderBy()limit()offset()leftJoin()prepare()