LESSON 12 / 18·2 min readIntermediate

Repository vs QueryBuilder vs raw SQL?

Try code in the playground
01 Understand02 Recall03 Prepare

Understand the concept

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.

Important ideas

  • 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.

PAUSE & RECALL

Can you explain it without the notes?

Repository vs QueryBuilder vs raw SQL?

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

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.

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

  • Show how you would inspect the resulting SQL before blaming the ORM for a slow endpoint.
Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: How should a TypeORM transaction work?

Adapted from 03_NESTJS.md · “Repository vs QueryBuilder vs raw SQL?”. Examples are study fragments; framework behavior can depend on your version.