Query the two cities inSTATIONwith the shortest and longestCITYnames, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically. TheSTATIONtable is described as follows:
whereLAT_Nis the northern latitude andLONG_Wis the western longitude.
Sample Input
For example,CITYhas four entries:DEF, ABC, PQRSandWXY.
Sample Output
ABC 3
PQRS 4
Explanation
When ordered alphabetically, theCITYnames are listed asABC, DEF, PQRS,andWXY, with lengths 3, 3, 4 and 3. The longest name isPQRS, but there are 3options for shortest named city. ChooseABC, because it comes first alphabetically.
Note You can write two separate queries to get the desired output. It need not be a single query.
My Answer
select city, char_length(city) as len_city
from station
order by len_city, city
limit 1;
select city, char_length(city) as len_city
from station
order by len_city desc, city
limit 1;