-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhackerrank_SQL_challenges.sql
122 lines (76 loc) · 3.43 KB
/
hackerrank_SQL_challenges.sql
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
------- HackerRank's SQL 'Basic Select' and 'Basic Join' -------
----------
-- Basic Select -> Revising the Select Query I
-- Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA.
-- Solution:
SELECT * FROM city WHERE COUNTRYCODE = 'USA' AND population > 100000;
----------
-- Basic Select -> Revising the Select Query II
-- Query the NAME field for all American cities in the CITY table with populations larger than 120000. The CountryCode for America is USA.
-- Solution:
SELECT name FROM city WHERE countrycode = 'USA' AND population > 120000;
----------
-- Basic Select -> Select All
-- Query all columns (attributes) for every row in the CITY table.
-- Solution:
SELECT * FROM city;
----------
-- Basic Select -> Select By ID
-- Query all columns for a city in CITY with the ID 1661.
-- Solution:
SELECT * FROM city WHERE id = 1661;
----------
-- Basic Select -> Japanese Cities' Attributes
-- Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.
-- Solution:
SELECT * FROM city WHERE countrycode = 'JPN';
----------
-- Basic Select -> Japanese Cities' Names
-- Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.
-- Solution:
SELECT name FROM city WHERE countrycode = 'JPN';
----------
-- Basic Select -> Weather Observation Station 1
-- Query a list of CITY and STATE from the STATION table.
-- Solution:
SELECT city, state FROM station;
----------
-- Basic Select -> Weather Observation Station 3
-- Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer.
-- Solution:
SELECT DISTINCT city FROM station WHERE id % 2 = 0;
----------
-- Basic Select -> Weather Observation Station 4
-- Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.
-- Solution:
SELECT (COUNT(city) - COUNT(DISTINCT city)) FROM station;
----------
-- Basic Join -> African Cities
-- Given the CITY and COUNTRY tables, query the names of all cities where the CONTINENT is 'Africa'.
-- Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
-- Solution:
SELECT city.name FROM city JOIN country ON city.countrycode = country.code WHERE continent = 'Africa';
----------
-- Basic Join -> Average Population of Each Continent
-- Given the CITY and COUNTRY tables, query the names of all the continents (COUNTRY.Continent) and their respective average city populations (CITY.Population) rounded down to the nearest integer.
-- Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
-- Solution:
SELECT country.continent, FLOOR(AVG(city.population)) FROM city JOIN country ON city.countrycode = country.code GROUP BY country.continent;
----------
-- Basic Join -> Population Census
-- Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.
-- Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
-- Solution:
SELECT SUM(city.population) FROM city JOIN country ON city.countrycode = country.code WHERE country.continent = 'Asia';
----------
-- Basic Join -> The Report (med)
-- You are given two tables: Students and Grades. Students contains three columns ID, Name and Marks.
-- Solution:
----------
--
--
-- Solution:
----------
--
--
-- Solution: