美文网首页
The nobel tablecan be used to pr

The nobel tablecan be used to pr

作者: 葵小ci | 来源:发表于2018-04-13 14:48 被阅读463次

    http://sqlzoo.net/wiki/The_nobel_table_can_be_used_to_practice_more_subquery./zh

    nobel( yr,subject, winner) 

    Chemistry 化學獎

    Economics 經濟獎

    Literature 文學獎

    Medicine 醫學獎

    Peace 和平獎

    Physics 物理獎

    1.

    紅十字國際委員會(International Committee of the Red Cross) 曾多次獲得和平獎。 試找出與紅十字國際委員會同年得獎的文學獎(Literature)得獎者和年份。

    SELECT WINNER,YRFROM nobel

    WHERESUBJECT='Literature'AND YR in (Select yr from nobel where winner='InternationalCommittee of the Red Cross' and subject='peace')

    说明:先求出红十字会获和平奖的时间“yr”;然后根据这个时间求出得文学奖的winner。

    语句【Select yr

    from nobel where sunject=’peacr’and winner='International Committee of the Red

    Cross'】得出红十字会获和平奖的时间,此处结果显示为多个数据,因为需要选用函数in()。

    2.

    日本物理學家益川敏英(Toshihide Maskawa) 曾獲得物理獎。同年還有兩位日本人一同獲得物理獎。試列出這2位日本人的名稱。

    Select distinctwinner from nobel

    Wheresubject='Physics'

    and yr in (Selectyr from nobel where winner='Toshihide Maskawa'and subject='Physics') andwinner!='Toshihide Maskawa'

    说明:先查询出winner“Toshihide Maskawa”获得物理奖的时间yr,此处得出一组数据,且此数据可能为重复。所以用函数IN()。

    根据原题意,在求出时间YR后,用去重函数distinct()得出所有获奖人姓名,并且排除

    Toshihide Maskawa。

    3.

    首次頒發的經濟獎(Economics)的得獎者是誰?

    SELECT WINNER FROMnobel where yr=

    (SELECT YR FROMnobel WHERE SUBJECT='Economics'

    ORDER BY YR

    LIMIT 1) ANDSUBJECT='Economics'

    说明:

    先求出首次颁发经济奖的时间,再根据时间得出winner,此处首次颁奖时间的值仅为一个,但是获奖者winner可能有多个数值。

    Order by 按照时间排序(默认从小到大升序排序)可得出所有颁奖时间,limit 1则可得出首次颁奖时间。

    4.

    哪幾年頒發了物理獎,但沒有頒發化學獎?

    SELECT DISTINCT YR FROM nobel

    WHERE SUBJECT='Physics' AND YR NOT IN(SELECT YR FROMnobel where subject='Chemistry')

    说明:使用去重函数distinct();根据题意,所求时间YR必须获得了物理奖,但是没有化学奖,语句SELECT YR FROM nobel where subject='Chemistry'得出获得化学奖的时间,使用否定含义的函数 NOT IN则可以满足没有获得化学奖的时间。

    5.

    哪幾年的得獎者人數多於12人呢? 列出得獎人數多於12人的年份,獎項和得獎者。

    select * fromnobel

    where yr in(SELECTYR FROM nobel GROUP BY YR HAVING COUNT(WINNER)>12)

    说明:

    语句SELECT YR

    FROM nobel GROUP BY YR HAVING COUNT(WINNER)>12意为按照时间yr分组且人数超过12人的时间年份“yr”。count()意为对获奖者winner进行计数。

    6.

    哪些得獎者獲獎多於1次呢?他們是哪一年獲得哪項獎項呢?列出他們的名字,獲獎年份及獎項。先按名字,再按年份順序排序。

    Selectwinner,yr,subject From nobel

    Where winnerin(Select winner

    From nobel

    Group by winner

    Havingcount(winner)>1)

    Order by winner,yr

    相关文章

      网友评论

          本文标题:The nobel tablecan be used to pr

          本文链接:https://www.haomeiwen.com/subject/kpbckftx.html