Search code examples
sqlroundingmariadbconcatenation

Newbie in SQL: Using CONCAT and ROUND together


I'm trying to teach myself SQL, and have been using SQLZoo (http://sqlzoo.net) among other resources. (Which seems to be using MariaDB, according to the error message.)

One of their exercises asks to show the name and population of every country in Europe (from a table 'world' where country name, population, continent and some other stuff are all listed), but to show the populations as a percentage of Germany's population. They mention the CONCAT and ROUND functions right there, so I'm assuming they want a '%' sign and no decimal places.

I tried:

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

I figured that I would have to just sort of nest everything in one big, fat mess. My outermost layer in the original SELECT is to add the '%' at the end of the calculation, followed by shaving off the decimal places, followed by the actual calculation: calculating the population by its percentage of Germany's populations in decimal form by simply dividing the country's population by Germany's, multiplied by 100 so I can just shave off the decimal places, slap a percentage sign on it, and be done with it.

The problem is that this sends me back an error message. The error tells me to check the syntax at the end (FROM world WHERE continent = 'Europe').

Can someone tell me where this is going wrong?


Solution

  • You have unbalanced parentheses problem. Try this version of your query:

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