SQLZOO-SELECT FROM WORLD
Small catalog
- Subquery returns more than 1 row error resolution;
- How to express a percentage with a percent sign;
- Do "comparison operation" with NULL value, resulting in a null output value;
- Two methods to find the maximum value (max, min);
text
- There are multiple sub-queries that meet the conditions, and a sequence is returned, and a continent variable cannot be compared with a bunch of return values at the same time, resulting in an error
error: Subquery returns more than 1 row
Solution: add any keyword before the subquery
select name, continent
from world
where continent = any(
select distinct continent from world
where name = 'Argentina' or name = 'Australia'
)
order by name
- Representation of an integer percentage with a percent sign
Use round() to make a rounding truncation, and then use concat() to splice% signs;
the precision of the percentage can be adjusted, just adjust the B parameter of round(A, B).
select name,
concat(round(100*population/(select population
from world
where name = 'Germany'))
, '%') as percentage
from world
where continent = 'Europe'
- When the number to the right of the comparison sign is NULL, there will be no output value.
Example:
This code does not exclude countries with gdp=NULL. The all() function will cause gdp to be compared with NULL, causing the output value to be lost;
select name
from world
where gdp > all(select gdp
from world
where continent = 'Europe' )
In addition to the restriction that gdp is a non-negative number, it is actually an operation to remove the NULL value, and the result can be output normally.
select name
from world
where gdp > all(select gdp
from world
where continent = 'Europe'
&& gdp>=0)
- When looking for the best value,
you can use >= to compare with yourself;
SELECT continent, name, area
FROM world x
WHERE area >= ALL(SELECT area
FROM world y
WHERE y.continent=x.continent
&& area>0)
You can also limit it in where not to compare with yourself;
SELECT continent, name, area
FROM world x
WHERE area > ALL(SELECT area
FROM world y
WHERE y.continent=x.continent
&& area>0
&& x.name <> y.name)