Average Height Per Floor
Beginner Mode
Scenario
You have a dataset of architectural structures with their heights and floor counts, and you need to compute a per-floor height metric.
Task
Write a Snowflake SQL query that:
- Selects
id,name,city,countryfrom{{ ref("buildings") }} - Calculates
avg_height_per_floorasheight_m / floors, rounded to 2 decimal places - Returns 0 for
avg_height_per_floorwhenfloorsis zero (to avoid division by zero)
Schema
buildings
| Column | Type | Description |
|---|---|---|
| id | Integer | Unique building identifier |
| name | String | Name of the building |
| city | String | City where the building is located |
| country | String | Country where the building is located |
| height_m | Float | Total height of the building in meters |
| floors | Integer | Number of floors in the building |
Example
buildings:
| id | name | city | country | height_m | floors |
|---|---|---|---|---|---|
| 1 | Apex Tower | Toronto | Canada | 320.5 | 65 |
| 2 | Summit Spire | Seoul | South Korea | 555.7 | 123 |
| 3 | Horizon Plaza | Sydney | Australia | 271.0 | 67 |
| 4 | Central Hub | Berlin | Germany | 368.0 | 0 |
| 5 | Coastal Point | Miami | USA | 240.8 | 55 |
Expected Output:
| id | name | city | country | avg_height_per_floor |
|---|---|---|---|---|
| 1 | Apex Tower | Toronto | Canada | 4.93 |
| 2 | Summit Spire | Seoul | South Korea | 4.52 |
| 3 | Horizon Plaza | Sydney | Australia | 4.04 |
| 4 | Central Hub | Berlin | Germany | 0 |
| 5 | Coastal Point | Miami | USA | 4.38 |
Note: Central Hub has 0 floors, so
avg_height_per_flooris set to 0 instead of causing a division error. All other values areheight_m / floorsrounded to 2 decimal places.
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Amazon