I do not think that we need to use the Min and Max functions and Group by is also not required.
We can achieve this using the code below:
select top 1 City, LEN(City) City_Length from STATION order by City_Length ASC,City ASC select top 1 CITY, LEN(city) City_Length from station order by City_Length desc, City ASC
but in this case it will display the output in 2 table , and if we want to combine into one table, we can use Union or Union ALL. Below is the SQL query for the same
select * from ( select top 1 City, LEN(City) City_Length from STATION order by City_Length ASC,City ASC) TblMin UNION select * from ( select top 1 CITY, LEN(city) City_Length from STATION order by City_Length desc, City ASC) TblMax
here I insert the select expression inside the subquery, because when we use the order by clause, we cannot use Union or Union ALL directly, so I wrote it inside the subquery.
source share