sqlzoo个人记录(1)

SELECT from WORLD Tutorial

world(name,continent,area,population,gdp)

1.Introduction

Read the notes about this table 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.Large Countries

How to use WHERE to filter records. 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.Per capita GDP

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.South America In millions

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.France, Germany, Italy

Show the name and population for France, Germany, Italy.

SELECT name, population FROM world
WHERE name IN ('France','Germany','Italy');

6.United

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

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.One or the other (but not both)

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;

9.Rounding

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 Americas 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';

10.Trillion dollar economies

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) FROM world
WHERE GDP >= 1000000000000;

11.Name and capital have the same length

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.Matching name and capital

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;

13.All the vowels

Equatorial Guinea and Dominican Republic have all of the vowels (a e i o u) in the name. They don’t count because they have more than one word in the name.

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’

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 NOT name LIKE '% %';

14.BBC QUIZ

answer: 1~7 E D B D B D C

SELECT from Nobel Tutorial

nobel(yr, subject, winner)

1. Winners from 1950

Change the query shown so that it displays Nobel prizes for 1950.

SELECT * FROM nobel
WHERE yr = 1950;

2. 1962 Literature

Show who won the 1962 prize for literature.

SELECT winner FROM nobel
WHERE yr = 1962 AND subject = 'literature';

3. Albert Einstein

Show the year and subject that won ‘Albert Einstein’ his prize.

SELECT yr,subject FROM nobel
WHERE winner = 'Albert Einstein';

4. Recent Peace Prizes

Give the name of the ‘peace’ winners since the year 2000, including 2000.

SELECT winner FROM nobel
WHERE yr >= 2000 AND subject = 'peace';

5. Literature in the 1980’s

Show all details (yr, subject, winner) of the literature prize winners for 1980 to 1989 inclusive.

SELECT * FROM nobel
WHERE yr BETWEEN 1980 AND 1989 AND subject = 'literature';

6. Only Presidents

Show all details of the presidential winners : Theodore Roosevelt, Thomas Woodrow Wilson, Jimmy Carter, Barack Obama

SELECT * FROM nobel
WHERE winner IN ('Theodore Roosevelt','Thomas Woodrow Wilson','Jimmy Carter','Barack Obama');

7. John

Show the winners with first name John

SELECT winner FROM nobel
WHERE winner LIKE 'John %';

8. Chemistry and Physics from different years

Show the year, subject, and name of physics winners for 1980 together with the chemistry winners for 1984.

SELECT * FROM nobel
WHERE (subject = 'physics' AND yr = 1980) OR (subject = 'chemistry' AND yr = 1984);

9. Exclude Chemists and Medics

Show the year, subject, and name of winners for 1980 excluding chemistry and medicine

SELECT * FROM nobel
WHERE yr = 1980 AND NOT subject IN ('chemistry','medicine');

10. Early Medicine, Late Literature

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 * FROM nobel
WHERE (yr < 1910 AND subject = 'medicine') OR (yr >= 2004 AND subject = 'literature');

11. Umlaut

Find all details of the prize won by PETER GRÜNBERG

SELECT * FROM nobel
WHERE winner = 'PETER GRÜNBERG';

12.Apostrophe

Find all details of the prize won by EUGENE O’NEILL

Escaping single quotes
You can’t put a single quote in a quote string directly. You can use two single quotes within a quoted string.

SELECT * FROM nobel
WHERE winner = 'EUGENE O''NEILL';

13.Knights of the realm

Knights in order

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. Chemistry and Physics last

The expression subject IN (‘chemistry’,‘physics’) can be used as a value - it will be 0 or 1.

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 subject IN ('chemistry','physics'),subject,winner;

15. Nobel Quiz

answer: 1~7 E C B C C C D

SELECT within SELECT Tutorial

1. Bigger than Russia

List each country name where the population is larger than that of ‘Russia’.

world(name, continent, area, population, gdp)

SELECT name FROM world
WHERE population > (SELECT population FROM world WHERE name = 'Russia');

2. Richer than UK

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. Neighbours of Argentina and Australia

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. Between Canada and Poland

Which country has a population that is more than United Kingdom but less than Germany? Show the name and the population.

SELECT name,population FROM world
WHERE population > (SELECT population FROM world WHERE name = 'United Kingdom') 
AND population < (SELECT population FROM world WHERE name = 'Germany');

5. Percentages of Germany

Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

You can use the function ROUND to remove the decimal places.
You can use the function CONCAT to add the percentage symbol.

SELECT name,
CONCAT(ROUND(population/(SELECT population FROM world WHERE name = 'Germany')*100,0),'%') FROM world
WHERE continent = 'Europe';

6. Bigger than every country in Europe

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 > (SELECT MAX(gdp) FROM world WHERE continent = 'Europe');

7. Largest in each continent

Find the largest country (by area) in each continent, show the continent, the name and the area:

SELECT continent,name,area FROM world AS x
WHERE area >= ALL(SELECT area FROM world AS y WHERE x.continent = y.continent);

8. First country of each continent (alphabetically)

List each continent and the name of the country that comes first alphabetically.

SELECT continent,name FROM world AS x
WHERE name <= ALL(SELECT name FROM world AS y WHERE x.continent = y.continent)
ORDER BY 2;

9. Difficult Questions That Utilize Techniques Not Covered In Prior Sections

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 AS x
WHERE 25000000 >= ALL(SELECT population FROM world AS y WHERE x.continent = y.continent)

10. Three time bigger

Some countries have populations more than three times that of all of their neighbours (in the same continent). Give the countries and continents.

SELECT name,continent FROM world AS x
WHERE population/3 >= ALL(SELECT population FROM world AS y WHERE x.continent = y.continent AND x.name != y.name);

Nested SELECT Quiz

answer: 1~7 C B A D B B B

SUM and COUNT

1. Total world population

Show the total population of the world.

world(name, continent, area, population, gdp)

SELECT SUM(population) FROM world;

2. List of continents

List all the continents - just once each.

SELECT continent FROM world
GROUP BY continent;

3. GDP of Africa

Give the total GDP of Africa

SELECT SUM(gdp) FROM world
WHERE continent = 'Africa';

4. Count the big countries

How many countries have an area of at least 1000000

SELECT COUNT(name) AS big_countries FROM world
WHERE area >= 1000000;

5. Baltic states population

What is the total population of (‘Estonia’, ‘Latvia’, ‘Lithuania’)

SELECT SUM(population) AS sum FROM world
WHERE name IN ('Estonia', 'Latvia', 'Lithuania');

6. Counting the countries of each continent

For each continent show the continent and number of countries.

SELECT continent,COUNT(name) FROM world
GROUP BY continent;

7. Counting big countries in each continent

For each continent show the continent and number of countries with populations of at least 10 million.

SELECT continent,COUNT(name) FROM world
WHERE population > 10000000
GROUP BY continent;

8. Counting big continents

List the continents that have a total population of at least 100 million.

SELECT continent FROM world
GROUP BY continent
HAVING SUM(population) >= 100000000;

SUM and COUNT Quiz

answer: 1~8 C A D E B E D D

  • 21
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值