We continue practicing simple SQL queries on a single table.
This tutorial is concerned with a table of Nobel prize winners:
nobel(yr, subject, winner)
Using the SELECT
statement.
检索1950年的诺贝尔奖信息。
Change the query shown so that it displays Nobel prizes for 1950.
SELECT yr,subject,winner
FROM nobel
WHERE yr=1950;
显示1962年诺贝尔文学奖得主信息。
Show who won the 1962 prize for Literature.
SELECT winner
FROM nobel
WHERE yr=1962 AND subject ='Literature';
显示爱因斯坦得诺贝尔奖的时间和奖项。
Show the year and subject that won 'Albert Einstein' his prize.
显示自2000年(包含2000)年至今,诺贝尔和平奖的得主。
Give the name of the 'Peace' winners since the year 2000, including 2000.
SELECT winner
FROM nobel
WHERE subject='Peace' AND yr>=2000;
显示从1980-1989年诺贝尔文学奖的所有信息。
Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive.
SELECT *
FROM nobel
WHERE subject='Literature' AND yr BETWEEN 1980 AND 1989;
显示总统得主‘西奥多·罗斯福’,‘伍德罗·威尔逊’,‘吉米·卡特’,‘巴拉克·奥巴马’的所有信息。
Show all details of the presidential winners:
SELECT *
FROM nobel
WHERE winner IN('Theodore Roosevelt','Woodrow Wilson','Jimmy Carter','Barack Obama');
显示名为‘John’的诺贝尔奖得主姓名。
Show the winners with first name John
SELECT winner
FROM nobel
WHERE winner LIKE 'John%';
显示1980年诺贝尔物理学和1984年诺贝尔化学奖得主的年份,奖项,姓名。
Show the year, subject, and name of Physics winners for 1980 together with the Chemistry winners for 1984.
SELECT yr,subject,winner
FROM nobel
WHERE subject='Physics' AND yr=1980 OR subject='Chemistry' AND yr=1984;
显示1980年诺贝尔除化学和药学之外的所有信息。
Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine
SELECT * FROM nobel
WHERE yr=1980 AND subject NOT IN ('Chemistry','Medicine');
显示1910年以前(不包含1910年)医药学得奖信息和2004年后(包含2004年)的文学奖得奖信息。
Show year, subject, and name of people who won a 'Medicine' prize in an early year (before 1910, not including 1910) together with winners of a 'Literature' prize in a later year (after 2004, including 2004)
SELECT * FROM nobel
WHERE subject='Medicine' AND yr <1910 OR subject='Literature' AND yr>=2004;
显示PETER GRÜNBERG的得奖信息。
Find all details of the prize won by PETER GRÜNBERG
SELECT * FROM nobel
WHERE winner='PETER GRÜNBERG';
显示EUGENE O'NEILL的得奖信息。
Find all details of the prize won by EUGENE O'NEILL
-- 字符串中出现单引号时,要加一个单引号,或者\
SELECT * FROM nobel
WHERE winner='EUGENE O''NEILL';
Knights in order
罗列以‘Sir’开头的得奖者姓名,年份,得奖项信息,年份按照最近排序,然后按照名字排序
List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
SELECT winner,yr,subject
FROM nobel
WHERE winner LIKE 'Sir%'
ORDER BY yr DESC,winner;
The expression subject IN ('Chemistry','Physics') can be used as a value - it will be 0 or 1.
Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
SELECT winner, subject FROM nobel
WHERE yr=1984
ORDER BY subject IN('Chemistry','Physics'),subject,winner;
手机扫一扫
移动阅读更方便
你可能感兴趣的文章