sqlzoo练习4

Nobel Quiz

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

sqlzoo练习4_第1张图片
image

题目

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

通配符的使用

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

找出两个年份之间,化学学科被授予的次数count

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

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

select count(distinct yr) 
from nobel
where yr not in (select distinct yr from nobel where subject = 'Medicine');   -- 现将Medicine的年份选出来,然后排除掉
  1. 从代码中选正确结果
SELECT subject, winner FROM nobel WHERE winner LIKE 'Sir%' AND yr LIKE '196%'
sqlzoo练习4_第2张图片
image
  1. Select the code which would show the year when neither a Physics or Chemistry award was given

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

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

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

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_第3张图片
image

你可能感兴趣的:(sqlzoo练习4)