Why can't I add group by clause into this query? [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I was filling this exercise from Sqlzoo.
Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)
the answer is
select name, continent
from world
where gdp >=all(select gdp
from world
where gdp>0 and continent='Europe') and continent!='Europe'
and results are
name
China
Japan
United States
but when I try to group and count them according to their continents it does not allow me to do it.
select count(name), continent
from world
where gdp >=all(select gdp
from world
where gdp>0 and continent='Europe') and continent!='Europe' and group by continent
it gives me a syntax error. it says "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'group by continent' at line 1"
Why does not it allow me do that?
6th exercise in that address https://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial

remove last and. group by is not condition
try it
select count(name), continent
from world
where gdp >=all(select gdp
from world
where gdp>0
and continent='Europe')
and continent!='Europe'
group by continent

Related

Display all countries that contain at least 2 words in the country name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
SELECT country_name
FROM Countries
WHERE country_name LIKE '%Word1%' AND country_name LIKE '%Word2%';
Display all countries that contain at least 2 words in the country name. It returns no results but in the system there's United states of America and United kingdom
If you want at least two words, then there is at least one space. So, if I understand correctly, you want:
where country_name like '% %'

SQL Query for desired output [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a table which contains countries:
Countries
============
Australia
South Africa
Bangladesh
New zeland
Sri Lanka
England
...
Desired out put is
Country
===========
India
Sri Lanka
Followed by other countries in `Asc` or `Desc`
You can use CASE:
SqlFiddleDemo
SELECT *
FROM Countries
ORDER BY
CASE Name
WHEN 'India' THEN 0
WHEN 'Sri Lanka' THEN 1
ELSE 10
END ASC,
Name ASC -- DESC

Sqlzoo SELECT within SELECT Tutorial #5

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.
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.
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')
select name, CONCAT(ROUND((population/(select population from world where name = "Germany"))*100),"%")
from world
where continent= "Europe"
SELECT name, CONCAT(ROUND(population/(SELECT population FROM world WHERE name = 'Germany')*100,0), '%')
FROM world
WHERE continent = 'Europe'
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';
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')
Below added code snippet
select name, concat (round(population/(select population from world where
name='germany')*100,0), '%') from world where continent='Europe'
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';

Find average GDP according to continent in SQL

I have 2 tables- Economics (land_code, gdp) and Continent (Land_code, Cont, Percentage). I need to create a query that calculates average GDP for Continent. In case the country is at the same time in several continents, we should also consider the percentage of GDP that belongs to continent. As I have understood if Egypt has GDP of 100, then 90 belongs to Africa and 10 to Asia, how can I implement this expression?
!!! ALREADY DONE)
Obviously,
select Economics.land_code, Economics.gdp * Continent.Percentage
from ...

update row value with another row value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
we have the below table
COUNTRY TOP_COUNTRY
-------------------
ST. HELENA OTHERS
BARBADOS OTHERS
UNITED STATES UNITED STATES
**RUSSIA OTHERS**
NETHERLANDS OTHERS
**GERMANY OTHERS**
ANGUILLA OTHERS
AUSTRALIA AUSTRALIA
CHINA CHINA
I would like to update TOP_COUNTRY row value for a few countries with the names as shown in COUNTRY column.
For eg:
Right now, we RUSSIA shown as 'OTHERS' in TOP_COUNTRY but i would like to update it to the 'RUSSIA'.
This needs to be done for a couple of values..
Can you please let me know how we can get this done..
You can do:
UPDATE tableName
SET TOP_COUNTRY = COUNTRY
WHERE <YourCLause>
In if you want a list of COUNTRY to be updated you can do:
WHERE COUNTRY IN ("COUNTRY1","COUNTRY2",...);
update [YourTableName] set top_country = 'RUSSIA'
where COUNTRY='RUSSIA'