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.
Good Approach but when we are asked to display as single result set . This works
-- creating sample STATION table and inserting value
create table STATION(id int , city varchar(50));
insert into STATION(id,city) values (1,'madras'),(2,'Salem'),(3,'Trichy'),(4,'Ambur'),(5,'Velur');
-- CTE table with extra colum having city string length
with AddedLengthCol
as
(
select city, len(city) as lencount from STATION
)
-- Used Rank_Dense to sove it .
select s.city , s.lencount,rank1 from (select city , lencount , DENSE_RANK() over (PARTITION BY lencount order by city ) as rank1 from AddedLengthCol) as s
where s.rank1=1 and ( s.lencount = (select max(lencount) from AddedLengthCol) or s.lencount = (select min(lencount) from AddedLengthCol))
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 →
Answer in MS SQL SERVER:
Good Approach but when we are asked to display as single result set . This works
-- creating sample STATION table and inserting value create table STATION(id int , city varchar(50)); insert into STATION(id,city) values (1,'madras'),(2,'Salem'),(3,'Trichy'),(4,'Ambur'),(5,'Velur');
-- CTE table with extra colum having city string length with AddedLengthCol as ( select city, len(city) as lencount from STATION )
-- Used Rank_Dense to sove it . select s.city , s.lencount,rank1 from (select city , lencount , DENSE_RANK() over (PARTITION BY lencount order by city ) as rank1 from AddedLengthCol) as s where s.rank1=1 and ( s.lencount = (select max(lencount) from AddedLengthCol) or s.lencount = (select min(lencount) from AddedLengthCol))