Skip to content
Advertisement

How to query for city name as well as length of the smallest city name from the same table

My code is this but gives error, don’t know why. Please help!

select city, 
       min(length(city)) 
from station 
group by length(city)=min(length(city)) 
order by city asc;

Advertisement

Answer

If you just want the city with the shortest name, you can simply order by and limit:

select city, char_length(city) city_length
from station
order by city_length
limit 1

This returns just one row. On the other hand, if you want to allow bottom ties, then you can filter with a subquery, like so:

select city, char_length(city) city_length
from station
where char_length(city) = (select min(char_length(city)) from station)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement