intermediate
Query builder
Compose typed or fluent SQL while preserving control over joins, filters, ordering, pagination, and execution plans.
A query builder composes SQL through functions or a fluent API, often with parameter binding and optional type inference. It shines for dynamic filters, joins, and pagination without string concatenation.
db.selectFrom('orders')
.innerJoin('customers', 'customers.id', 'orders.customer_id')
.where('orders.status', '=', status)
.limit(pageSize)
.offset(page * pageSize);
On interviews: explain optional filters, joins, pagination, SQL injection safety, and why builders still require index and plan knowledge.
Common pitfalls: fluent APIs that hide inefficient joins; unreadable dynamic queries; logging disabled in production so bad SQL stays invisible.
The trade-off is safer dynamic SQL and reuse versus readability and plan discipline.
Checklist:
- Inspect generated SQL.
- Parameterize dynamic values.
- Tie query shape to indexes and pagination.
- Log or trace important access paths.