Category: Conditional Expressions
Platform Support:
✅ Snowflake | ✅ BigQuery | ✅ Databricks
Description
CASE provides if-then-else conditional logic in SQL, allowing you to return different values based on conditions. It's essential for data transformation, categorization, and implementing business logic within queries.
Syntax
Simple CASE (equality checks):
Searched CASE (complex conditions):
Platform-Specific Notes
Snowflake:
Fully supports both simple and searched CASE
Also has
IFF(condition, true_value, false_value)shorthandEvaluates conditions sequentially (first match wins)
BigQuery:
Fully supports both CASE types
Also has
IF(condition, true_value, false_value)functionCase-insensitive string comparisons by default
Databricks:
Standard SQL CASE implementation
Also supports Spark SQL
IFfunctionCompatible with Hive syntax
Example 1: Categorizing Values
All Platforms:
Sample Data (products table):
product_name | price |
|---|---|
Mouse | 25.99 |
Keyboard | 89.99 |
Monitor | 349.99 |
Laptop | 1299.99 |
USB Cable | 12.99 |
Result:
product_name | price | price_category | discount_rate |
|---|---|---|---|
Mouse | 25.99 | Budget | 0.05 |
Keyboard | 89.99 | Mid-Range | 0.10 |
Monitor | 349.99 | Premium | 0.15 |
Laptop | 1299.99 | Luxury | 0.15 |
USB Cable | 12.99 | Budget | 0.05 |
Example 2: Simple CASE with Status Codes
All Platforms:
Sample Data (orders table):
order_id | customer_name | status_code |
|---|---|---|
101 | John Smith | 1 |
102 | Maria Garcia | 3 |
103 | Sarah Johnson | 4 |
104 | Ahmed Hassan | 2 |
105 | Lisa Wang | 5 |
Result:
order_id | customer_name | status_code | status_text | status_group |
|---|---|---|---|---|
101 | John Smith | 1 | Pending | In Progress |
102 | Maria Garcia | 3 | Shipped | In Progress |
103 | Sarah Johnson | 4 | Delivered | Complete |
104 | Ahmed Hassan | 2 | Processing | In Progress |
105 | Lisa Wang | 5 | Cancelled | Complete |
Example 3: CASE in Aggregation
All Platforms:
Sample Data (employees table):
department | employee_name | salary | performance_rating |
|---|---|---|---|
Sales | Alice | 75000 | Excellent |
Sales | Bob | 65000 | Good |
Sales | Carol | 85000 | Excellent |
IT | Diana | 90000 | Excellent |
IT | Eric | 95000 | Good |
IT | Frank | 70000 | Average |
Marketing | Grace | 72000 | Good |
Result:
department | total_employees | high_earners | regular_earners | total_bonus_amount |
|---|---|---|---|---|
IT | 3 | 2 | 1 | 13750 |
Marketing | 1 | 0 | 1 | 3600 |
Sales | 3 | 1 | 2 | 10750 |
Example 4: Nested CASE Statements
All Platforms:
Sample Data:
customer_id | order_total | customer_type |
|---|---|---|
101 | 1500 | Premium |
102 | 600 | Premium |
103 | 800 | Standard |
104 | 300 | Standard |
105 | 500 | Guest |
Result:
customer_id | order_total | customer_type | discount_amount |
|---|---|---|---|
101 | 1500 | Premium | 300.00 |
102 | 600 | Premium | 90.00 |
103 | 800 | Standard | 80.00 |
104 | 300 | Standard | 15.00 |
105 | 500 | Guest | 10.00 |
Platform-Specific Shortcuts
Snowflake IFF:
BigQuery IF:
Common Use Cases
Data categorization: Group continuous values into buckets
Conditional aggregation: Count/sum based on conditions
Status translation: Convert codes to human-readable text
Null handling: Provide default values for NULL
Business rules: Implement pricing tiers, discount logic
Data cleaning: Standardize inconsistent values
Best Practices
Order matters: Conditions are evaluated sequentially - first match wins
Always include ELSE: Prevent unexpected NULLs
Use searched CASE for complex logic: More flexible than simple CASE
Consider performance: Multiple CASE in same query can be costly
Extract to views/CTEs: Complex CASE logic may be reusable
NULL handling: Remember NULL doesn't equal anything, including NULL





