0
0
NestJSframework~10 mins

Query builder in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Query builder
Start Query Builder
Initialize Query Object
Add Select Fields
Add Where Conditions
Add Joins (optional)
Add Order/Limit (optional)
Execute Query
Return Results
This flow shows how a query builder in NestJS creates a query step-by-step, adding parts like select fields, conditions, joins, and finally runs the query to get results.
Execution Sample
NestJS
const query = this.repo.createQueryBuilder('user')
  .select(['user.id', 'user.name'])
  .where('user.age > :age', { age: 18 })
  .orderBy('user.name', 'ASC')
  .getMany();
Builds a query to select user id and name where age is over 18, orders by name ascending, and fetches the results.
Execution Table
StepActionQuery StateParametersOutput
1Initialize QueryBuilder with alias 'user'SELECT * FROM user{}QueryBuilder object created
2Add select fields ['user.id', 'user.name']SELECT user.id, user.name FROM user{}Select fields set
3Add where condition 'user.age > :age' with {age:18}SELECT user.id, user.name FROM user WHERE user.age > :age{age:18}Where condition added
4Add order by 'user.name' ASCSELECT user.id, user.name FROM user WHERE user.age > :age ORDER BY user.name ASC{age:18}Order by added
5Execute getMany()Final SQL executed{age:18}Returns array of user objects matching criteria
6EndQuery complete{}Results returned
💡 Query executed and results returned, no more steps.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
queryundefinedQueryBuilder with select fieldsQueryBuilder with where conditionQueryBuilder with order byPromise of resultsArray of user objects
Key Moments - 2 Insights
Why do we use ':age' in the where condition instead of directly writing the number 18?
Using ':age' is a parameter placeholder that safely inserts the value 18, preventing SQL injection and allowing reuse. See execution_table step 3 where parameters {age:18} are passed separately.
What happens if we forget to call getMany() at the end?
Without getMany(), the query is not executed and no results are returned. The query builder only builds the query until getMany() triggers execution (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the query state after step 3?
ASELECT * FROM user
BSELECT user.id, user.name FROM user WHERE user.age > :age
CSELECT user.id, user.name FROM user ORDER BY user.name ASC
DFinal SQL executed
💡 Hint
Check the 'Query State' column in row with Step 3.
At which step is the query actually executed to get results?
AStep 2
BStep 3
CStep 5
DStep 4
💡 Hint
Look for 'Execute getMany()' action in the execution_table.
If we change the where condition to 'user.age > :age' with {age:21}, how does the parameters column change at step 3?
A{age:21}
B{}
C{age:18}
DNo change
💡 Hint
Parameters column shows the values passed for placeholders, see step 3.
Concept Snapshot
NestJS Query Builder:
- Start with createQueryBuilder(alias)
- Chain select(), where(), orderBy(), join() as needed
- Use parameter placeholders like ':param' with values
- Call getMany() or getOne() to execute
- Returns results as objects

Build queries step-by-step, then run to get data.
Full Transcript
This visual execution shows how NestJS Query Builder works step-by-step. First, it creates a query builder object with an alias. Then it adds select fields to choose which columns to fetch. Next, it adds a where condition using a parameter placeholder to safely insert values. After that, it adds an order by clause to sort results. Finally, calling getMany() executes the SQL query and returns matching user objects. Variables track the query state and parameters at each step. Key moments explain why parameters are used and the importance of calling getMany(). The quiz tests understanding of query state, execution step, and parameter changes.