I have a table with one of the columns is of type varchar(city). and want to find the longest and shortest of values stored in that column.
select a.city, a.city
For oracle SQL in one resulting table. This will retrieve the min and max city names, and if same length will get the first city sorted in alphabetic order.
SELECT * FROM (
SELECT CITY, LENGTH(CITY) CITY_LENGTH
FROM STATION
ORDER BY CITY_LENGTH ASC, CITY ASC ) MAX_LEN
WHERE ROWNUM <= 1
UNION
SELECT * FROM (
SELECT CITY, LENGTH(CITY) CITY_LENGTH
FROM STATION
ORDER BY CITY_LENGTH DESC, CITY ASC ) MIN_LENGTH
WHERE ROWNUM <= 1;