Bird
0
0

How can you combine JSON extraction and conditional filtering in dbt SQL to select users whose data:profile:age is over 25 and data:profile:active is true?

hard🚀 Application Q9 of 15
dbt - Advanced Patterns
How can you combine JSON extraction and conditional filtering in dbt SQL to select users whose data:profile:age is over 25 and data:profile:active is true?
AUse <code>WHERE CAST(data:profile:age AS INT) > 25 AND data:profile:active::boolean = true</code>
BUse <code>WHERE data:profile:age > 25 AND data:profile:active = 'true'</code>
CUse <code>WHERE json_extract_path_text(data, 'profile', 'age') > 25 AND json_extract_path_text(data, 'profile', 'active') = true</code>
DUse <code>WHERE CAST(json_extract_path_text(data, 'profile', 'age') AS INT) > 25 AND json_extract_path_text(data, 'profile', 'active') = 'true'</code>
Step-by-Step Solution
Solution:
  1. Step 1: Extract and cast age correctly

    json_extract_path_text returns text, so cast age to INT for numeric comparison.
  2. Step 2: Compare active as string

    active is text 'true', so compare to string 'true' for boolean check.
  3. Step 3: Evaluate options

    Use WHERE CAST(json_extract_path_text(data, 'profile', 'age') AS INT) > 25 AND json_extract_path_text(data, 'profile', 'active') = 'true' correctly casts age and compares active as string. Others miss casting or use invalid syntax.
  4. Final Answer:

    Use WHERE CAST(json_extract_path_text(data, 'profile', 'age') AS INT) > 25 AND json_extract_path_text(data, 'profile', 'active') = 'true' -> Option D
  5. Quick Check:

    Cast extracted JSON text before numeric comparison [OK]
Quick Trick: Cast JSON text before numeric filters, compare booleans as strings [OK]
Common Mistakes:
MISTAKES
  • Comparing text directly to numbers
  • Using boolean literals on text
  • Skipping casting functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More dbt Quizzes