Bird
Raised Fist0
dbtdata~10 mins

if/else logic in models in dbt - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new column status that shows 'active' if is_active is true, otherwise 'inactive'.

dbt
select user_id, case when is_active = [1] then 'active' else 'inactive' end as status from users
Drag options to blanks, or click blank then click option'
A'true'
Bfalse
C1
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around true like 'true' makes it a string, not a boolean.
Using false instead of true reverses the logic.
2fill in blank
medium

Complete the code to assign category as 'high' if score is greater than 80, else 'low'.

dbt
select user_id, case when score [1] 80 then 'high' else 'low' end as category from scores
Drag options to blanks, or click blank then click option'
A<=
B=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' checks for exact equality, not greater than.
Using '<=' or '<' reverses the intended logic.
3fill in blank
hard

Fix the error in the code to correctly assign risk_level based on age: 'senior' if age >= 65, else 'adult'.

dbt
select user_id, case when age [1] 65 then 'senior' else 'adult' end as risk_level from demographics
Drag options to blanks, or click blank then click option'
A>=
B!=
C=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' assigns 'senior' to younger ages incorrectly.
Using '=' only matches exactly 65, missing older ages.
4fill in blank
hard

Fill both blanks to create a column discount that is 0.2 if purchase_amount is greater than 100, else 0.

dbt
select customer_id, case when purchase_amount [1] 100 then [2] else 0 end as discount from sales
Drag options to blanks, or click blank then click option'
A>
B<
C0.2
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' reverses the condition.
Using 1 instead of 0.2 assigns wrong discount.
5fill in blank
hard

Fill all three blanks to create a column grade that is 'A' if score >= 90, 'B' if score >= 80, else 'C'.

dbt
select student_id, case when score [1] 90 then 'A' when score [2] 80 then 'B' else [3] end as grade from exams
Drag options to blanks, or click blank then click option'
A>=
B>
C'C'
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' excludes boundary scores like 90 or 80.
Not quoting 'C' makes it invalid as a string.

Practice

(1/5)
1. In dbt models, which SQL construct is commonly used to add if/else logic for creating new columns?
easy
A. WHILE LOOP
B. FOR LOOP
C. CASE WHEN ... THEN ... ELSE ... END
D. FUNCTION CALL

Solution

  1. Step 1: Understand conditional logic in SQL

    SQL uses CASE statements to check conditions and return values accordingly.
  2. Step 2: Identify the correct syntax for if/else logic

    CASE WHEN ... THEN ... ELSE ... END is the standard way to implement if/else logic in SQL and dbt models.
  3. Final Answer:

    CASE WHEN ... THEN ... ELSE ... END -> Option C
  4. Quick Check:

    CASE statement = if/else logic [OK]
Hint: Remember: CASE is SQL's if/else [OK]
Common Mistakes:
  • Confusing loops with conditional logic
  • Using function calls instead of CASE
  • Trying to use procedural code in SQL
2. Which of the following is the correct syntax for a CASE statement in a dbt model to check if a column score is greater than 50?
easy
A. CASE WHEN score THEN 'Pass' ELSE 'Fail' END
B. CASE score > 50 THEN 'Pass' ELSE 'Fail'
C. IF score > 50 THEN 'Pass' ELSE 'Fail' END
D. CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END

Solution

  1. Step 1: Review correct CASE syntax

    The correct syntax starts with CASE WHEN condition THEN result ELSE other_result END.
  2. Step 2: Check each option for syntax errors

    CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END correctly uses CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END. Others miss WHEN, END, or have wrong structure.
  3. Final Answer:

    CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END -> Option D
  4. Quick Check:

    CASE WHEN + condition + THEN + result + ELSE + result + END [OK]
Hint: CASE must have WHEN and END keywords [OK]
Common Mistakes:
  • Omitting WHEN keyword
  • Missing END at the end
  • Using IF instead of CASE in SQL
3. Given the following dbt model SQL snippet, what will be the value of status for a row where score = 75?
SELECT
  score,
  CASE
    WHEN score >= 90 THEN 'Excellent'
    WHEN score >= 70 THEN 'Good'
    ELSE 'Average'
  END AS status
