You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: alexander_connelly_code_challenge.txt
+107Lines changed: 107 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,92 @@ Data Integrity Checking & Cleanup
4
4
5
5
- For all countries that have multiple rows in the continent_map table, delete all multiple records leaving only the 1 record per country. The record that you keep should be the first one when sorted by the continent_code alphabetically ascending. Provide the query/ies and explanation of step(s) that you follow to delete these records.
6
6
7
+
CREATE DATABASE BrainTreeSQLCodingChallenge
8
+
9
+
CREATE TABLE continent_map (
10
+
ID int IDENTITY (1 , 1) PRIMARY KEY ,
11
+
country_code nvarchar(3),
12
+
continent_code nvarchar(2)
13
+
);
14
+
CREATE TABLE continents (
15
+
continent_code nvarchar(2),
16
+
continent_name nvarchar(50)
17
+
);
18
+
CREATE TABLE countries (
19
+
country_code nvarchar(3),
20
+
country_name nvarchar(100)
21
+
);
22
+
CREATE TABLE per_capita (
23
+
country_code nvarchar(3),
24
+
year int,
25
+
gdp_per_capita float
26
+
);
27
+
28
+
# Used Flat File Import to Get CSV files into DB Tables
29
+
30
+
31
+
SELECT COALESCE([country_code],'FOO')
32
+
33
+
FROM [dbo].[continent_map]
34
+
35
+
group by country_code
36
+
37
+
having COUNT(*) > 1
38
+
39
+
order by country_code;
40
+
41
+
Results from first part of question selecting duplicates and brining NULL up top.
42
+
43
+
country_code:
44
+
FOO
45
+
ARM
46
+
AZE
47
+
CYP
48
+
GEO
49
+
KAZ
50
+
RUS
51
+
TUR
52
+
UMI
53
+
54
+
Part 2:
55
+
56
+
/* Create a temporary table with a new column ID as a row_number on the table after order by contry_code, continent_code*/
57
+
SELECT row_number() over (order by country_code, continent_code asc) as 'ID',[country_code]
58
+
,[continent_code] into temp_table
59
+
FROM [dbo].[continent_map] ;
60
+
61
+
/*Delete the rows that dont have a min ID number after group by country_code*/
62
+
Delete From temp_table where ID NOT IN (Select MIN(ID) from temp_table group by country_code);
63
+
64
+
/*Reset continent_map table*/
65
+
Delete From continent_map;
66
+
67
+
/*Refill continent_map from temp_table*/
68
+
insert into continent_map
69
+
select country_code, continent_code from temp_table;
70
+
71
+
/*drop temp_table*/
72
+
DROP TABLE temp_table
73
+
74
+
Results in Table continent_map:
75
+
76
+
country_code continent_code
77
+
NULL AS
78
+
ABW NA
79
+
AFG AS
80
+
AGO AF
81
+
AIA NA
82
+
ALA EU
83
+
ALB EU
84
+
AND EU
85
+
ANT NA
86
+
ARE AS
87
+
ARG SA
88
+
ARM AF
89
+
ASM OC
90
+
....... etc
91
+
92
+
7
93
2\. List the countries ranked 10-12 in each continent by the percent of year-over-year growth descending from 2011 to 2012.
8
94
9
95
The percent of growth should be calculated as: ((2012 gdp - 2011 gdp) / 2011 gdp)
@@ -16,6 +102,27 @@ The list should include the columns:
16
102
- country_name
17
103
- growth_percent
18
104
105
+
/* in order to do this one I first created a VIEW in my data base which joined the per_capita table to all other tables appropriately. the join looks like this*/
0 commit comments