sqlzoo 4.SELECT within SELECT answer.

If you have any questions, please comment or chat in private. Please chat with the blogger privately, thank you.

Link to the original title: https://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial

Other question solution links: https://blog.csdn.net/aiqiyizz/article/details/109057732

The solution corresponds to the English version.

4 SELECT within SELECT

4.1 Bigger than Russia

SELECT name FROM world
  WHERE population >
     (SELECT population FROM world
      WHERE name='Russia')

4.2 Richer than UK

SELECT name
FROM world
WHERE continent = 'Europe'
AND gdp/population > (
  SELECT gdp/population 
  FROM world
  WHERE name = 'United Kingdom'
)

4.3 Neighbours of Argentina and Australia

Find the continent ⇒ \Rightarrow⇒Find the country corresponding to the continent

SELECT name, continent
FROM world
WHERE continent IN (
  SELECT continent
  FROM world
  WHERE name IN ('Argentina', 'Australia')
)
ORDER BY name

4.4 Between Canada and Poland

SELECT name, population
FROM world
WHERE population > (
  SELECT population
  FROM world
  WHERE name = 'Canada'
)
AND
population < (
  SELECT population
  FROM world
  WHERE name = 'Poland'
)

The following is also a positive solution (funny)

SELECT name, population
FROM world
WHERE 0=1

4.5 Percentages of Germany

Find the population of Germany ⇒ \Rightarrow⇒Use various functions to spell out the answer

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

4.6 Bigger than every country in Europe

SELECT name
FROM world
WHERE gdp > (
  SELECT max(gdp)
  FROM world
  WHERE continent = 'Europe'
  GROUP BY continent
)

4.7 Largest in each continent

SELECT continent, name, area FROM world x
  WHERE area>= ALL
    (SELECT area FROM world y
        WHERE y.continent=x.continent
          AND area>0)

4.8 First country of each continent (alphabetically)

SELECT continent, min(name)
FROM world
GROUP BY continent

4.9 Difficult Questions That Utilize Techniques Not Covered In Prior Sections

Find the maximum value for each continent ⇒ \Rightarrow⇒Find the continent with the maximumvalue⇒ \Rightarrow⇒Find the countries corresponding to these continents

Note that it needs to be added AS temp1, otherwise it will appear Every derived table must have its own alias.

SELECT name, continent, population
FROM world
WHERE continent IN (
  SELECT continent
  FROM (
    SELECT continent, max(population) AS Mpop
    FROM world
    GROUP BY continent
  ) AS temp1
  WHERE Mpop <= 25000000
)

4.10

Forced to use ALL, but it’s not difficult

SELECT name, continent
FROM world x
WHERE population >= ALL (
  SELECT 3 * population
  FROM world y
  WHERE x.continent = y.continent
  AND y.name <> x.name
)

Guess you like

Origin blog.csdn.net/aiqiyizz/article/details/109082291