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

后端 未结 29 2010
春和景丽
春和景丽 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:21

    length(CITY) will return the length of the string,

    https://www.w3schools.com/sql/func_mysql_length.asp

    (select CITY, length(CITY) from STATION order by length(CITY),CITY limit 1)
    UNION
    (select CITY, length(CITY) from STATION order by length(CITY) DESC limit 1);
    
    0 讨论(0)
  • 2021-01-31 00:22

    In MySQL

    (select city, LENGTH(city) cityLength  from station order by cityLength desc,city asc LIMIT 1)
        union all
        (select city, LENGTH(city) cityLength  from station order by cityLength asc,city asc LIMIT 1)
    
    0 讨论(0)
  • 2021-01-31 00:22

    In Oracle (and any other language that supports analytic functions), using the ROW_NUMBER analytic function you can assign the rows a unique number according to ASCending (or DESCending) length of the city. Since there may be multiple rows with the same length then a secondary order can be applied to get the first city of that length alphabetically. Then all you need is an outer query to filter the results to only the shortest (or longest) name:

    SELECT city
    FROM   (
      SELECT CITY,
             ROW_NUMBER() OVER ( ORDER BY LENGTH( CITY ) ASC,  CITY ) shortest_rn,
             ROW_NUMBER() OVER ( ORDER BY LENGTH( CITY ) DESC, CITY ) longest_rn
      FROM   station
    )
    WHERE shortest_rn = 1
    OR    longest_rn  = 1;
    

    If you want to return all the cities with the shortest (or longest) name then use DENSE_RANK instead of ROW_NUMBER:

    SELECT city
    FROM   (
      SELECT CITY,
             DENSE_RANK() OVER ( ORDER BY LENGTH( CITY ) ASC  ) shortest_rn,
             DENSE_RANK() OVER ( ORDER BY LENGTH( CITY ) DESC ) longest_rn
      FROM   station
    )
    WHERE shortest_rn = 1
    OR    longest_rn  = 1
    ORDER BY shortest_rn, city; -- Shortest first and order tied lengths alphabetically
    
    0 讨论(0)
  • 2021-01-31 00:23

    You query requires just a few tweaks. The fundamental problem is that you cannot use a in the subquery as you are doing:

    select a.city, a.city_length
    from (select city, char_length(city) city_length 
          from station 
         ) a
    where a.city_length = (select min(char_length(city)) from station) or
          a.city_length = (select max(char_length(city)) from station);
    

    That said, a simpler way to write the query is:

    select s.*
    from station s cross join
         (select min(char_length(city)) as mincl, max(char_length(city)) as maxcl
          from station
         ) ss
    where char_length(s.city) in (mincl, maxcl);
    
    0 讨论(0)
  • 2021-01-31 00:23

    For oracle :

    select min(city),length(city) from station where length(city) <= all(select 
    length(city) from station) group by length(city);
    
    select max(city),length(city) from station where length(city) >= all(select 
    length(city) from station) group by length(city);
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题