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
Related
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.
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
I have a table with about 3 million rows that consists of cities, the countries in which the city lies and some other data.
I want to retrieve for example limited rows where the name of the city starts with a certain string but list those that lie in a specific country first.
I tried to order them by country <> "us" which is very slow as the server has to sort all found rows before applying the limit.
Now I came up with the following statement:
(SELECT * FROM cities WHERE city LIKE "ab%" AND country = "at" LIMIT 5)
UNION
(SELECT * FROM cities WHERE city LIKE "ab%" LIMIT 5)
LIMIT 5
It is very fast with any prefix length but it looks kinda dirty.
Is there a more efficient way to do this?
select top 5 *
, case when country = 'ab' then 1
else 0 end as rnk
from cities
where lower(city) like 'ab%'
or country = 'ab'
order by rnk desc;
So i need to select the city with highest and lowest population in the same query using a Union statement... I have tried almost every answer on the site, google, mysql.com and mysql tutorials... nothing works so
this is my code
(select name, population from city order by population asc limit 1)
union
( select name, population from city order by population desc limit 1)
it is not working obviously, I dont want the answer, I just need to understand why it isnt working and what to fix please and thanks
I have a query where I want to get the 20 most popular locations from a table, and then order them alphabetically. I'm wondering if there's a cleaner or more efficient way to do this?
SELECT
city
FROM (
SELECT
city,
count(*) AS cnt
FROM locations
GROUP BY city
ORDER BY cnt DESC
LIMIT 20
) s ORDER BY city;
Slightly cleaner:
SELECT city FROM (
SELECT city FROM locations
GROUP BY city
ORDER BY count(*) DESC
LIMIT 20
) s ORDER BY city
You don't need to retrieve the count(*) if you're not going to use it.