-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.sql
More file actions
257 lines (222 loc) · 9.4 KB
/
Copy pathProject.sql
File metadata and controls
257 lines (222 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
DROP VIEW IF EXISTS forestation;
CREATE VIEW forestation AS (
SELECT
f.country_code,
f.year,
f.forest_area_sqkm,
l.total_area_sq_mi,
r.country_name,
r.region,
r.income_group,
f.forest_area_sqkm / (l.total_area_sq_mi * 2.59) * 100 AS forest_sqkm_percent
FROM
forest_area f
JOIN land_area l ON f.country_code = l.country_code
AND f.year = l.year
JOIN regions r ON f.country_code = r.country_code
);
Part -1 GLOBAL SITUATION
Question 1.What was the total forest area (in sq km) of the world in 1990? Please keep in mind that you can use the country record denoted as “World" in the region table.
SELECT ROUND(SUM(forest_area_sqkm)::numeric,2) AS forest_area_1990
FROM forestation
WHERE year = 1990 AND country_name = 'World';
Que.2 What was the total forest area (in sq km) of the world in 2016? Please keep in mind that you can use the country record in the table is denoted as “World.”
SELECT ROUND(SUM(forest_area_sqkm)::numeric,2) AS forest_area_2016
FROM forestation
WHERE year = 2016 AND country_name = 'World';
Que.3 What was the change (in sq km) in the forest area of the world from 1990 to 2016?
WITH forest_1990 AS (
SELECT ROUND(SUM(forest_area_sqkm)::numeric,2) AS forest_area_1990
FROM forestation
WHERE year = 1990 AND Country_name = 'World'),
forest_2016
AS (
SELECT ROUND(SUM(forest_area_sqkm)::numeric,2) AS forest_area_2016
FROM forestation
WHERE year = 2016 AND Country_name = 'World')
SELECT forest_area_1990 - forest_area_2016 AS diffrence
FROM forest_1990,forest_2016;
Que.4 What was the percent change in forest area of the world between 1990 and 2016?
WITH forest_1990 AS (
SELECT ROUND(SUM(forest_area_sqkm)::numeric,2) AS forest_area_1990
FROM forestation
WHERE year = 1990 AND Country_name = 'World'),
forest_2016
AS (
SELECT ROUND(SUM(forest_area_sqkm)::numeric,2) AS forest_area_2016
FROM forestation
WHERE year = 2016 AND Country_name = 'World')
SELECT forest_area_1990 - forest_area_2016 AS diffrence, round((forest_area_2016 - forest_area_1990)*100/forest_area_1990::numeric,2) AS precentage
FROM forest_1990,forest_2016;
que.5 If you compare the amount of forest area lost between 1990 and 2016, to which country's total area in 2016 is it closest to?
WITH forest_1990 AS (
SELECT ROUND(SUM(forest_area_sqkm) :: numeric, 2) AS forest_area_1990
FROM forestation
WHERE year = 1990 AND Country_name = 'World'
),
forest_2016 AS (
SELECT ROUND(SUM(forest_area_sqkm) :: numeric, 2) AS forest_area_2016
FROM forestation
WHERE year = 2016 AND Country_name = 'World'
),
diff AS (
SELECT forest_area_1990, forest_area_2016, forest_area_1990 - forest_area_2016 AS diffrence
FROM forest_1990, forest_2016
),
total_area AS (
SELECT country_name, total_area_sq_mi * 2.59 AS total_area_sqkm
FROM forestation
)
SELECT distinct round(total_area_sqkm :: numeric, 2) AS area_same, country_name
FROM diff, total_area
WHERE diffrence >= total_area_sqkm
ORDER BY 1 DESC
LIMIT 5;
part--2 REGIONAL OUTLOOK
Que.1 a. What was the percent forest of the entire world in 2016? Which region had the HIGHEST percent forest in 2016, and which had the LOWEST, to 2 decimal places?
SELECT region, ROUND ((SUM(forest_area_sqkm) * 100/ SUM(total_area_sq_mi * 2.59))::numeric, 2) AS percentag
FROM forestation
WHERE year = 2016
GROUP BY region
ORDER BY percentag DESC;
Que.2 What was the percent forest of the entire world in 1990? Which region had the HIGHEST percent forest in 1990, and which had the LOWEST, to 2 decimal places?
SELECT region, ROUND ((SUM(forest_area_sqkm) * 100/ SUM(total_area_sq_mi * 2.59))::numeric, 2) AS percentag
FROM forestation
WHERE year = 1990
GROUP BY region
ORDER BY percentag DESC;
QUE. 3 Based on the table you created, which regions of the world DECREASED in forest area from 1990 to 2016?
with fr1990 AS (
select country_name, forest_area_sqkm
from forestation
where year = 1990
),
fr2016 AS (
select country_name, forest_area_sqkm
from forestation
where year = 2016
),
pr1990 AS (
SELECT ROUND ((SUM(forest_area_sqkm) * 100 / SUM(total_area_sq_mi * 2.59)) :: numeric, 2) AS percentag, country_name
FROM forestation
WHERE year = 1990
GROUP BY country_name
),
pr2016 AS (
SELECT ROUND ((SUM(forest_area_sqkm) * 100 / SUM(total_area_sq_mi * 2.59)) :: numeric,2) AS percentag, country_name
FROM forestation
WHERE year = 2016
GROUP BY country_name
)
select round((b.forest_area_sqkm - a.forest_area_sqkm) :: numeric,2) AS diff, a.country_name, d.percentag - c.percentag AS pre
from fr1990 a
JOIN fr2016 b
ON a.country_name = b.country_name
JOIN pr1990 c
ON c.country_name = b.country_name
JOIN pr2016 d
ON d.country_name = b.country_name
where b.forest_area_sqkm - a.forest_area_sqkm IS NOT NULL AND d.percentag - c.percentag IS NOT NULL
order by diff desc;
PART-3 COUNTRY-LEVEL DETAIL
Que.2 Which 5 countries saw the largest amount decrease in forest area from 1990 to 2016? What was the difference in forest area for each?
WITH fa1990 AS (
SELECT forest_sqkm_percent, country_name, region
FROM forestation
WHERE year = 1990 AND forest_sqkm_percent IS NOT NULL AND country_name !='World'
),
fa2016 AS (
SELECT forest_sqkm_percent, country_name, region
FROM forestation
WHERE year = 2016 AND forest_sqkm_percent IS NOT NULL AND country_name !='World'
)
SELECT DISTINCT ROUND((b.forest_sqkm_percent - a.forest_sqkm_percent )::numeric, 2) AS pr, a.country_name, a.region
FROM fa2016 a
JOIN fa1990 b
ON a.country_name = b.country_name
ORDER BY pr desc;
Que.1 Which 5 countries saw the largest percent decrease in forest area from 1990 to 2016? What was the percent change to 2 decimal places for each?
WITH fa1990 AS (
SELECT SUM(forest_area_sqkm) AS sum_1990, country_name, region
FROM forestation
WHERE year = 1990 AND forest_area_sqkm IS NOT NULL AND country_name != 'World'
GROUP BY country_name, region
),
fa2016 AS (
SELECT SUM(forest_area_sqkm) AS sum_2016, country_name, region
FROM forestation
WHERE year = 2016 AND forest_area_sqkm IS NOT NULL AND country_name != 'World'
GROUP BY country_name, region
)
SELECT ROUND((sum_1990 - sum_2016) :: numeric, 2) AS di, a.country_name, a.region
FROM fa2016 a
JOIN fa1990 b
ON a.country_name = b.country_name
ORDER BY di DESC;
que 3 If countries were grouped by percent forestation in quartiles, which group had the most countries in it in 2016?
WITH
fa2016 AS (
SELECT country_name, forest_sqkm_percent, CASE WHEN forest_sqkm_percent >= 75 THEN '75%-100%'
WHEN forest_sqkm_percent >= 50 THEN '50%-75%'
WHEN forest_sqkm_percent >= 25 THEN '25%-50%'
ELSE '0-25%'
END AS qurtile
FROM forestation
WHERE year = 2016 AND forest_sqkm_percent IS NOT NULL AND country_name != 'World'
)
SELECT distinct qurtile, count(*)
FROM fa2016
GROUP BY 1
ORDER BY 1;
que 5 How many countries had a percent forestation higher than the United States in 2016?
WITH t1 AS (
SELECT forest_sqkm_percent, country_name
FROM forestation
WHERE country_name = 'United States' and year = 2016 AND forest_sqkm_percent IS NOT NULL AND country_name != 'World'
group by 1,2)
SELECT b.forest_sqkm_percent , a.country_name, count(*), CASE WHEN a.forest_sqkm_percent >= b.forest_sqkm_percent then 'country_name' end AS co
FROM forestation a
JOIN t1 b
ON a.country_name = b.country_name
WHERE a.forest_sqkm_percent >= b.forest_sqkm_percent AND year = 2016 AND b.forest_sqkm_percent IS NOT NULL AND a.country_name != 'World'
GROUP BY 1,2,a.forest_sqkm_percent
ORDER BY 1,2 DESC;
SELECT distinct round(forest_sqkm_percent::numeric, 2), country_name
FROM forestation
WHERE forest_sqkm_percent > 33.39 and year = 2016 AND forest_sqkm_percent IS NOT NULL AND country_name != 'World'
group by 1,2
Order by 1 desc;
Que 4 List all of the countries that were in the 4th quartile (percent forest > 75%) in 2016.
WITH
fa2016 AS (
SELECT country_name, forest_sqkm_percent, CASE WHEN forest_sqkm_percent >= 75 THEN '75%-100%'
WHEN forest_sqkm_percent >= 50 THEN '50%-75%'
WHEN forest_sqkm_percent >= 25 THEN '25%-50%'
ELSE '0-25%'
END AS qurtile
FROM forestation
WHERE year = 2016 AND forest_sqkm_percent IS NOT NULL AND country_name != 'World'
)
SELECT distinct a.qurtile, a.country_name, b.region, round(a.forest_sqkm_percent::numeric, 2)
FROM fa2016 a
JOIN forestation b
ON a.country_name = b.country_name
WHERE A.qurtile = '75%-100%'
ORDER BY 1 desc;
PArt >3 Q.1) If countries were grouped by percent forestation in quartiles, which group had the most countries in it in 2016?
WITH fa1990 AS (
SELECT SUM(forest_area_sqkm) AS sum_1990, country_name, region
FROM forestation
WHERE year = 1990 AND forest_area_sqkm IS NOT NULL AND country_name != 'World'
GROUP BY country_name, region
),
fa2016 AS (
SELECT SUM(forest_area_sqkm) AS sum_2016, country_name, region
FROM forestation
WHERE year = 2016 AND forest_area_sqkm IS NOT NULL AND country_name != 'World'
GROUP BY country_name, region)
SELECT ROUND((sum_1990 - sum_2016)::numeric, 2) AS di , a.country_name, a.region
FROM fa2016 a
JOIN fa1990 b
ON a.country_name = b.country_name
ORDER BY di DESC;