8

My Question is:

Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany.

Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

My answer:

SELECT name,CONCAT(ROUND(population/80000000,-2),'%')
FROM world
WHERE population = (SELECT population
                    FROM world
                      WHERE continent='Europe')

What I am doing wrong? Thanks.

user3027665
  • 97
  • 1
  • 1
  • 8

8 Answers8

8

The question was incomplete and was taken from here

This is the answer

SELECT 
  name, 
  CONCAT(ROUND((population*100)/(SELECT population 
                                 FROM world WHERE name='Germany'), 0), '%')
FROM world
WHERE population IN (SELECT population
                     FROM world
                     WHERE continent='Europe')

I was wondering about sub-query as from OP'S question it wasn't clear (at least to me). The reason is that "world" table (as the name suggest, I have to admit) contains all world country whereas we're interested only into european one. Moreover, the population of Germany has to be retrieved from DB because it's not extacly 80.000.000; if you use that number you receive back 101% as Germany population.

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
3

When using sql server in SQL Zoo, then don't use CONCAT:

I think SQL Zoo uses a version of SQL Server that doesn't support CONCAT and furthermore it looks like you have to do a CAST. Instead concatenate with the use of '+'. Also see this post.

I figure the script should be something like beneath (though I haven't got it to my desired stated, because of the fact I want to result to look like 3%;0%;4%;etc. instead of 3.000000000000000%;0.000000000000000%;4.000000000000000%;etc.. And I start a new topic for that one here).

SELECT name, CAST(ROUND(population*100/(SELECT population FROM world WHERE name='Germany'), 0) as varchar(20)) +'%' FROM world WHERE population IN (SELECT population FROM world WHERE continent='Europe')

Community
  • 1
  • 1
cybork
  • 569
  • 2
  • 5
  • 24
1
    select name, CONCAT(ROUND((population/(select population from world where name = "Germany"))*100),"%") 
    from world
    where continent= "Europe"
pratik dhamanekar
  • 113
  • 1
  • 2
  • 9
  • 1
    Thanks for answering! Code-only answers [are discouraged](http://meta.stackexchange.com/a/148274) on Stack Overflow, because a code dump with no context doesn't explain how or why the solution will work, making it impossible for the original poster (or any future readers) to understand the logic behind it. Please, edit your question and include an explanation of your code so that others can benefit from your answer! – AHiggins Jul 23 '15 at 14:56
1
SELECT name, CONCAT(ROUND(population/(SELECT population FROM world WHERE name = 'Germany')*100,0), '%')
FROM world
WHERE continent = 'Europe'
Floern
  • 33,559
  • 24
  • 104
  • 119
Miller
  • 11
  • 1
1

As of this writing (Aug 16th, 2022), the accepted answer is a percentage without trailing 0s.

Using CONCAT and CAST solves this.

SELECT name, CONCAT(CAST(100*ROUND((population / (SELECT population FROM world WHERE name ='Germany')), 2) AS INT), '%')
FROM world
WHERE continent = 'Europe';
BoomBoxBoy
  • 1,770
  • 1
  • 5
  • 23
0

sub query should be return multiple data so you can use in function like this

  SELECT name,CONCAT(ROUND(population/80000000,-2),'%')
    FROM world
    WHERE population IN (SELECT population
                        FROM world
                          WHERE continent='Europe')
Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
0

Below added code snippet

select name, concat (round(population/(select population from world where 
name='germany')*100,0), '%') from world where continent='Europe'
Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
0

The following worked for me:


SELECT name, concat(format(ROUND(population / (SELECT population FROM world WHERE name = 'Germany') * 100, 5), '0'), '%') AS percentage
FROM world
WHERE continent = 'Europe';