1. 程式人生 > >[SQL]LeetCode595. 大的國家 | Big Countries

[SQL]LeetCode595. 大的國家 | Big Countries

create mil country not etc code mys nbsp sel

SQL架構

1 Create table If Not Exists World (name varchar(255), continent varchar(255), area int, population int, gdp int)
2 Truncate table World
3 insert into World (name, continent, area, population, gdp) values (Afghanistan, Asia, 652230, 25500100, 20343000000)
4 insert into World (name, continent, area, population, gdp) values (
Albania, Europe, 28748, 2831741, 12960000000) 5 insert into World (name, continent, area, population, gdp) values (Algeria, Africa, 2381741, 37100000, 188681000000) 6 insert into World (name, continent, area, population, gdp) values (Andorra, Europe, 468, 78115, 3712000000) 7 insert into World (name, continent, area, population, gdp) values (
Angola, Africa, 1246700, 20609294, 100990000000)

There is a table World

+-----------------+------------+------------+--------------+---------------+
| name            | continent  | area       | population   | gdp           |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan     | Asia       | 652230     | 25500100     | 20343000      |
| Albania         | Europe     | 28748      | 2831741      | 12960000      |
| Algeria         | Africa     | 2381741    | 37100000     | 188681000     |
| Andorra         | Europe     | 468        | 78115        | 3712000       |
| Angola          | Africa     | 1246700    | 20609294     | 100990000     |
+-----------------+------------+------------+--------------+---------------+

A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.

Write a SQL solution to output big countries‘ name, population and area.

For example, according to the above table, we should output:

+--------------+-------------+--------------+
| name         | population  | area         |
+--------------+-------------+--------------+
| Afghanistan  | 25500100    | 652230       |
| Algeria      | 37100000    | 2381741      |
+--------------+-------------+--------------+

這裏有張 World

+-----------------+------------+------------+--------------+---------------+
| name            | continent  | area       | population   | gdp           |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan     | Asia       | 652230     | 25500100     | 20343000      |
| Albania         | Europe     | 28748      | 2831741      | 12960000      |
| Algeria         | Africa     | 2381741    | 37100000     | 188681000     |
| Andorra         | Europe     | 468        | 78115        | 3712000       |
| Angola          | Africa     | 1246700    | 20609294     | 100990000     |
+-----------------+------------+------------+--------------+---------------+

如果一個國家的面積超過300萬平方公裏,或者人口超過2500萬,那麽這個國家就是大國家。

編寫一個SQL查詢,輸出表中所有大國家的名稱、人口和面積。

例如,根據上表,我們應該輸出:

+--------------+-------------+--------------+
| name         | population  | area         |
+--------------+-------------+--------------+
| Afghanistan  | 25500100    | 652230       |
| Algeria      | 37100000    | 2381741      |
+--------------+-------------+--------------+

1613ms
1 # Write your MySQL query statement below
2 select 
3     a.name, a.population, a.area
4 from 
5     world as a
6 where 
7     a.area > 3000000 or a.population > 25000000

1627ms

1 # Write your MySQL query statement below
2 select w.name, w.population, w.area
3 from World w
4 where w.area > 3000000 or
5       w.population > 25000000;  

[SQL]LeetCode595. 大的國家 | Big Countries