SQL QUERY - Max and length - mysql

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;

Related

SQL Group By min(len())

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 )

SQL Subquery Question in MySQL. Subquery in WHERE, SELECT or FROM?

I am a beginner at SQL and I am trying to solve this question from HackerRank using Subquery. Could you tell me what I did wrong?
Problem
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.
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 ABC, DEF, PQRS, and WXY, with lengths and . The longest name is PQRS, but there are options for shortest named city. Choose ABC, because it comes first alphabetically.
My Solution
SELECT
CITY,
LENGTH(SELECT MAX(CITY)
FROM STATION) AS Length
FROM STATION
ROWNUM 1;
SELECT
CITY, LENGTH(SELECT MIN(CITY)
FROM STATION) AS Length
FROM STATION
ROWNUM 1;
You can UNION the first and the last
CREATE tABLE A (city varchar(10))
INSERT INTO A VALUES ('DEF'),('ABC'), ('PQRS'), ('WXY')
(SELECT city
, LENGTH(city)
FROM A
ORDER BY LENGTH(city) ASC,city LIMIT 1)
UNION ALL
(SELECT city
, LENGTH(city)
FROM A
ORDER BY LENGTH(city) DESC,city LIMIT 1)
city | LENGTH(city)
:--- | -----------:
ABC | 3
PQRS | 4
db<>fiddle here
My solution without using UNION:
select city,length(city) from station order by length(city),city limit 1;
select city,length(city) from station order by length(city) desc ,city limit 1;

How to query for city name as well as length of the smallest city name from the same table

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)

ORDER BY LENGTH(CITY) ASC LIMIT 1; IN MYSQL DOESN'T SHOW FIRST ITEM

I'm trying to show the smallest city in a list and its length. It also has to be the first one alphabetically in case there is more then 1 with the same length, but whenever I use the code below, I receive the second one in the list.
SELECT CITY, LENGTH(CITY) FROM STATION
ORDER BY LENGTH(CITY) ASC LIMIT 1;
When I use
ORDER BY LENGTH(CITY) ASC LIMIT 1 OFFSET 1;
instead of just
ORDER BY LENGTH(CITY) ASC LIMIT 1;
I get the correct answser. Am I doing something wrong?
ORDER BY LENGTH(CITY) only orders by the length of the city name. Cities with the same length are in an arbitrary ordering.
If you want to then order by the city alphabetically, then use:
ORDER BY LENGTH(CITY), CITY

Merge as Single Query

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,CHAR_LENGTH(CITY) AS CHARLENGTH
FROM STATION
ORDER BY CHARLENGTH ASC
LIMIT 1;
SELECT CITY,CHAR_LENGTH(CITY) AS CHARLENGTH
FROM STATION
ORDER BY CHARLENGTH DESC
LIMIT 1;
Something like this?
##DROP TABLE STATION;
CREATE TABLE STATION (CITY VARCHAR(100));
INSERT INTO STATION VALUES ('aaaaaaa');
INSERT INTO STATION VALUES ('bbbbbbb');
INSERT INTO STATION VALUES ('cccc');
INSERT INTO STATION VALUES ('dd');
SELECT *
FROM
(SELECT CITY,CHAR_LENGTH(CITY) AS CHARLENGTH FROM STATION ORDER BY CHARLENGTH ASC, CITY LIMIT 1) A
UNION ALL
SELECT * FROM
(SELECT CITY,CHAR_LENGTH(CITY) AS CHARLENGTH FROM STATION ORDER BY CHARLENGTH DESC, CITY LIMIT 1) B
Output:
CITY CHARLENGTH
1 dd 2
2 aaaaaaa 7