-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuided project_CIA factbook.Rmd
91 lines (75 loc) · 2.05 KB
/
Guided project_CIA factbook.Rmd
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
---
title: "Analyzing CIA Factbook"
author: "Fredrick Boshe"
date: "08/04/2021"
output:
github_document: default
rmarkdown::github_document: default
---
Load Packages and establish DB connection
```{r packages, include = TRUE, Message=FALSE, warning=FALSE, Echo=FALSE}
library(RSQLite)
con<-dbConnect(SQLite(), "./factbook.db")
```
```{sql, connection=con, results="hide"}
--Preview the tables available
SELECT *
FROM sqlite_master
WHERE type='table';
```
```{sql, connection=con, max.print=5}
--First 5 rows from facts table
SELECT *
FROM facts
LIMIT 5;
```
### **Min and Max populations states**
```{sql, connection=con, max.print=5}
SELECT MIN(population) as min_pop, MAX(population) as max_pop,
MIN(population_growth) as min_pop_g, MAX(population_growth) as max_pop_g
FROM facts;
```
### **Country with the minimum population**
```{sql, connection=con, max.print=5}
SELECT *
FROM facts
WHERE population ==(SELECT MIN(population)
FROM facts);
```
### **Country with the maximum population**
```{sql, connection=con, max.print=5}
SELECT *
FROM facts
WHERE population ==(SELECT MAX(population)
FROM facts);
```
### **Min and Max populations states (excluding 'World')**
```{sql, connection=con, max.print=5}
SELECT MIN(population) as min_pop, MAX(population) as max_pop,
MIN(population_growth) as min_pop_g, MAX(population_growth) as max_pop_g
FROM facts
WHERE name != "World";
```
### **Average value of Population and Area (excluding 'World')**
```{sql, connection=con, max.print=5}
SELECT AVG(population), AVG(area)
FROM facts
WHERE name != "World";
```
### **Countries with an above average population and below average area**
```{sql, connection=con, max.print=5}
SELECT*
FROM facts
WHERE population > (SELECT AVG(population)
FROM facts
WHERE name != "World")
AND area < (SELECT AVG(area)
FROM facts
WHERE name != "World");
```
### **Countries with the most people**
```{sql, connection=con, max.print=5}
SELECT*, MAX(population)
FROM facts
WHERE name != "World";
```