The nobel table can be used to practice more subquery./zh

From SQLZOO
Jump to: navigation, search

諾貝爾獎:子查詢

此教程有關子查詢

練習

  • Chemistry 化學獎
  • Economics 經濟獎
  • Literature 文學獎
  • Medicine 醫學獎
  • Peace 和平獎
  • Physics 物理獎

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

nobel(yr, subject, winner)  
SELECT * FROM nobel 
WHERE winner = 'International Committee of the Red Cross'
SELECT winner , yr
FROM nobel WHERE
subject ='Literature' AND
yr IN
(
SELECT yr
FROM nobel 
WHERE winner = 'International Committee of the Red Cross')


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

nobel(yr, subject, winner)  
SELECT winner
FROM nobel
WHERE winner = 'Toshihide Maskawa'
SELECT winner 
FROM nobel WHERE
subject = 'Physics' AND
winner <> 'Toshihide Maskawa' AND
yr = (
SELECT yr
FROM nobel
WHERE winner = 'Toshihide Maskawa')

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

nobel(yr, subject, winner)  
 
SELECT winner
FROM nobel WHERE
subject = 'Economics' AND yr = (
SELECT MIN(yr)
FROM nobel
WHERE subject = 'Economics')

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

nobel(yr, subject, winner)  
 
SELECT DISTINCT yr 
FROM nobel
WHERE subject = 'Physics' AND yr NOT IN (
SELECT yr
  FROM nobel
 WHERE subject = 'Chemistry')


Using GROUP BY and HAVING.

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

nobel(yr,subject, winner)  
 
SELECT yr, subject, winner
FROM nobel WHERE yr IN(
 SELECT yr
  FROM nobel
GROUP BY yr
HAVING COUNT(*)>12
)

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

nobel(yr, subject, winner)  
 
SELECT winner , yr, subject
FROM nobel
WHERE winner IN (
SELECT winner
  FROM nobel
GROUP BY winner
HAVING COUNT(*) >1
)
ORDER BY winner, yr