-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrank Challenges
More file actions
82 lines (57 loc) · 2.46 KB
/
Hackerrank Challenges
File metadata and controls
82 lines (57 loc) · 2.46 KB
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
--Below is my SQL solutions to problems found on the Hacker rank website.
1.Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA.
SELECT *
FROM CITY
WHERE COUNTRYCODE = 'USA'
AND POPULATION > 100000;
2. Query the NAME field for all American cities in the CITY table with populations larger than 120000. The CountryCode for America is USA.
SELECT NAME
FROM CITY
WHERE COUNTRYCODE = 'USA'
AND POPULATION > 120000;
3. Query all columns (attributes) for every row in the CITY table.
SELECT *
FROM CITY;
4. Query all columns for a city in CITY with the ID 1661
SELECT *
FROM CITY
WHERE ID = 1661;
5. Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.
SELECT *
FROM CITY
WHERE COUNTRYCODE = 'JPN';
6. Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.
SELECT NAME
FROM CITY
WHERE COUNTRYCODE ='JPN';
7. Query a list of CITY and STATE from the STATION table.
SELECT CITY,
STATE
FROM STATION;
8. 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.
SELECT DISTINCT CITY
FROM STATION
WHERE MoD(ID,2) = 0;
9. 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.
SELECT Country.Continent,
FLOOR(AVG(City.Population))
FROM COUNTRY, CITY
WHERE Country.Code = City.CountryCode
GROUP BY Country.Continent;
10. Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.
SELECT (count(CITY) - count(distinct CITY))
FROM STATION;
11. Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
SELECT CITY, length(CITY)
FROM STATION
ORDER BY length(CITY) DESC, CITY
LIMIT 1;
SELECT CITY, length(CITY)
FROM STATION
ORDER BY length(CITY), CITY
LIMIT 1;
12. Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.
SELECT DISTINCT(CITY)
FROM STATION
WHERE CITY LIKE 'a%' OR CITY LIKE 'e%' OR CITY LIKE 'i%' OR CITY LIKE 'o%'
OR CITY LIKE 'u%';