SQLZoo刷题系列 2

SELECT from Nobel Tutorial

刷题网站是SQLZoo

下面是 Nobel 的数据表:

yrsubjectwinner
1960ChemistryWillard F. Libby
1960LiteratureSaint-John Perse
1960MedicineSir Frank Macfarlane Burnet
1960MedicinePeter 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.

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,即将这两科排在最后。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值