Skip to content

Commit a80450a

Browse files
Uploaded Question 1.SQL
1 parent 661da24 commit a80450a

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

SQL Query/Question 1.sql

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*Data Integrity Checking & Cleanup
2+
3+
- Alphabetically list all of the country codes in the continent_map table that appear more than once. Display any values where country_code is null as country_code = "FOO" and make this row appear first in the list, even though it should alphabetically sort to the middle. Provide the results of this query as your answer.
4+
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+
*/
7+
CREATE DATABASE BrainTree
8+
9+
CREATE TABLE `braintree`.`continent_map` (`country_code` text, `continent_code` text);
10+
PREPARE stmt FROM 'INSERT INTO `braintree`.`continent_map` (`continent_code`,`country_code`) VALUES(?,?)'
11+
DEALLOCATE PREPARE stmt
12+
13+
CREATE TABLE `braintree`.`continents` (`continent_code` text, `continent_name` text)
14+
PREPARE stmt FROM 'INSERT INTO `braintree`.`continents` (`continent_name`,`continent_code`) VALUES(?,?)'
15+
DEALLOCATE PREPARE stmt
16+
17+
CREATE TABLE `braintree`.`countries` (`country_code` text, `country_name` text)
18+
PREPARE stmt FROM 'INSERT INTO `braintree`.`countries` (`country_name`,`country_code`) VALUES(?,?)'
19+
DEALLOCATE PREPARE stmt
20+
21+
CREATE TABLE `braintree`.`per_capita` (`country_code` text, `year` int, `gdp_per_capita` double)
22+
PREPARE stmt FROM 'INSERT INTO `braintree`.`per_capita` (`gdp_per_capita`,`country_code`,`year`) VALUES(?,?,?)'
23+
DEALLOCATE PREPARE stmt
24+
25+
/* Replace '' empty strings with NULL*/
26+
USE braintree;
27+
UPDATE continent_map
28+
29+
SET
30+
country_code = CASE country_code WHEN '' THEN NULL ELSE country_code END,
31+
continent_code = CASE continent_code WHEN '' THEN NULL ELSE continent_code END;
32+
33+
/* Select Statement To Pull Up Duplicate Country Codes, FOO on top*/
34+
SELECT
35+
COALESCE(country_code, 'FOO')
36+
FROM
37+
braintree.continent_map
38+
GROUP BY country_code
39+
HAVING COUNT(*) > 1
40+
ORDER BY country_code;
41+
42+
/*Results from first part of question selecting duplicates and brining NULL up top.
43+
44+
country_code:
45+
FOO
46+
ARM
47+
AZE
48+
CYP
49+
GEO
50+
KAZ
51+
RUS
52+
TUR
53+
UMI
54+
55+
Part 2:
56+
57+
Create a temporary table with a new column ID as a row_number on the table after order by contry_code, continent_code*/
58+
CREATE TABLE t1 (
59+
SELECT row_number() over (order by country_code, continent_code asc) as 'ID',country_code
60+
,continent_code
61+
FROM braintree.continent_map );
62+
CREATE TABLE t2 (Select MIN(ID) as ID from t1 group by country_code );
63+
64+
/*Delete the rows that dont have a min ID number after group by country_code*/
65+
Delete From t1 where ID NOT IN(select ID from t2) ;
66+
67+
/*Reset continent_map table*/
68+
Delete From continent_map;
69+
70+
/*Refill continent_map from temp_table*/
71+
insert into continent_map
72+
select country_code, continent_code from t1;
73+
74+
/*drop temporary tables*/
75+
DROP TABLE t1;
76+
DROP TABLE t2;
77+
78+
79+
/*Results in Table continent_map:
80+
81+
country_code continent_code
82+
NULL AS
83+
ABW NA
84+
AFG AS
85+
AGO AF
86+
AIA NA
87+
ALA EU
88+
ALB EU
89+
AND EU
90+
ANT NA
91+
ARE AS
92+
ARG SA
93+
ARM AF
94+
ASM OC
95+
....... etc */

0 commit comments

Comments
 (0)