Fork me on GitHub

sqlzoo练习4

Nobel Quiz

下面是Nobel表中字段的相关信息

题目

  1. Pick the code which shows the name of winner’s names beginning with C and ending in n

通配符的使用

1
2
3
select name
from nobel
where winner like 'C%'' and winner like '%n';
  1. Select the code that shows how many Chemistry awards were given between 1950 and 1960

找出两个年份之间化学奖品被授予的次数

1
2
3
4
select count(subject)   -- 统计总数
from nobel
where subject = 'Chemistry'
and yr between 1950 and 1960;
  1. Pick the code that shows the amount of years where no Medicine awards were given

统计没有医药授予的年份,需要考虑去重

1
2
3
select count(distinct yr)
from nobel
where yr not in (select distinct yr from nobel where subject = 'Medicine'); -- 现将Medicine的年份选出来,然后排除掉
  1. 从代码中选正确结果
1
SELECT subject, winner FROM nobel WHERE winner LIKE 'Sir%' AND yr LIKE '196%'

  1. Select the code which would show the year when neither a Physics or Chemistry award was given

物理和化学同时不授予的年份

1
2
3
select yr from nobel
where yr not in (select yr from nobel
where subject in ('Chemistry', 'Physics')) -- 先通过子查询选择出两个学科的年份,然后排除掉这些年份
  1. Select the code which shows the years when a Medicine award was given but no Peace or Literature award was

找出医药授予,但是和平和文学不授予的年份

1
2
3
4
select distinct yr from nobel
where subject = 'Medicine'
and yr not in (select yr from nobel where subject='Literature') -- 把两个学科的年份同时排除掉
and yr not in (select yr from nobel where subject='Peace');
  1. 从代码中选出正确的答案

select subject, count(subject) – 统计每个学科的数量
from nobel
where yr=‘1960’
group by subject;

本文标题:sqlzoo练习4

发布时间:2020年01月14日 - 18:01

原始链接:http://www.renpeter.cn/2020/01/14/sqlzoo%E7%BB%83%E4%B9%A04.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea