人工智能/模式识别/机器学习精华专题机器学习与数据挖掘

sqlzoo练习2

2020-01-13  本文已影响0人  皮皮大

主要练习的是select语句

表结构

image

练习

  1. show the name, continent and population of all countries.
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.
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.

select name, GDP/population  -- 计算人均GDP
from world 
where population >= 200000000;
image
  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.
select name, population/1000000   
from world
where continent = 'South America';
  1. Show the name and population for France, Germany, Italy
select name, population
from world
where name in ('France', 'Germany', 'Italy');
  1. Show the countries which have a name that includes the word 'United'
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.

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.

两个条件不能同时满足

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.

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

关于round函数的使用

image
  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的问题

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

SELECT name,  capital
FROM world
WHERE length(name) = length(capital);

12.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.

SELECT name, capital
FROM world
where LEFT(name,1) = LEFT(capital, 1)
and name <> capital;
  1. Find the country that has all the vowels and no spaces in its name.
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 '% %';  -- 不能带有空格
上一篇 下一篇

猜你喜欢

热点阅读