Merge as Single Query - mysql

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

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 QUERY - Max and length

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;

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)

selecting the largest n values and smallest n values simulataneously from a table mySQL

In mySQL, I have a database of country names and population I want to return the 5 most populated and 5 least populated countries. I understanding how to order the results by population and how to limit the results to the top 'n' values or bottom 'n' values of a column, but cannot determine how to return both the top and bottom at the by using one query.
HERE are the two codes separately...
SELECT Name, population FROM country
ORDER BY GNP DESC LIMIT 5;
SELECT Name, GNP FROM country
ORDER GNP LIMIT 5;
How can I combine these codes into one output??
THanks
You need UNION ALL:
(
SELECT Name, population FROM country
ORDER BY population DESC LIMIT 5
)
UNION ALL
(
SELECT Name, population FROM country
ORDER BY population ASC LIMIT 5
)
ORDER by population DESC;
See a simplified demo.