0
0
dbtdata~20 mins

if/else logic in models in dbt - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If/Else Logic Mastery in dbt Models
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of conditional logic in a dbt model
Consider this dbt model SQL snippet using a CASE statement. What will be the value of category for a row where sales_amount is 150?
dbt
SELECT
  sales_amount,
  CASE
    WHEN sales_amount > 200 THEN 'High'
    WHEN sales_amount > 100 THEN 'Medium'
    ELSE 'Low'
  END AS category
FROM sales_data
ANULL
B'High'
C'Low'
D'Medium'
Attempts:
2 left
💡 Hint
Think about the order of conditions in the CASE statement and which condition matches 150.
data_output
intermediate
2:00remaining
Resulting rows after applying if/else logic in a dbt model
Given this dbt model SQL, how many rows will have status = 'Active'?
dbt
SELECT
  user_id,
  CASE
    WHEN last_login > CURRENT_DATE - INTERVAL '30 days' THEN 'Active'
    ELSE 'Inactive'
  END AS status
FROM user_logins
ANo users, because of syntax error
BAll users who logged in within the last 30 days
CAll users regardless of login date
DAll users who logged in more than 30 days ago
Attempts:
2 left
💡 Hint
Look at the condition comparing last_login date with current date minus 30 days.
🔧 Debug
advanced
2:00remaining
Identify the error in if/else logic in a dbt model
This dbt model SQL snippet is intended to assign a label based on score. What error will it raise?
dbt
SELECT
  user_id,
  CASE
    WHEN score > 90 THEN 'Excellent'
    WHEN score > 75 THEN 'Good'
    ELSE 'Average'
  END AS performance
FROM user_scores
WHERE score >= 0
ANo error, runs correctly
BSyntaxError due to missing END keyword
CRuntime error because score can be NULL
DTypeError because score is compared to string
Attempts:
2 left
💡 Hint
Check if the CASE statement syntax is complete and if the WHERE clause handles NULLs.
🚀 Application
advanced
2:00remaining
Applying if/else logic to create a new column in a dbt model
You want to create a new column risk_level in your dbt model based on credit_score: scores above 700 are 'Low', between 500 and 700 are 'Medium', and below 500 are 'High'. Which SQL snippet correctly implements this?
ACASE WHEN credit_score > 700 THEN 'Low' WHEN credit_score >= 500 THEN 'Medium' ELSE 'High' END AS risk_level
BCASE WHEN credit_score >= 700 THEN 'Low' WHEN credit_score > 500 THEN 'Medium' ELSE 'High' END AS risk_level
CCASE WHEN credit_score > 700 THEN 'Low' WHEN credit_score > 500 THEN 'Medium' ELSE 'High' END AS risk_level
DCASE WHEN credit_score >= 700 THEN 'Low' WHEN credit_score >= 500 THEN 'Medium' ELSE 'High' END AS risk_level
Attempts:
2 left
💡 Hint
Pay attention to the boundary conditions and whether to use > or >=.
🧠 Conceptual
expert
3:00remaining
Understanding nested if/else logic in dbt models
In dbt models, what is the main difference between using nested CASE statements versus multiple WHEN conditions in a single CASE statement?
AMultiple WHEN conditions execute faster than nested CASE statements in all databases.
BNested CASE statements are invalid syntax in dbt models.
CNested CASE statements allow more complex, multi-level decision trees, while multiple WHEN conditions in one CASE are flat and evaluated in order.
DMultiple WHEN conditions can only handle numeric comparisons, nested CASE can handle strings.
Attempts:
2 left
💡 Hint
Think about how CASE statements evaluate conditions and how nesting affects logic structure.