1. 程式人生 > >SQL的一點總結(持續更新。。。)

SQL的一點總結(持續更新。。。)

1、Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.顯示1984年的獲獎者按學科和獲獎者排序;但化學和物理排在後面。

對應的SQL語句為:SELECT winner, subject
                      FROM nobel
                       WHERE yr=1984
                       ORDER BY subject IN ('Physics','Chemistry'),subject ,winner ;

2、查詢尤金•奧尼爾EUGENE O'NEILL得獎的所有細節Find all details of the prize won by EUGENE O'NEILL

對應的SQL語句為:SELECT *
                      FROM nobel

                      WHERE winner='EUGENE O''NEILL';(不能把一個單引號直接的放在字串中。但可連續使用兩個單引號在字串中當作一個單引號)

3、找出哪些國家的人口是高於歐洲每一國的人口。

SELECT name FROM world
 WHERE population >ALL


      (SELECT population FROM world
        WHERE continent='Europe');

4、顯示歐洲的國家名稱name和每個國家的人口population。以德國的人口的百分比作人口顯示。

SELECT name,CONCAT(ROUND(population*100/(SELECT population FROM world WHERE name='Germany'),0),'%') FROM world WHERE continent='Europe';

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

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

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

SELECT winner FROM nobel
WHERE subject='Economics'
      AND yr IN(SELECT min(yr) FROM nobel WHERE subject='Economics');