SELECT from Nobel Tutorial
刷题网站是SQLZoo
下面是 Nobel 的数据表:
yr | subject | winner |
---|---|---|
1960 | Chemistry | Willard F. Libby |
1960 | Literature | Saint-John Perse |
1960 | Medicine | Sir Frank Macfarlane Burnet |
1960 | Medicine | Peter Madawar |
… | … | … |
We continue practicing simple SQL queries on a single table.
This tutorial is concerned with a table of Nobel prize winners: nobel(yr, subject, winner)
Using the SELECT statement.
文章目录
- SELECT from Nobel Tutorial
- 1. Winners from 1950
- 2. 1962 Literature
- 3. Albert Einstein
- 4. Recent Peace Prizes
- 5. Literature in the 1980's
- 6. Only Presidents
- 7. John
- 8. Chemistry and Physics from different years
- 9. Exclude Chemists and Medics
- 10. Early Medicine, Late Literature
- 11. Umlaut
- 12. Apostrophe
- 13. Knights of the realm
- 14. Chemistry and Physics last
1. Winners from 1950
Change the query shown so that it displays Nobel prizes for 1950.
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1950
2. 1962 Literature
Show who won the 1962 prize for Literature.
注意使用的是 AND 还是 OR
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 yr,subject,winner
from nobel
where yr >= 1980 and yr <= 1989 and subject = 'Literature'
也可以使用between and语句
select yr,subject,winner
from nobel
where (yr between 1980 and 1989) and subject = 'Literature'
6. Only Presidents
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')
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 (subject <> 'Medicine' and subject <> 'Chemistry') and yr = 1980
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 (subject = 'Medicine' and yr < 1910) or (subject = 'Literature' and yr >= 2004)
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
在SQL语句中,字符串是用两个单引号包起来标示的,所以要在字符串里保留单引号,必须要转义,而转义很简单,就是两个连续的单引号就表示一个单引号字符。
select * from nobel
where winner = 'EUGENE O''NEILL'
13. Knights of the realm
List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
这里需要调用 ORDER BY 语句对结果集进行排序,asc 表示升序,desc 表示降序
select winner,yr,subject from nobel
where winner like 'Sir%'
order by yr desc, winner asc
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 case when subject IN ('Chemistry','Physics') then 1 else 0 end asc
,subject asc,winner asc
case when 语句的意思是 subject 为 Chemistry 和 Physics 这两科的时候,将排序顺序置为1,其余顺序置为0,即将这两科排在最后。