Fork me on GitHub

sqlzoo练习2

主要练习的是select语句

  • round函数
  • left函数
  • 通配符的使用

表结构

练习

  1. show the name, continent and population of all countries.
1
SELECT name, continent, population FROM world
  1. Show the name for the countries that have a population of at least 200 million. 200 million is 200000000, there are eight zeros.
  • Million:6个0
  • Billion:8个0
1
2
SELECT name FROM world
WHERE population > 200000000;
  1. 关于人均GDP

Give the name and the per capita GDP for those countries with a population of at least 200 million.

1
2
3
select name, GDP/population  -- 计算人均GDP
from world
where population >= 200000000;

  1. Show the name and population in millions for the countries of the continent ‘South America’. Divide the population by 1000000 to get population in millions.
1
2
3
select name, population/1000000
from world
where continent = 'South America';
  1. Show the name and population for France, Germany, Italy
1
2
3
select name, population
from world
where name in ('France', 'Germany', 'Italy');
  1. Show the countries which have a name that includes the word ‘United’
1
2
3
select name
from world
where name like '%United%';
  1. Two ways to be big: A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million.

    Show the countries that are big by area or big by population. Show name, population and area.

1
2
3
select name, population, area
from world
where area > 3000000 or population > 250000000;
  1. Exclusive OR (XOR). Show the countries that are big by area (more than 3 million) or big by population (more than 250 million) but not both. Show name, population and area.

两个条件不能同时满足

1
2
3
select name, population, area
from world
where area > 3000000 xor population > 250000000;
  1. Show the name and population in millions and the GDP in billions for the countries of the continent ‘South America’. Use the ROUND function to show the values to two decimal places.

    For South America show population in millions and GDP in billions both to 2 decimal places.

1
2
3
select name, round(population / 1000000, 2), round(gdp / 1000000000, 2)
from world
where continent = 'South America';

关于round函数的使用

  • 后面的第二个参数如果是正数,表示的保留几位有效小数
  • 如果是负数,表示的是取整数,保留几个0
  1. Show the name and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.

    Show per-capita GDP for the trillion dollar countries to the nearest $1000.

笔记:关于人均GDP的问题

1
2
3
select name, round(gdp/population, -3)
from world
where gdp >= 1000000000000;
  1. Show the name and capital where the name and the capital have the same number of characters.
    • You can use the LENGTH function to find the number of characters in a string
1
2
3
SELECT name,  capital
FROM world
WHERE length(name) = length(capital);
  1. Show the name and the capital where the first letters of each match. Don’t include countries where the name and the capital are the same word.
    • You can use the function LEFT to isolate the first character.
    • You can use <> as the NOT EQUALS operator.
1
2
3
4
SELECT name, capital
FROM world
where LEFT(name,1) = LEFT(capital, 1)
and name <> capital;
  • 判断左边的几个字母相同left函数
  • 使用不等于符号<>
  1. Find the country that has all the vowels and no spaces in its name.
    • You can use the phrase name NOT LIKE '%a%' to exclude characters from your results.
    • The query shown misses countries like Bahamas and Belarus because they contain at least one ‘a’
1
2
3
4
5
6
7
8
SELECT name
FROM world
WHERE name LIKE '%a%'
AND name LIKE '%e%'
AND name LIKE '%i%'
AND name LIKE '%o%'
AND name LIKE '%u%'
AND name NOT LIKE '% %'; -- 不能带有空格
  • 国家名字中带有全部的元音字母
  • 名字中间不能有空格

本文标题:sqlzoo练习2

发布时间:2020年01月13日 - 16:01

原始链接:http://www.renpeter.cn/2020/01/13/sqlzoo%E7%BB%83%E4%B9%A02.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea