WITH loss AS(
WITH fa_1990 AS (
SELECT
SUM(forest_area_sqkm) total_fa_1990
FROM
forestation
WHERE
year = 1990
AND LOWER(country_name) != 'world'
),
fa_2016 AS (
SELECT
SUM(forest_area_sqkm) total_fa_2016
FROM
forestation
WHERE
year = 2016
AND LOWER(country_name) != 'world'
)
SELECT
ABS(total_fa_2016 - total_fa_1990) AS fa_change_16_90
FROM
fa_2016, fa_1990
),
countries AS (
SELECT
f.country_name,
ABS(l.fa_change_16_90 - f.total_area_sq_km) AS differance
--f.total_area_sq_km
FROM
forestation f, loss l
WHERE
year = 2016 AND LOWER(country_name) != 'world'
ORDER BY differance ASC
LIMIT 1
)
SELECT *
FROM countries
-----------------------------------------OR----------------------------------------------------------------
--same code but using "world" instead of sum of all filtered countries
--the reason why we used sum of all countries is because excluding some null values that counted in world
-- if we didn't exclude null values the result would be PERU