1

I have a table called country with a column called population. I need to get a table with only the highest and lowest population country. I have tried:

select max(population) from country;  

select min(population) from country;  

but I can't figure out how to combine these queries into only one query.

Best wishes!

0

2 Answers 2

2
select min(population), max(population) from country;

Will get you a result with two columns, the first holding the minimum population and the second the maximum population.

That only returns the populations though. If what you need is a query that will return the two countries, embedded queries would do :

select * from country where population=(select min(population) from country) or population=(select max(population) from country);
Sign up to request clarification or add additional context in comments.

3 Comments

When i try your second query i get the error "aggregate functions are not allowed in WHERE", any tips?
"Don't trust someone who hasn't wrote any SQL in some time" would be a good tip I guess :-/ The error message means you can't use functions that work on the whole table in the where clause, which I had forgotten ; let me check how to go around that
I've fixed my query, but I'm pretty sure there are better ways to do so.
0

Try this:

select country,min(population) from country
group by 1
Union
select country,max(population) from country
group by 1
order by 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.