if/else logic in models in dbt - Time & Space Complexity
We want to see how using if/else logic in dbt models affects how long the code takes to run.
Specifically, we ask: does adding if/else slow down the model as data grows?
Analyze the time complexity of the following code snippet.
select
id,
case
when amount > 100 then 'high'
else 'low'
end as amount_category
from {{ ref('sales_data') }}
This code classifies each row's amount as 'high' or 'low' using if/else logic inside a dbt model.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The case (if/else) check runs once for each row in the input table.
- How many times: Exactly once per row, so as many times as there are rows.
Each row is checked once, so if the data doubles, the work doubles too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of rows.
[X] Wrong: "If I add if/else, the model will run much slower because it adds extra steps."
[OK] Correct: The if/else check happens once per row, just like reading the row itself, so it does not add extra loops or multiply work.
Understanding how simple conditional logic scales helps you explain model performance clearly and confidently.
"What if we added a nested if/else inside the first one? How would the time complexity change?"
Practice
Solution
Step 1: Understand conditional logic in SQL
SQL uses CASE statements to check conditions and return values accordingly.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.Final Answer:
CASE WHEN ... THEN ... ELSE ... END -> Option CQuick Check:
CASE statement = if/else logic [OK]
- Confusing loops with conditional logic
- Using function calls instead of CASE
- Trying to use procedural code in SQL
score is greater than 50?Solution
Step 1: Review correct CASE syntax
The correct syntax starts with CASE WHEN condition THEN result ELSE other_result END.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.Final Answer:
CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END -> Option DQuick Check:
CASE WHEN + condition + THEN + result + ELSE + result + END [OK]
- Omitting WHEN keyword
- Missing END at the end
- Using IF instead of CASE in SQL
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_tableSolution
Step 1: Check conditions in order for score = 75
First condition: score >= 90? No (75 < 90). Second condition: score >= 70? Yes (75 >= 70).Step 2: Determine which condition applies first
The first true condition is WHEN score >= 70 THEN 'Good', so status = 'Good'.Final Answer:
'Good' -> Option AQuick Check:
75 >= 70 = 'Good' [OK]
- Ignoring order of CASE conditions
- Choosing 'Excellent' incorrectly
- Assuming ELSE applies before WHEN
SELECT
user_id,
CASE
WHEN age > 18 THEN 'Adult'
WHEN age <= 18 THEN 'Minor'
ELSE 'Unknown'
FROM usersSolution
Step 1: Check CASE statement syntax
CASE statements must end with END keyword to close the block.Step 2: Verify presence of END keyword
The snippet lacks END after ELSE 'Unknown', causing syntax error.Final Answer:
Missing END keyword for CASE statement -> Option AQuick Check:
CASE must end with END [OK]
- Forgetting END keyword
- Misplacing ELSE clause
- Assuming ELSE is optional
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?Solution
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'.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.Final Answer:
CASE WHEN sales > 1000 THEN 'High' WHEN sales >= 500 AND sales <= 1000 THEN 'Medium' ELSE 'Low' END -> Option BQuick 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]
- Wrong order of conditions causing misclassification
- Syntax error using ELSE WHEN instead of WHEN
- Missing inclusive range checks
Ready to prove it?
Pass the test and this pattern moves unknown
