36, SQLZOO to exercises SELECT from WORLD Tutorial

https://sqlzoo.net/
SQLZOO: the SELECT WORLD from the Tutorial
Here Insert Picture Description
name: Country name
continent: Africa copies
area: the area of
population: population
gdp: Gross Domestic Product

Country Profile
In this tutorial, we'll use the SELECT statement on World table query.

1, the result of precautions observed reading this table to run a simple SQL commands.

select name, continent, population from world

2. How to use WHERE to filter records. Display name of the country has at least 200 million people. 200 million is 200 million, eight zero.

Population world the WHERE name from the SELECT> = 200000000.


3. identify the name of the country has at least 200 million (200 million) of the population, and per capita gross domestic product.

name the SELECT, GDP / Population from the WHERE world Population> = 200000000.


4. show 'South America' country name South American continent for the number of units and millions of people. The population population divided by one million (1,000,000) was the number of units available in millions population.

name the SELECT, Population / 1000000 from the WHERE Continent is a world = 'South America'


5. displayed France, Germany, Italy (France, Germany, Italy) the name of the country and population.

name the SELECT, the WHERE Population from world name in ( 'France', 'Germany', 'Italy')


6. display the country that contain the word "United" as the name.

world name from the WHERE name the SELECT like '%% United'


7. become a great power in two ways: if it has more than 3 million square kilometers, or more than 250 million (250 million) more than the population.
Show Name, population and area powers.


name the SELECT, Population, Area from the WHERE world Area> or = 3000000 Population> = 250000000


8. United States, India and China (USA, India, China) is a big population, while the area of the big country. Excluding these countries.
Display national population or area of a big country, but not both at the same time. Display name of the country, population and area.


name SELECT, Population, from World WHERE Area (Area> = 3,000,000 and Population <= 250 million) or (Area <3,000,000 and Population> 250 million)


9. The 1,000,000 except that (six zeros) are millions. Divided by 1,000,000,000 (nine zero) is the billions of dollars. ROUND function using the displayed value to two decimal places. For South America are displayed in millions of people, billions two decimal GDP.

name the SELECT, round (Population / 1000000,2), round (GDP / 1000000000,2) from the WHERE Continent is a world = 'South America'


10. countries show that at least a one trillion yuan of gross domestic product (trillion, which is 12 zeros) per capita gross domestic product. This value is rounded to the nearest 1000.
Show per capita GDP one trillion yuan country, rounded to the nearest $ 1000.


select name, round (gdp / population , -3) from world where gdp> = 1000000000000


Statement Used Shown the CASE IS piont in to `substitute for Caribbean in North America The THIRD column.
The Show The name - Australasia But` substitute for Oceania - for Countries Beginning with N.
In the third column column illustrates an alternative to the case shown in North America rather than the Caribbean. Displays the name - but instead of using Australasia Oceania - countries beginning with N.

SELECT name, Case
When Continent = 'Oceania' the then 'Australasia'
the else Continent End
from World
WHERE name like 'N%'



12 is, the Show The name and The Continent - But `substitute Eurasia for Europe and Asia;` substitute America - for each Country in . North America or South America or Caribbean show countries beginning with a or B
displays the name and the mainland - but instead of using Eurasia Europe and Asia; with the Americas in place of each country in North America, South America or the Caribbean. A national or displayed at the beginning of the B

SELECT name, Case
When Continent in ( 'Europe', 'Asia') the then 'Eurasia'
When Continent in ( 'North America', 'South America', 'Caribbean') the then 'America'
the else Continent End
from World
WHERE name like 'A%' or name like 'B%'



13 is, of Put The Continents right ...
the continent straighten ...
Oceania becomes Australasia
Oceania Australia became Chau
countries in Eurasia and Turkey go to Europe / Asia
Eurasia and Turkey, countries go to Europe / Asia
Caribbean islands starting with 'B' go to North America, other Caribbean islands go to South America
beginning with "B" in the Caribbean Islands to North America, South America and other Caribbean islands go to
show the name, the original continent and the new continent of all countries.
the names of all the country, the continent and the original New World.

select name,continent,case
when continent=‘Oceania’ then ‘Australasia’
when continent in (‘Eurasia’,‘Turkey’) then ‘Europe/Asia’
when continent=‘Caribbean’ then case when name like ‘B%’ then ‘North America’ else ‘South America’ end
else continent end
from world
order by name ASC


In this exercise, the main contact with two new functions

1, ROUND function : rounding function. Returns expression are rounded to the specified length, the accuracy.
ROUND (specify a value, leave decimal places), while retaining a positive number indicates the number of decimal places rounded to develop; is negative, expressed rounded to the specified number of digits to the left of the decimal point.

Example:
the ROUND (838.234,1) returns: 838.2
the ROUND (835.578,2) returns: 835.58
the ROUND (867.43, -1) is returned: 870
the ROUND (933.643, -2) returns: 900
the ROUND (945.8, -3 ) returns: 1000

2, simple and Case Case function search function.
case when then else end: the values are determined by the write back the specified value, commonly used in sorting, grouping, Analysis
- Simple Case function
SELECT the CASE Sex
the WHEN '. 1' THEN 'M'
the WHEN '2' THEN 'F'
the ELSE 'other' the END
from table_a

--Case search function
select CASE WHEN sex = '1' THEN ' M'
the WHEN Sex = '2' THEN 'F'
the ELSE 'other' the END
from table_b

Published 38 original articles · won praise 5 · Views 6013

Guess you like

Origin blog.csdn.net/luluisntlulu/article/details/100581509