select min(length(city)), city
from station
group by min(length(city));
select max(length(city)), city
from station
group by min(length(city));
output = ERROR 1056 (42000) at line 1: Can't group on 'min(length(city))'
I am trying to get minimum and maximum char length with city name. I thought this algorithm will be okay.
Why ı can't group on min(length(city))
It doesn't make sense to group by an aggregate function. Aggregate functions are called after grouping, to calculate something for all the rows in the group (such as a sum total).
You can use a subquery to calculate an aggregate for the entire table. Then you can compare this with individual rows.
SELECT city, LENGTH(city) AS length
FROM station
WHERE LENGTH(city) = (SELECT MIN(length(city)) FROM station);
Another solution :
SELECT CITY , LENGTH(city) AS length
FROM STATION
WHERE LENGTH(CITY)
IN (
SELECT MAX(LENGTH(CITY))
FROM STATION
UNION
SELECT MIN(LENGTH(CITY))
FROM STATION )
Related
My goal is to find the name of the max length of the city. If there is more than one largest city, choose the one that comes first when ordered alphabetically.
I used it below but it's showing an error
select city, max(length(city))
from station
order by city asc
limit 1;
You need to find the city(ies) where the length equals the maximum length, one option would be
select City, length(City)
from station
where Length(city) = (select Max(Length(city)) from station)
order by city
limit 1;
You don't want to select the max, you just want to use it to order:
select city
from station
order by length(city) desc, city asc
limit 1;
Use a sub-select instead:
SELECT city, MAX(len_city)
FROM (SELECT city, LENGTH(city) AS len_city
FROM station
ORDER BY LENGTH(city) DESC, city)
LIMIT 1;
i am having problem with the mysql statements... the answer is to get the name of city and length of city of min length from a table named station where ordered by alphabetically by city names i have tried some statements as described below.
SELECT City, LENGTH(City) FROM Station ORDER BY City WHERE LENGTH(City) = MIN(LENGTH(City));
I have also tried to use user defined varibales like below:
SELECT #min := MIN(LENGTH(City)) FROM Station ORDER BY City;
SELECT City, LENGTH(City) FROM Station ORDER BY City WHERE LENGTH(City) = #min;
Sample Output: Amo 3
it showing this error : check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE LENGTH(City) = #min'
But it's not working please help
I have got this solution. I can use complex sql statements to equalize the length of the city to the minimun length by using the sql statements below:
SELECT City, LENGTH(City) FROM Station WHERE LENGTH(City) = (SELECT MIN(LENGTH(City)) FROM Station) ORDER BY City LIMIT 1;
In task below
Hackerrank
i did that query
SELECT CITY ,length(CITY ) from STATION where length(CITY )in(
select max(length(CITY ))from STATION union select min(length(CITY ))from STATION )Group by length(CITY )
order by length(CITY ) desc,CITY ;
i recieved Error
also i make another query like that
Select max(length(CITY)),CITY from STATION Group by length(CITY) Order by length(CITY)Desc;
and recieved that error
Error
The question phrases as:
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.
You don't really need aggregation to achieve this task.
I think this would be simpler expressed with union all and two row-limiting queries (one that sorts cities by ascending length, the other by descending length):
(
select city, char_length(city) cnt_chars
from station
order by char_length(city), city limit 1
) union all (
select city, char_length(city)
from station
order by char_length(city) desc, city limit 1
)
If there is only one city in the table (or if all cities have the same length), then union might be more appropriate than union all (so the same city does not appear twice in the resultset).
Alternatively, if you are running MySQL 8.0, you can use window functions:
select city, cnt_chars
from (
select s.*,
row_number() over(order by char_length(city), city) rn_asc,
row_number() over(order by char_length(city) desc, city) rn_desc
from station s
) s
where 1 in (rn_asc, rn_desc)
Side note: char_length() is safer than length(). The former counts characters, while the latter counts bytes (if your strings have multi-byte characters, this is not what you want).
You are trying to select CITY name and length(CITY) but grouping only by length(CITY). In case when two or more cities have the some value for length(CITY) mysql does not know which of the CITY should be selected.
My code is this but gives error, don't know why. Please help!
select city,
min(length(city))
from station
group by length(city)=min(length(city))
order by city asc;
If you just want the city with the shortest name, you can simply order by and limit:
select city, char_length(city) city_length
from station
order by city_length
limit 1
This returns just one row. On the other hand, if you want to allow bottom ties, then you can filter with a subquery, like so:
select city, char_length(city) city_length
from station
where char_length(city) = (select min(char_length(city)) from station)
I'm wondering how one would sum the results from a query?
I want to know how many people live in total in the three biggest cities in Norway. I'm using mysql, the world.sql sample database in mysql workbench.
This is the closest I've gotten
SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3
There's a few problems here namely this gives me three results instead of one, and while using LIMIT which actually limits how many results it gives, not how many it uses.
Any ideas?
You would use a subquery:
SELECT SUM(population)
FROM (SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3
) cp
simply sum the result:
select sum(population) from (SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3) t1
select sum(population) from (SELECT population FROM city WHERE
CountryCode = 'NOR' ORDER BY population DESC LIMIT 3) temp
Read on subqueries.
Make your current query a subquery and get sum from your subquery.
SELECT SUM(population) FROM (
SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3) p
You query will now act as a virtual table, from which you can you can write a select query to get the sum