FROM scores_table
medium
A. 'Good'
B. NULL
C. 'Average'
D. 'Excellent'

Solution

  1. Step 1: Check conditions in order for score = 75

    First condition: score >= 90? No (75 < 90). Second condition: score >= 70? Yes (75 >= 70).
  2. Step 2: Determine which condition applies first

    The first true condition is WHEN score >= 70 THEN 'Good', so status = 'Good'.
  3. Final Answer:

    'Good' -> Option A
  4. Quick Check:

    75 >= 70 = 'Good' [OK]
Hint: Check CASE conditions top to bottom [OK]
Common Mistakes:
  • Ignoring order of CASE conditions
  • Choosing 'Excellent' incorrectly
  • Assuming ELSE applies before WHEN
4. Identify the error in this dbt model snippet:
SELECT
  user_id,
  CASE
    WHEN age > 18 THEN 'Adult'
    WHEN age <= 18 THEN 'Minor'
    ELSE 'Unknown'
FROM users
medium
A. Missing END keyword for CASE statement
B. Incorrect comparison operator in WHEN clauses
C. ELSE clause cannot be used in CASE
D. Missing comma after user_id

Solution

  1. Step 1: Check CASE statement syntax

    CASE statements must end with END keyword to close the block.
  2. Step 2: Verify presence of END keyword

    The snippet lacks END after ELSE 'Unknown', causing syntax error.
  3. Final Answer:

    Missing END keyword for CASE statement -> Option A
  4. Quick Check:

    CASE must end with END [OK]
Hint: Always close CASE with END [OK]
Common Mistakes:
  • Forgetting END keyword
  • Misplacing ELSE clause
  • Assuming ELSE is optional
5. You want to create a new column category in your dbt model based on sales amount: if sales > 1000, category is 'High'; if sales between 500 and 1000 inclusive, category is 'Medium'; otherwise, 'Low'. Which CASE statement correctly implements this logic?
hard
A. CASE WHEN sales > 1000 THEN 'High' WHEN sales > 500 THEN 'Medium' ELSE 'Low' END
B. CASE WHEN sales > 1000 THEN 'High' WHEN sales >= 500 AND sales <= 1000 THEN 'Medium' ELSE 'Low' END
C. CASE WHEN sales > 500 AND sales <= 1000 THEN 'Medium' WHEN sales > 1000 THEN 'High' ELSE 'Low' END
D. CASE WHEN sales > 1000 THEN 'High' ELSE WHEN sales >= 500 THEN 'Medium' ELSE 'Low' END

Solution

  1. Step 1: Analyze the conditions and their order

    Check sales > 1000 first for 'High', then sales between 500 and 1000 inclusive for 'Medium', else 'Low'.
  2. Step 2: Verify correct syntax and condition order

    CASE WHEN sales > 1000 THEN 'High' WHEN sales >= 500 AND sales <= 1000 THEN 'Medium' ELSE 'Low' END correctly orders conditions (highest first) and uses proper inclusive range with AND. CASE WHEN sales > 500 AND sales <= 1000 THEN 'Medium' WHEN sales > 1000 THEN 'High' ELSE 'Low' END misses inclusive lower bound (sales=500 fails) and suboptimal order. CASE WHEN sales > 1000 THEN 'High' WHEN sales > 500 THEN 'Medium' ELSE 'Low' END misses inclusive lower bound. CASE WHEN sales > 1000 THEN 'High' ELSE WHEN sales >= 500 THEN 'Medium' ELSE 'Low' END has syntax error with ELSE WHEN.
  3. Final Answer:

    CASE WHEN sales > 1000 THEN 'High' WHEN sales >= 500 AND sales <= 1000 THEN 'Medium' ELSE 'Low' END -> Option B
  4. Quick Check:

    Order and range checks correct in CASE WHEN sales > 1000 THEN 'High' WHEN sales >= 500 AND sales <= 1000 THEN 'Medium' ELSE 'Low' END [OK]
Hint: Check ranges carefully and order conditions top-down [OK]
Common Mistakes:
  • Wrong order of conditions causing misclassification
  • Syntax error using ELSE WHEN instead of WHEN
  • Missing inclusive range checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard