SQL ZOO PART 1 : SELECT from nobel & SELECT within SELECT
SQL ZOO 練習
SQL ZOO是蠻適合新手練習SQL語法的地方,這次單純將練習時的結果紀錄下來,Nobel以前的章節都蠻基礎,nobel則是綜合前面章節的小練習,所以來試試看~第二段SELECT WITHIN SELECT則是巢狀的應用,稍微複雜不過稍為看一下不會太難懂
SELECT from Nobel
https://sqlzoo.net/wiki/SELECT_from_Nobel_Tutorial
1. Change the query shown so that it displays Nobel prizes for 1950.
SELECT * 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 LIKE 'Albert Einstein'
4. Give the name of the 'Peace' winners since the year 2000, including 2000.
SELECT winner
FROM nobel
WHERE subject LIKE 'Peace'
AND
yr >= 2000
5. Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive.
SELECT*FROM nobel
WHERE subject LIKE 'Literature'
AND
yr BETWEEN 1980 AND 1989
6. Show all details of the presidential winners:
- Theodore Roosevelt
- Woodrow Wilson
- Jimmy Carter
- Barack Obama
SELECT * FROM nobel
WHERE winner IN ('Theodore Roosevelt',
'Woodrow Wilson',
'Jimmy Carter',
'Barack Obama')
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*FROM nobel
WHERE (subject LIKE 'Physics' AND yr = 1980)
OR
(subject LIKE 'Chemistry' AND yr = 1984)
9. Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine
select * from nobel
where subject not in('Chemistry','Medicine') and yr=1980
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* FROM nobel
WHERE (yr < 1910 AND subject LIKE 'Medicine') OR
(yr >= 2004 AND subject Like 'Literature')
11. Find all details of the prize won by PETER GRÜNBERG
Non-ASCII characters
select * from nobel where winner = 'Peter Grünberg'
12. Find all details of the prize won by EUGENE O'NEILL
Non-ASCII characters
select * from nobel where winner = 'Eugene O\'Neill'
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 asc
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 winner, subject FROM nobel
where yr=1984
ORDER BY subject IN ('Physics','Chemistry'),subject asc,winner asc
#Subject in 給它賦值
SELECT within SELECT
https://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial1. 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 LIKE '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. 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.
select name,
CONCAT(ROUND(100*population/(select population from world where name='Germany')),'%')
from world
where continent='Europe'
#CONCAT為合併字串使用
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 gdp > 0 AND continent ='Europe')
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)
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 x.continent=y.continent
AND name IS NOT NULL);
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 x.continent = y.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 population*3 FROM world y
WHERE y.continent=x.continent
AND y.name!=x.name)
留言
張貼留言