관리 메뉴

TEAM EDA

[HackerRank] Basic Select : Weather Observation Station 1~12 본문

EDA Study/SQL

[HackerRank] Basic Select : Weather Observation Station 1~12

김현우 2021. 2. 7. 22:07

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Q1. Query a list of CITY and STATE from the STATION table.

SELECT CITY, STATE FROM STATION; 

Q3. 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 CITY FROM STATION
    WHERE ID % 2 = 0
    GROUP BY CITY;

Q4. Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table. For example, if there are three records in the table with CITY values 'New York', 'New York', 'Bengalaru', there are 2 different city names: 'New York' and 'Bengalaru'. The query returns 1, because total number of records - number of unique city names = 3 - 2 = 1

SELECT COUNT(CITY) - COUNT(DISTINCT CITY) FROM STATION;

Q5. 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. Sample Input

Sample Input

For example, CITY has four entries: DEF, ABC, PQRS and WXY.

Sample Output

ABC 3
PQRS 4

Explanation

When ordered alphabetically, the CITY names are listed as ABC, DEF, PQRS, and WXY, with lengths 3, 3, 4 and 3. The longest name is PQRS, but there are 3 options for shortest named city. Choose ABC, because it comes first alphabetically.

Note

You can write two separate queries to get the desired output. It need not be a single query.

SELECT CITY, CHAR_LENGTH(CITY)
    FROM STATION
    ORDER BY 2 ASC, 1 LIMIT 1;

SELECT CITY, CHAR_LENGTH(CITY)
    FROM STATION
    ORDER BY 2 DESC, 1 DESC LIMIT 1;

Q6. 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 REGEXP '^[aeiou]';

Q7. Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.

SELECT DISTINCT CITY FROM STATION 
    WHERE CITY REGEXP '[aeiou]$';

Q8. 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.

SELECT DISTINCT CITY FROM STATION 
    WHERE CITY REGEXP '^[aeiou]' AND CITY REGEXP '[aeiou]$';

Q9. Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.

SELECT DISTINCT CITY FROM STATION 
    WHERE CITY REGEXP '^[^aeiou]';

Q10. Query the list of CITY names from STATION that do not end with vowels. Your result cannot contain duplicates.

SELECT DISTINCT CITY FROM STATION 
    WHERE CITY REGEXP '[^aeiou]$';

Q11. 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.

SELECT DISTINCT CITY FROM STATION 
    WHERE CITY REGEXP '^[^aeiou]' OR CITY REGEXP '[^aeiou]$';

Q12. 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.

SELECT DISTINCT CITY FROM STATION 
    WHERE CITY REGEXP '^[^aeiou]' AND CITY REGEXP '[^aeiou]$';