2.Show the name and the population for Sweden, Norway and Denmark
SELECT name, population FROM world WHERE name IN (Sweden, Norway, Denmark);
3.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
注意:BETWEEN AND 包含边界值
SELECT from WORLD
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
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
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 like 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.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) 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) XOR name = capital
或者
SELECT name, capital FROM world
WHERE (LEFT(name, 1) = LEFT(capital, 1)) <> (name = capital)
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 % %;
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 nobelwhere winner =Albert Einstein
4.Give the name of the Peace winners since the year 2000, including 2000.
select winner from nobel where yr>=2000and subject =Peace
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 between 1980 and 1989 and subject=Literature
6.Show all details of the presidential winners:Theodore Roosevelt、Woodrow Wilson、Jimmy Carter
SELECT * FROM nobel WHERE winner IN (Theodore Roosevelt,Woodrow Wilson,Jimmy Carter)
7.Show the winners with first name John
select winner from nobel where winner like John%
8.Show the Physics winners for 1980 together with the Chemistry winners for 1984.
select yr,subject,winner from nobel
where (yr=1980and subject=physics) or (yr=1984and subject =chemistry
9.Show the winners for 1980 excluding the Chemistry and Medicine
select yr,subject,winner from nobel
where yr=1980 and subject not in(Chemistry,Medicine
10.Show 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 yr,subject,winner from nobel where winner =PETER GRüNBERG
12.Find all details of the prize won by EUGENE ONEILL
select yr,subject,winner 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 ordered by subject and winner name; but list Chemistry and Physics last.
SELECT winner, subject FROM nobel
WHERE yr=1984 ORDER BY subject IN (Physics,Chemistry),subject,winner
SELECT within SELECT
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 =Europeand ((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)),%)
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)