shortest city query

query station

Query the two cities in STATION with the shortest and longest CITY names, 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.

select CITY, LEN(CITY) from STATION;

says wrong answer
please advise
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Are you answering to an exam?
You can do this in many ways. Here's one:
SELECT TOP 1 'Shortest', City, LEN(City) LengthCity
FROM station
ORDER BY LEN(city), City
SELECT TOP 1 'Longest', City, LEN(City) LengthCity
FROM station

Open in new window

slightwv (䄆 Netminder) Commented:
You asked this in both SQL Server and Oracle.

Which database do you want this for?

A lot of your other questions are Oracle so here is one way to get them both as a single row.  It should also work in SQL Server if you change the LENGTH function.

	max(case when min_rn=1 then city end) shortest_city,
	max(case when min_rn=1 then city_len end) shortest_length,
	max(case when max_rn=1 then city end) longest_city,
	max(case when max_rn=1 then city_len end) longest_length
	select city,length(city) city_len,
		row_number() over(order by length(city)) min_rn,
		row_number() over(order by length(city) desc) max_rn
	from station

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark WillsTopic AdvisorCommented:
and for the SQL server equivalent of slightwv's code - very similar
select distinct city, len_city
( select city, len(city) as len_city
       , row_number() over (order by len(city),city) as short_city
       , row_number() over (order by len(city) desc,city asc) as long_city
  from station ) s
where 1 in (short_city,long_city)
order by city

Open in new window

and slightwv is quite correct, if you change length(city) to len(city) then his version will also work for SQL Server.
Pavel GushchinProgrammerCommented:
select distinct 
       first_value(city) over(order by nvl(length(city),0), city) shortest,
       first_value(city) over(order by nvl(length(city),0) desc, city) longest
from station

Open in new window

Vitor MontalvãoMSSQL Senior EngineerCommented:
Pavel, what makes you think that only your comment is correct?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.