Annual GDP Growth Rate
Beginner Mode

Scenario

You are an economist computing annual GDP growth rates from two separate data sources that share the same schema.

Task

Write a Snowflake SQL query that:

  1. Combines {{ ref("gdp_domestic") }} and {{ ref("gdp_international") }} using UNION ALL
  2. Uses LAG to look up the previous year's GDP for each country (partitioned by country, ordered by year)
  3. Computes GDP_growth_rate as ROUND(((GDP - prev_GDP) / prev_GDP) * 100, 2), returning NULL when no previous year exists
  4. Returns Country, Year, and GDP_growth_rate, sorted by Country ASC then Year ASC

Schema

gdp_domestic and gdp_international (same schema)

Column Type Description
Country String Name of the country
Year Integer Year of the GDP measurement
GDP Double GDP value in billions (USD)

Example

gdp_domestic:

Country Year GDP
Japan 2020 5040.11
Japan 2021 4937.42
Brazil 2020 1444.73

gdp_international:

Country Year GDP
Brazil 2021 1608.98
Japan 2022 4231.14

Expected Output:

Country Year GDP_growth_rate
Brazil 2020 NULL
Brazil 2021 11.37
Japan 2020 NULL
Japan 2021 -2.04
Japan 2022 -14.3

Note: The first year for each country has a NULL growth rate because there is no previous year to compare against. Japan shows negative growth in 2021 and 2022 because its GDP decreased year over year.

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →