简体   繁体   中英

How to select rows that have a maximum value for a column

I'm doing this challenge on Hackerrank:

https://www.hackerrank.com/challenges/weather-observation-station-5

I'm a beginner on SQL and I'm trying to query all the rows that have a maximum value for a column, maximum that I can only obtain via MAX() . So I'm trying this:

SELECT CITY, LENGTH(CITY) AS citylength
FROM STATION
WHERE LENGTH(CITY) = (SELECT MIN(CITY) FROM STATION)

and I get errors.

I've looked up on google about sub-queries but I'm not accustomed enough to know exactly how it works, so I need your help guys.Thanks.

So to sum up, I need a query that can get the rows on a table that has a maximum value obtained via MAX() clause.

You are requested to find two different results:

  • The city with maximum length (and the first in the alphabet in case of a tie)
  • The city with minimum length (and the first in the alphabet in case of a tie)

This means two different queries, which you glue together with UNION ALL.

(
  select concat(city, ' ', length(city))
  from station 
  order by length(city), city limit 1
)
union all
(
  select concat(city, ' ', length(city))
  from station 
  order by length(city) desc, city limit 1
);

As Strawberry pointed out: You need the parentheses in order to place two ORDER BY clauses, one per query part. (Otherwise you can only place one ORDER BY clause at the end for the whole query.)

In your query you are comparing LENGTH(CITY) , ie an integer holding the name's length and MIN(CITY) , ie the city name itself, which cannot work of course. You would have to compare with MIN(LENGTH(CITY)) . Then do the same for the maximum and then use UNION ALL. This doesn't solve the problem with ties, however, which the LIMIT query does.

This works without sub-queries

SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY 2,1
LIMIT 1

This works :

SELECT CITY,LENGTH(CITY) 
FROM STATION  
WHERE LENGTH(CITY) = (SELECT MIN(LENGTH(CITY)) M FROM STATION);

This works correctly:

select city, length(city) 
from station 
where length(city) = (select min(length(city)) as m from station)
order by city ASC
limit 1;
(SELECT CONCAT(city,' ',LENGTH(city)) city, 'shortest' category FROM stations ORDER BY LENGTH(city),city LIMIT 1)
UNION ALL
(SELECT CONCAT(city,' ',LENGTH(city)), 'longest' FROM stations ORDER BY LENGTH(city) DESC,city LIMIT 1);
select TOP 1 concat(city, ' ' , len(city)) from station where len(city) = (select min(len(city)) from station) order by city asc;

select TOP 1 concat(city, ' ' , len(city)) from station where len(city) = (select max(len(city)) from station) order by city desc;
SELECT * FROM
    (SELECT City, LENGTH(City) FROM STATION 
    ORDER BY LENGTH(City),city ASC) 
WHERE ROWNUM <= 1
UNION ALL
SELECT * FROM 
    (SELECT City, LENGTH(City) FROM STATION 
    ORDER BY LENGTH(City) DESC) 
WHERE ROWNUM <= 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM