We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Weather Observation Station 5
Weather Observation Station 5
Sort by
recency
|
7316 Discussions
|
Please Login in order to post a comment
I first calculate the minimum and maximum city name length. Then I filter cities having those lengths. After that, I sort by length and alphabetically to handle ties, and finally return only two rows. But when I hit run it was successful but when I submitted, it failed test case
If anyone don’t mind can explain this
My query: WITH lens AS ( SELECT MIN(LENGTH(CITY)) AS min_len, MAX(LENGTH(CITY)) AS max_len FROM STATION ) SELECT CITY, LENGTH(CITY) AS len FROM STATION, lens WHERE LENGTH(CITY) IN (lens.min_len, lens.max_len) GROUP BY CITY ORDER BY LENGTH(CITY),
CITY
LIMIT 2;
SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY), CITY LIMIT 1;
SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY) DESC, CITY LIMIT 1;
two methods you can use here first using multiple windows function
select city, name_length from (Select length(city) as name_length, city, row_number() over (partition by length(city) order by city) as rn, max(length(city)) over () as max_length, min(length(city)) over () as min_length from station) t where (name_length=max_length or name_length=min_length) and rn=1 order by city asc
Or you can do it using union this one is easier
( SELECT city, LENGTH(city) AS name_length FROM station ORDER BY LENGTH(city), city LIMIT 1 ) UNION ALL ( SELECT city, LENGTH(city) AS name_length FROM station ORDER BY LENGTH(city) DESC, city LIMIT 1 );
asc
select city ,len_city from ( select city ,length(city) as len_city ,row_number() over(order by length(city) asc, city asc) as min_len ,row_number() over(order by length(city) desc, city asc) as max_len from station ) t where min_len = 1 or max_len = 1 ;
SELECT city,length(city) from station where length(city)=(SELECT min(length(city)) from station) ORDER BY city asc limit 1;
SELECT city,length(city) from station where length(city)=(select max(length(city)) from station) order by city desc limit 1;