Skip to content

Commit 023b8ee

Browse files
finished q1 and started q2
1 parent 6b067d9 commit 023b8ee

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

alexander_connelly_code_challenge.txt

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,92 @@ Data Integrity Checking & Cleanup
44

55
- 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.
66

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+
793
2\. List the countries ranked 10-12 in each continent by the percent of year-over-year growth descending from 2011 to 2012.
894

995
The percent of growth should be calculated as: ((2012 gdp - 2011 gdp) / 2011 gdp)
@@ -16,6 +102,27 @@ The list should include the columns:
16102
- country_name
17103
- growth_percent
18104

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*/
106+
107+
SELECT dbo.per_capita.country_code, dbo.continents.continent_code, dbo.continents.continent_name, dbo.countries.country_name, dbo.per_capita.year, dbo.per_capita.gdp_per_capita
108+
FROM dbo.continent_map INNER JOIN
109+
dbo.countries ON dbo.continent_map.country_code = dbo.countries.country_code INNER JOIN
110+
dbo.continents ON dbo.continent_map.continent_code = dbo.continents.continent_code INNER JOIN
111+
dbo.per_capita ON dbo.continent_map.country_code = dbo.per_capita.country_code
112+
113+
/* results from the select look like this:*/
114+
country_code continent_code continent_name country_name year gdp_per_capita
115+
ABW NA North America Aruba 2004 22566.68216
116+
AND EU Europe Andorra 2004 29372.16674
117+
AFG AS Asia Afghanistan 2004 220.0562878
118+
AGO AF Africa Angola 2004 1229.342988
119+
..... and on and on
120+
121+
/*Using this view as my dataset to refer to, I'll answer the GDP question with this query:*/
122+
123+
124+
125+
19126
3\. For the year 2012, create a 3 column, 1 row report showing the percent share of gdp_per_capita for the following regions:
20127

21128
(i) Asia, (ii) Europe, (iii) the Rest of the World. Your result should look something like

0 commit comments

Comments
 (0)