Challenge - 5 Problems
Query Builder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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;
Attempts:
2 left
💡 Hint
Look at the where and andWhere conditions carefully.
✗ Incorrect
The query filters users with age > 18 and isActive true, so the result length is the count of users matching both conditions.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
LEFT JOIN with selection requires a specific method.
✗ Incorrect
leftJoinAndSelect performs a LEFT JOIN on the relation and selects its fields. join() is an INNER JOIN by default, and joinAndSelect() performs an INNER JOIN with selection.
🔧 Debug
advanced2: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();
Attempts:
2 left
💡 Hint
Check how parameters are passed and when setParameters is called.
✗ Incorrect
Calling where with a parameter placeholder and then setParameters before getOne is valid and works correctly.
❓ state_output
advanced2: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();
Attempts:
2 left
💡 Hint
getCount() returns the count of records matching the query.
✗ Incorrect
getCount() returns the number of users where isActive is true, matching the where condition.
🧠 Conceptual
expert2:00remaining
Which option best describes how QueryBuilder handles SQL injection prevention?
How does NestJS QueryBuilder (TypeORM) protect against SQL injection when using parameters?
Attempts:
2 left
💡 Hint
Think about how parameters are passed safely in QueryBuilder.
✗ Incorrect
QueryBuilder uses parameter binding with placeholders and automatically escapes values to prevent SQL injection.