SQLZOO练习题总结(一)

SELECT basics

 

world

namecontinentareapopulationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748283174112960000000
AlgeriaAfrica238174137100000188681000000
AndorraEurope468781153712000000
AngolaAfrica124670020609294100990000000
....

Introducing the world table of countries

1.

The example uses a WHERE clause to show the population of 'France'. Note that strings (pieces of text that are data) should be in 'single quotes';

Modify it to show the population of Germany

select population
from world 
where name = 'Germany'

Scandinavia

2.

Checking a list The word IN allows us to check if an item is in a list. The example shows the name and population for the countries 'Brazil', 'Russia', 'India' and 'China'.

Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.

select name, population 
from world 
where name in ('Sweden', 'Norway', 'Denmark');

Just the right size

3.

Which countries are not too small and not too big? BETWEEN allows range checking (range specified is inclusive of boundary values). The example below shows countries with an area of 250,000-300,000 sq. km. Modify it to 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

 

 

SELECT from WORLD Tutorial

namecontinentareapopulationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748283174112960000000
AlgeriaAfrica238174137100000188681000000
AndorraEurope468781153712000000
AngolaAfrica124670020609294100990000000
...

Introduction

1.

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

Large Countries

2.

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

Per capita GDP

3.

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

HELP:How to calculate per capita GDP

per capita GDP is the GDP divided by the population GDP/population

select name,gdp/population 
from world 
where population>=200000000

South America In millions

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'

France, Germany, Italy

5.

Show the name and population for France, Germany, Italy

select name,population 
from world 
where name in('France','Germany','Italy');

United

6.

Show the countries which have a name that includes the word 'United'

select name 
from world 
where name like 'United%';

Two ways to be big

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

One or the other (but not both)

8.

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.

  • Australia has a big area but a small population, it should be included.
  • Indonesia has a big population but a small area, it should be included.
  • China has a big population and big area, it should be excluded.
  • United Kingdom has a small population and a small area, it should be excluded.
select name,population,area 
from world 
where area>3000000 xor population > 250000000

Rounding

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.

Millions and billions

Divide by 1000000 (6 zeros) for millions. Divide by 1000000000 (9 zeros) for billions.
 

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

Trillion dollar economies

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) 
from world 
where gdp >=1000000000000

Name and capital have the same length

11.

Greece has capital Athens.

Each of the strings 'Greece', and 'Athens' has 6 characters.

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)

Matching name and capital

12.

The capital of Sweden is Stockholm. Both words start with the letter 'S'.

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

All the vowels

13.

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 name not like'% %'

 

SELECT from Nobel Tutorial

nobel

  yrsubjectwinner
1960ChemistryWillard F. Libby  
1960LiteratureSaint-John Perse  
1960MedicineSir Frank Macfarlane Burnet  
1960MedicinePeter Madawar  
...

 

Winners from 1950

1.

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

select yr, subject, winner  
from nobel 
where yr = 1950

1962 Literature

2.

Show who won the 1962 prize for Literature.

select winner 
from nobel 
where yr = 1962 AND subject = 'Literature'

Albert Einstein

3.

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

select yr,subject 
from nobel 
where winner='Albert Einstein'

Recent Peace Prizes

4.

Give the name of the 'Peace' winners since the year 2000, including 2000.

select winner 
from nobel 
where subject='Peace' and yr>=2000

Literature in the 1980's

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'

Only Presidents

6.

Show all details of the presidential winners:

  • Theodore Roosevelt
  • Woodrow Wilson
  • Jimmy Carter
  • Barack Obama
select * 
from nobel 
where yr = 1970 and subject in ('Cookery', 'Chemistry', 'Literature')

John

7.

Show the winners with first name John

select winner 
from nobel 
where winner like 'John%'

Chemistry and Physics from different years

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'

Exclude Chemists and Medics

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

Early Medicine, Late Literature

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 (subject='medicine' and yr<1910) 
or subject='literature' and yr>=2004;

 

Harder Questions

Umlaut

11.

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

select * 
from nobel 
where winner='PETER GRÜNBERG';

Apostrophe

12.

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

Knights of the realm

13.

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;

Chemistry and Physics last

14.

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 within SELECT Tutorial

This tutorial looks at how we can use SELECT statements within SELECT statements to perform more complex queries.

namecontinentareapopulationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748283174112960000000
AlgeriaAfrica238174137100000188681000000
AndorraEurope468781153712000000
AngolaAfrica124670020609294100990000000

 

Bigger than Russia

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')

Richer than UK

2.

Show the countries in Europe with a per capita GDP greater than 'United Kingdom'.

Per Capita GDP

The per capita GDP is the gdp/population
 

select name 
from world 
where gdp/population > 
(select gdp/population 
from world 
where name='United Kingdom') 
and continent='Europe'

Neighbours of Argentina and Australia

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

Between Canada and Poland

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') 
order by name

Percentages of Germany

5.

Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany.

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

The format should be Name, Percentage for example:

namepercentage
Albania3%
Andorra0%
Austria11%
......

Decimal places

You can use the function ROUND to remove the decimal places.

Percent symbol %

You can use the function CONCAT to add the percentage symbol.
 

select name,
concat(round(100*population/(select population from world where name='Germany'),0),'%')
from world where continent='Europe'

Bigger than every country in 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)

Largest in each continent

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 x.area >= all(select y.area from world y where y.continent=x.continent and area>0)

First country of each continent (alphabetically)

8.

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

select continent,name 
from world x 
where x.name=
(select y.name from world y where x.continent=y.continent order by name limit 1) 

Difficult Questions That Utilize Techniques Not Covered In Prior Sections

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 y.populationfrom world y where x.continent=y.continent and population>0)

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 x.population >=
all(select y.population*3 
    from world y 
    where y.continent=x.continent and population >0 and x.name!=y.name )

 

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值