SQL ZOO練習
來自專欄猴子聊數據分析
1. SELECT basics
1.show the population of Germany
答案:
SELECT population
FROM world
WHERE name = Germany;
2.Show the name and the population for Sweden, Norway and Denmark.
答案:
SELECT name, population
FROM world
WHERE name IN (Sweden, Norway, Denmark);
3.show the country and the area for countries with an area between 200,000 and 250,000
答案:
SELECT name, area
FROM world
WHERE area BETWEEN 200000 AND 250000;
2.1 SELECT from WORLD Tutorial
1. Observe the result of running this SQL command to show the name, continent and population of all countries.
答案:
SELECT name, continent, population
FROM world;
2.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;
3.Give the name and the per capita GDP for those countries with a population of at least 200 million.
答案:
SELECT name,gdp/population
FROM world
WHERE population > 200000000;
4.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;
5.Show the name and population for France, Germany, Italy
答案:
SELECT name,population
FROM world
WHERE name IN (France, Germany, Italy);
6. Show the countries which have a name that includes the word United
答案:
SELECT name
FROM world
WHERE name Like %United%;
7. 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;
8.Exclusive OR (XOR). Show the countries that are big by area or big by population but not both. Show name, population and area.
答案:
SELECT name,population,area
FROM world
WHERE (area > 3000000 AND population <= 250000000)
OR (area <= 3000000 AND population > 250000000);
9. 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) AS gdp
FROM world
WHERE continent = South America;
10.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.
答案:
SELECT name,ROUND(gdp/population,-3) AS gdp
FROM world
WHERE gdp > 1000000000000;
11. Show the name and capital where the name and the capital have the same number of characters.
答案:
SELECT name,capital
FROM world
WHERE length(name)=length(capital);
12. Show the name and the capital where the first letters of each match. Dont include countries where the name and the capital are the same word.
答案:
SELECT name,capital
FROM world
WHERE LEFT(name,1) = LEFT(capital,1)
AND name<>capital;
13. 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 % %;
3.1 SELECT from nobel
1.Change the query shown so that it displays Nobel prizes for 1950.
答案:
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1950;
2. Show who won the 1962 prize for Literature.
答案:
SELECT winner
FROM nobel
WHERE yr = 1962
AND subject = Literature;
3. Show the year and subject that won Albert Einstein his prize.
答案:
SELECT yr,subject
FROM nobel
WHERE winner=Albert Einstein;
4. Give the name of the Peace winners since the year 2000, including 2000.
答案:
SELECT winner
FROM nobel
WHERE subject=Peace
AND yr >= 2000;
5. Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive.
答案:
SELECT yr, subject, winner
FROM nobel
WHERE (yr >=1980 AND yr <=1989) AND subject = Literature;
6. Show all details of the presidential winners:
答案:
SELECT *
FROM nobel
WHERE winner IN (Theodore Roosevelt,Woodrow Wilson,Jimmy Carter,Barack Obama);
7. Show the winners with first name John
答案:
SELECT winner
FROM nobel
WHERE winner LIKE John%;
8. Show the year, subject, and name of Physics winners for 1980 together with the Chemistry winners for 1984.
答案:
SELECT yr, subject, winner
FROM nobel
WHERE (yr = 1980 AND subject = Physics)
OR (yr = 1984 AND subject = Chemistry);
9. Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine
答案:
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1980 AND subject NOT IN ( Chemistry , Medicine);
10. Show year, subject, and name of people who won a Medicine prize in an early year (before 1910, not including 1910) together with winners of a Literature prize in a later year (after 2004, including 2004)
答案:
SELECT yr, subject, winner
FROM nobel
WHERE (yr < 1910 AND subject = Medicine)
OR (yr >= 2004 AND subject = Literature);
11. Find all details of the prize won by PETER GRüNBERG
答案:
SELECT *
FROM nobel
WHERE winner = PETER GRüNBERG;
12. Find all details of the prize won by EUGENE ONEILL
答案:
SELECT *
FROM nobel
WHERE winner = Eugene ONeill;
13. List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
答案:
SELECT winner, yr, subject
FROM nobel
WHERE winner LIKE sir%
ORDER BY yr DESC, winner;
14. Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
答案:
SELECT winner, subject
FROM nobel
WHERE yr=1984
ORDER BY
CASE WHEN subject IN (Physics,Chemistry) THEN 1 ELSE 0 END,subject,winner;
4.1 SELECT within SELECT Tutorial
1.List each country name where the population is larger than that of Russia.
答案:
SELECT name FROM world
WHERE population >
(SELECT population FROM world
WHERE name=Russia);
2. Show the countries in Europe with a per capita GDP greater than United Kingdom.
答案:
SELECT name FROM world
WHERE continent = Europe AND gdp/population >
(SELECT gdp/population FROM world
WHERE name=United Kingdom);
3. List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.
答案:
SELECT name,continent
FROM world
WHERE continent IN
(SELECT continent
FROM world
WHERE name in (Argentina,Australia))
ORDER BY name;
4. Which country has a population that is more than Canada but less than Poland? Show the name and the population.
答案:
SELECT name,population
FROM world
WHERE population > (SELECT population FROM world WHERE name = Canada)
AND population < (SELECT population FROM world WHERE name = Poland);
5. Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.
答案:
SELECT name,
CONCAT(ROUND(population*100/(SELECT population FROM world WHERE name=Germany)), %) AS population
FROM world
WHERE continent = Europe;
6. Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)
答案:
SELECT name FROM world
WHERE gdp > ALL
(SELECT gdp FROM world
WHERE continent = Europe AND gdp > 0);
7. Find the largest country (by area) in each continent, show the continent, the name and the area:
答案:
SELECT continent, name, area FROM world x
WHERE area >= ALL
(SELECT area FROM world y
WHERE y.continent=x.continent
AND area>0);
8. List each continent and the name of the country that comes first alphabetically.
答案:
SELECT continent, name FROM world x
WHERE name <= ALL
(SELECT name FROM world y
WHERE y.continent=x.continent)
ORDER BY continent;
9. Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show name, continent and population.
答案:
SELECT name, continent, population FROM world x
WHERE 25000000 >= ALL
(SELECT population FROM world y
WHERE y.continent=x.continent);
10. Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.
答案:
SELECT name, continent FROM world x
WHERE population > ALL
(SELECT 3*population FROM world y
WHERE y.continent=x.continent AND x.name <> y.name);
5.1 SUM and COUNT
1. Show the total population of the world.
答案:
SELECT SUM(population)
FROM world;
2. List all the continents - just once each.
答案:
SELECT DISTINCT continent
FROM world;
3. Give the total GDP of Africa
答案:
SELECT SUM(gdp)
FROM world
WHERE continent = Africa;
4. How many countries have an area of at least 1000000
答案:
SELECT COUNT(area)
FROM world
WHERE area > 1000000;
5. What is the total population of (Estonia, Latvia, Lithuania)
答案:
SELECT SUM(population)
FROM world
WHERE name in (Estonia, Latvia, Lithuania);
6. For each continent show the continent and number of countries.
答案:
SELECT continent,COUNT(continent)
FROM world
GROUP BY continent;
7. For each continent show the continent and number of countries with populations of at least 10 million.
答案:
SELECT continent,COUNT(continent)
FROM world
WHERE population > 10000000
GROUP BY continent;
8. List the continents that have a total population of at least 100 million.
答案:
SELECT continent
FROM world
GROUP BY continent
HAVING SUM(population) > 100000000;
推薦閱讀:
※Python固化周期性的sql請求
※SQL常見面試題
※用好CASE,輕鬆表達複雜條件
※NoSQL還是SQL?這一篇講清楚
※學習SQL【10】-SQL高級處理
TAG:SQL |