Bird
0
0

How do you correctly add a WHERE clause to filter users with age greater than 18 using QueryBuilder in NestJS?

easy📝 Syntax Q3 of 15
NestJS - Database with TypeORM
How do you correctly add a WHERE clause to filter users with age greater than 18 using QueryBuilder in NestJS?
A.where('user.age > 18')
B.where('user.age > :age', { age: 18 })
C.where('age > :age', { age: 18 })
D.where('user.age = 18')
Step-by-Step Solution
Solution:
  1. Step 1: Use parameterized query

    .where('user.age > :age', { age: 18 }) correctly uses a parameter :age with a value of 18.
  2. Step 2: Check alias usage

    .where('age > :age', { age: 18 }) misses the alias 'user' before 'age', which is required.
  3. Step 3: Validate other options

    .where('user.age > 18') uses a raw string without parameters (less safe), and D checks equality, not greater than.
  4. Final Answer:

    .where('user.age > :age', { age: 18 }) -> Option B
  5. Quick Check:

    Always use parameters with alias in WHERE clauses [OK]
Quick Trick: Use alias and parameters in WHERE clauses [OK]
Common Mistakes:
  • Omitting alias in the condition
  • Using raw values instead of parameters
  • Using wrong comparison operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes