SQL query for finding the longest name and shortest name in a table

后端 未结 29 2061
春和景丽
春和景丽 2021-01-31 00:11

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         


        
29条回答
  •  无人共我
    2021-01-31 00:23

    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;
    

提交回复
热议问题