-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLHackerrankSolutionsPartI.txt
85 lines (62 loc) · 5.47 KB
/
SQLHackerrankSolutionsPartI.txt
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
//SQL Preparation on Hackerrank PART I//
//WRITING ON MYSQL//
1. Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA.
1. select * from CITY where POPULATION > 100000 and COUNTRYCODE = "USA";
2. Query the NAME field for all American cities in the CITY table with populations larger than 120000. The CountryCode for America is USA.
2. select NAME from CITY where POPULATION > 120000 and COUNTRYCODE = "USA";
3. Query all columns (attributes) for every row in the CITY table.
3. select * from CITY;
4. Query all columns for a city in CITY with the ID 1661.
4. select * from CITY where ID = "1661";
5. Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.
5. 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.
6. select NAME from CITY where COUNTRYCODE ="JPN";
7. Query a list of CITY and STATE from the STATION table.
7. 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.
8. select DISTINCT CITY from STATION where ID MOD 2 = 0;
9. Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.
9. select (count(CITY)-count(DISTINCT CITY)) from STATION;
10. 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.
10. select CITY,length(CITY) from STATION order by length(CITY) asc, CITY limit 1;
select CITY,length(CITY) from STATION order by length(CITY) desc, CITY limit 1;
11. Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.
11. select distinct CITY from STATION where regexp_like(CITY,'^[AEIOU]');
12. Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.
12. select distinct CITY from STATION where regexp_like(CITY,'[AEIOU]$');
13. Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.
13. select distinct CITY from STATION where regexp_like(CITY,'^[aeiouAEIOU].*[aeiouAEIOU]$');
14. Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.
14. select distinct CITY from STATION where not regexp_like(CITY,'^[AEIOU]');
15. Query the list of CITY names from STATION that do not end with vowels. Your result cannot contain duplicates.
15. select distinct CITY from STATION where not regexp_like(CITY,'[AEIOU]$');
16. Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your result cannot contain duplicates.
16. select distinct CITY from STATION where not regexp_like(CITY,'^[aeiouAEIOU].*[aeiouAEIOU]$');
17. Query the list of CITY names from STATION that do not start with vowels and do not end with vowels. Your result cannot contain duplicates.
17. select distinct CITY from STATION where LOWER(SUBSTR(CITY,1,1)) NOT IN ('a','e','i','o','u') AND LOWER(SUBSTR(CITY,LENGTH(CITY),1)) NOT IN ('a','e','i','o','u');
18. Query the Name of any student in STUDENTS who scored higher than 75 Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.
18. select Name from STUDENTS where Marks > 75 order by RIGHT(NAME, 3), ID asc;
19. Generate the following two result sets: 1. Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S)
2. Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:
There are a total of [occupation_count] [occupation]s where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.
19. SELECT CONCAT(
Name,'(',upper(substring(occupation,1,1)),')'
) FROM OCCUPATIONS
ORDER BY Name;
SELECT CONCAT(
"There are a total of",' ',COUNT(occupation),' ',
lower(occupation),'s',"."
) FROM OCCUPATIONS
GROUP BY occupation
ORDER BY COUNT(OCCUPATION) ASC;
20. Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:
Equilateral: It's a triangle with 3 sides of equal length.Isosceles: It's a triangle with 2 sides of equal length.Scalene: It's a triangle with 3 sides of differing lengths. Not A Triangle: The given values of A, B, and C don't form a triangle.
20. SELECT
CASE
WHEN A + B <= C or A + C <= B or B + C <= A THEN 'Not A Triangle'
WHEN A = B and B = C THEN 'Equilateral'
WHEN A = B or A = C or B = C THEN 'Isosceles'
WHEN A <> B and B <> C THEN 'Scalene'
END tuple
FROM TRIANGLES;