Repository vs QueryBuilder vs raw SQL?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn Repository vs QueryBuilder vs raw SQL?
Definition:
A repository exposes common operations around an entity, QueryBuilder assembles more flexible database queries, and raw SQL gives direct control over SQL text. These are levels of access rather than a ranking from bad to good. Use the simplest approach that expresses the required query clearly and efficiently.
Key points:
-
Repository: common entity reads and writes.
-
QueryBuilder: more control over joins, filtering and selected fields.
-
Raw SQL: useful for database-specific queries; still use bound parameters.
-
Bind values in QueryBuilder and raw SQL; dynamic table or column identifiers need separate allowlists.
Interview point:
- Show how you would inspect the resulting SQL before blaming the ORM for a slow endpoint.
Adapted from 03_NESTJS.md · “Repository vs QueryBuilder vs raw SQL?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.