weather station 5

MySQL solution to the weather observation station 5 problem from HackerRank:

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
CREATE TABLE STATION (
    ID INT PRIMARY KEY,
    CITY VARCHAR(21)
); 

INSERT INTO STATION VALUES(1, 'Ohio');
INSERT INTO STATION VALUES(2, 'Boston');
INSERT INTO STATION VALUES(3, 'Loston');


SELECT CITY, CITY_LENGTH
FROM (
    SELECT CITY, LENGTH(CITY) AS CITY_LENGTH
    FROM STATION
    ORDER BY LENGTH(CITY) ASC, CITY
    LIMIT 1
) AS shortest

UNION ALL

SELECT CITY, CITY_LENGTH
FROM (
    SELECT CITY, LENGTH(CITY) AS CITY_LENGTH
    FROM STATION
    ORDER BY LENGTH(CITY) DESC, CITY
    LIMIT 1
) AS longest;