How to join two select statements in one table? - mysql

I'm new to mysql and I have two correctly working select statements with longest and shortest names. I can't really understand how can I join them in one statement. Can someone help, please.
Here are the queries:
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength
FROM country
ORDER BY CHAR_LENGTH(name) DESC, name
LIMIT 1;
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength
FROM country
ORDER BY CHAR_LENGTH(name), name
LIMIT 1;
The result I'm looking for is:
Country NameLength
----------------------------------------------------------
South Georgia and the South Sandwich Islands 44
Chad 4
Cuba 4

You use UNION:
SELECT Country, NameLength FROM (
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength FROM country LIMIT 1;
UNION
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength FROM country LIMIT 1; )a
ORDER BY NameLength DESC, Country

Related

How do I return only one row with MAX() aggregate?

SELECT Name, MAX(Population) as Population
FROM County
GROUP BY Name;
Question: This just returned 16 rows... I want to return only the ONE county which has the highest population, showing 1 row with with the name of that county and its population,, not the highest population for every county. How do I fix this?
Thanks in advance.
Order and take first
SELECT Name,
Population
FROM County
ORDER BY Population DESC LIMIT 1
with data (Population, Name) as(
Select '12345' ,'c1' union all
Select '1234500001' ,'c2' union all
Select '1234500002' ,'c3' union all
Select '12346' ,'c4' union all
Select '1234600001' ,'c4' union all
Select '1234600002' ,'c4' )
SELECT Name, Population
FROM data
where Population = (SELECT MAX(Population) FROM data)
;

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 sum top results?

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

How to do this in a mysql query

I have got a mysql table containing data about companies: id, company name, city, address, telephone.
I'm trying to write a query for getting list of cities where there are more then 10 companies within.
Is it possible to achieve that?
Try
select city, count(*) as nbPerCity from tableName group by city having nbPerCity > 10;
select city from Table group by city having count(company_name) > 10 ;
or
select s.city from
(select city,count(company_name) as counted from Table group by city) as s
where s.counted > 10

Order by COUNT per value

I have a table which stores IDs and the city where the store is located.
I want to list all the stores starting with the stores that are in the city where there are the most stores.
TABLE
ID CITY
1 NYC
2 BOS
3 BOS
4 NYC
5 NYC
The output I want is the following since I have the most stores in NYC, I want all the NYC location to be listed first.
1 NYC
4 NYC
5 NYC
2 BOS
3 BOS
SELECT count(City), City
FROM table
GROUP BY City
ORDER BY count(City);
OR
SELECT count(City) as count, City
FROM table
GROUP BY City
ORDER BY count;
Ahh, sorry, I was misinterpreting your question. I believe Peter Langs answer was the correct one.
This one calculates the count in a separate query, joins it and orders by that count (SQL-Fiddle):
SELECT c.id, c.city
FROM cities c
JOIN ( SELECT city, COUNT(*) AS cnt
FROM cities
GROUP BY city
) c2 ON ( c2.city = c.city )
ORDER BY c2.cnt DESC;
This solution is not a very optimal one so if your table is very large it will take some time to execute but it does what you are asking.
select c.city, c.id,
(select count(*) as cnt from city c2
where c2.city = c.city) as order_col
from city c
order by order_col desc
That is, for each city that you come across you are counting the number of times that that city occurs in the database.
Disclaimer: This gives what you are asking for but I would not recommend it for production environments where the number of rows will grow too large.
SELECT `FirstAddressLine4`, count(*) AS `Count`
FROM `leads`
WHERE `Status`='Yes'
AND `broker_id`='0'
GROUPBY `FirstAddressLine4`
ORDERBY `Count` DESC
LIMIT 0, 8