-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackerrank_sql_WeatherObservationStation5.sql
More file actions
29 lines (22 loc) · 829 Bytes
/
hackerrank_sql_WeatherObservationStation5.sql
File metadata and controls
29 lines (22 loc) · 829 Bytes
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
-- 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.
-- CASE1 OK
SELECT CITY, LENGTH(CITY)
FROM (SELECT CITY, LENGTH(CITY) AS LENG
FROM STATION
ORDER BY LENG, CITY)
WHERE ROWNUM <= 1
UNION
SELECT CITY, LENGTH(CITY)
FROM (SELECT CITY, LENGTH(CITY) AS LENG
FROM STATION
ORDER BY LENG DESC, CITY)
WHERE ROWNUM <= 1;
-- CASE2 OK
SELECT CITY, LENGTH(CITY)
FROM (SELECT CITY,
ROW_NUMBER() OVER(ORDER BY LENGTH(CITY), CITY) AS RN,
COUNT(*) OVER() AS CNT
FROM STATION)
WHERE RN IN (1, CNT);