[Solved] Hackerrank SQL challenge


Since you want first and last, I’d probably just use a union and top 1. makes it clear as to what you’re after and easy to maintain.

And since you can use alias in order by… I’d alias len(city)

SELECT TOP 1 
    city, len(city) LenCity 
FROM 
    station 
ORDER BY
    LenCity ASC 

UNION ALL 

SELECT TOP 1 
    city, Len(City) lenCity 
FROM 
    station 
ORDER BY 
    LenCity DESC

solved Hackerrank SQL challenge