HackerRank SQL Solution - Basic Select - Weather Observation Station 5

  • 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.
  • The STATION table is described as follows:
HackerRank SQL Solution - Basic Select - Weather Observation Station 5
  • where LAT_N is the northern latitude and LONG_W is the western longitude.
Sample Input:
  • For example, CITY has four entries: DEF, ABC, PQRS and WXY.
Sample Output:

ABC 3
PQRS 4
Explanation:
  • When ordered alphabetically, the CITY names are listed as DEF, ABC, PQRS, and WXY, with lengths 3, 3, 4, and 3. The longest name is PQRS, but there are 3 options for the shortest named city. Choose ABC, because it comes first alphabetically.
Note:
  • You can write two separate queries to get the desired output. It need not be a single query.
Solution:

/* Editor - MySQL */

select CITY, length(CITY) from STATION order by length(CITY), CITY asc limit 1;
select CITY, length(CITY) from STATION order by length(CITY) desc limit 1;
Note: The problem statement is given by hackerrank.com but the solution is generated by the Geek4Tutorial admin. We highly recommend you solve this on your own, however, you can refer to this in case of help. If there is any concern regarding this post or website, please contact us using the contact form. Thank you!

No comments:

Post a Comment

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...