0
0
NestJSframework~20 mins

Query builder in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Builder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this NestJS QueryBuilder code?
Consider this code snippet using TypeORM's QueryBuilder in a NestJS service. What will be the output of the query execution?
NestJS
const users = await this.userRepository.createQueryBuilder('user')
  .where('user.age > :age', { age: 18 })
  .andWhere('user.isActive = :active', { active: true })
  .getMany();

return users.length;
ANumber of all users regardless of age or active status
BNumber of users older than 18 and active
CNumber of users older than 18 regardless of active status
DNumber of users active regardless of age
Attempts:
2 left
💡 Hint
Look at the where and andWhere conditions carefully.
📝 Syntax
intermediate
2:00remaining
Which option correctly adds a LEFT JOIN in NestJS QueryBuilder?
You want to join the 'profile' relation of 'user' entity using a LEFT JOIN. Which code snippet is correct?
AcreateQueryBuilder('user').joinAndSelect('user.profile', 'profile')
BcreateQueryBuilder('user').join('user.profile', 'profile')
CcreateQueryBuilder('user').leftJoin('profile', 'user.profile')
DcreateQueryBuilder('user').leftJoinAndSelect('user.profile', 'profile')
Attempts:
2 left
💡 Hint
LEFT JOIN with selection requires a specific method.
🔧 Debug
advanced
2:00remaining
Why does this QueryBuilder code throw an error?
This code throws a runtime error. What is the cause?
NestJS
const result = await this.userRepository.createQueryBuilder('user')
  .where('user.name = :name')
  .setParameters({ name: 'Alice' })
  .getOne();
ANo error; code runs and returns user named Alice
BsetParameters is called after where without parameters, causing error
CMissing parameter value in where clause causes error
DUsing setParameters instead of passing parameters in where causes error
Attempts:
2 left
💡 Hint
Check how parameters are passed and when setParameters is called.
state_output
advanced
2:00remaining
What is the value of 'count' after running this QueryBuilder code?
Given this code, what will be the value of 'count'?
NestJS
const count = await this.userRepository.createQueryBuilder('user')
  .where('user.isActive = :active', { active: true })
  .getCount();
ATotal number of all users regardless of active status
BNumber of users returned by getMany()
CTotal number of active users in the database
DNumber of users with isActive false
Attempts:
2 left
💡 Hint
getCount() returns the count of records matching the query.
🧠 Conceptual
expert
2:00remaining
Which option best describes how QueryBuilder handles SQL injection prevention?
How does NestJS QueryBuilder (TypeORM) protect against SQL injection when using parameters?
AIt automatically escapes parameters passed via placeholders like :param
BIt requires manual escaping of all parameters before passing
CIt disables parameter binding by default for performance
DIt does not protect; raw SQL must be sanitized manually
Attempts:
2 left
💡 Hint
Think about how parameters are passed safely in QueryBuilder.