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.
First I selected the cities and ranked them basing on their length.
Then i filtered min or max lengths.
Finaly wraped the query in a CTE and filtered the first row of every rank.
WITH cte_cities AS (
SELECT city
, LEN(city) as citylength
, ROW_NUMBER() OVER(PARTITION BY LEN(city) ORDER BY city) as cityorder
FROM station
WHERE LEN(city) = (SELECT MIN(LEN(city)) FROM station)
OR LEN(city) = (SELECT MAX(LEN(city)) FROM station)
)
select city, citylength
from cte_twocities
where cityorder=1
order by citylength, city asc
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Weather Observation Station 5
You are viewing a single comment's thread. Return to all comments →
First I selected the cities and ranked them basing on their length. Then i filtered min or max lengths. Finaly wraped the query in a CTE and filtered the first row of every rank.
WITH cte_cities AS ( SELECT city , LEN(city) as citylength , ROW_NUMBER() OVER(PARTITION BY LEN(city) ORDER BY city) as cityorder FROM station WHERE LEN(city) = (SELECT MIN(LEN(city)) FROM station) OR LEN(city) = (SELECT MAX(LEN(city)) FROM station) ) select city, citylength from cte_twocities where cityorder=1 order by citylength, city asc