[HackerRank MySQL] Weather Observation Station 1, 3-12 + REGEXP (Basic) & MOD() Function

2021. 1. 5. 20:25Today I Learned.../MySQL

Weather Observation Station 1

Query a list of CITY and STATE from the STATION table. 
The STATION table is described as follows: 

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

select city, state
from station

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.

-- 1
select distinct city
from station
where id % 2 = 0
-- 2
SELECT DISTINCT city
FROM station
WHERE MOD(id, 2) = 0

 

MOD() Function

  • MOD(x, y): returns the remainder of a number x divided by another number y
    • e.g. MOD(8, 3) returns 2

 


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.

 

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 , because

total number of records - number of unique city names = 3 - 2 = 1.

select count(city) - count(distinct city)
from station

Weather Observation Station 5

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

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, length(city)
from station
where id = (select id from station order by length(city), city limit 1)
or id = (select id from station order by length(city) desc, city limit 1)

Weather Observation Station 6

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]'

Weather Observation Station 7

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]$'

Weather Observation Station 8

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.

-- 1
select distinct city
from station
where city regexp '^[aeiou]' and city regexp '[aeiou]$'
-- 2
select distinct city
from station
where city regexp '^[aeiou].*[aeiou]$'
. 임의의 한 문자를 표현 ('ㄱ'이 마지막으로 끝남)
* 'ㄱ'이 0번 이상 반복

Weather Observation Station 9

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 not city regexp '^[aeiou]'

Weather Observation Station 10

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 not city regexp '[aeiou]$'

Weather Observation Station 11

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.

-- 1
select distinct city
from station
where not city regexp '^[aeiou].*[aeiou]$'
-- 2
select distinct city
from station
where city regexp '^[^aeiou]' or city regexp '[^aeiou]$'
[^ㄱㄴㄷㄹ] ㄱ, ㄴ, ㄷ, ㄹ에 해당하지 않는 문자 집합

Weather Observation Station 12

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.

-- 1
select distinct city
from station
where not city regexp '^[aeiou]' and not city regexp '[aeiou]$'
-- 2
select distinct city
from station
where city regexp '^[^aeiou].*[^aeiou]$'
[^aeiou] : aeiou에 속하지 않는 집합을 의미

문제출처: 해커랭크