153、MySQL入门(三):ORDER BY 语句用法

Sqlzoo习题练习:SELECT from nobel

习题链接:<u>http://sqlzoo.net/wiki/SELECT_from_Nobel_Tutorial</u>

下面为SELECT from nobel习题内容:

--#1
/*
Change the query shown so that it displays Nobel prizes for 1950.
*/
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1950

--#2
/*
Show who won the 1962 prize for Literature.
*/
SELECT winner
  FROM nobel
 WHERE yr = 1962
   AND subject = 'Literature'

--#3
/*
Show the year and subject that won 'Albert Einstein' his prize.
*/
SELECT yr,subject
FROM nobel
WHERE winner = 'Albert Einstein'

--#4
/*
Give the name of the 'Peace' winners since the year 2000, including 2000.
*/
SELECT winner FROM nobel
WHERE (subject LIKE 'Peace%') AND (yr >= 2000)

--#5
/*
Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive
*/
SELECT *
FROM nobel
WHERE (yr BETWEEN 1980 AND 1989) AND (subject = 'Literature')

--#6
/*
Show all details of the presidential winners:
Theodore Roosevelt
Woodrow Wilson
Jimmy Carter
*/
SELECT * FROM nobel
 WHERE winner IN ('Theodore Roosevelt',
                  'Woodrow Wilson',
                  'Jimmy Carter',
                  'Barack Obama')

--#7
/*
Show the winners with first name John
*/
SELECT winner FROM nobel
WHERE winner LIKE 'John%'

--#8
/*
Show the Physics winners for 1980 together with the Chemistry winners for 1984.
*/
SELECT *
FROM nobel
WHERE (subject = 'PhysicS' AND yr = 1980) OR (subject = 'Chemistry' AND yr = 1984)

--#9
/*
Show the winners for 1980 excluding the Chemistry and Medicine
*/
SELECT * FROM nobel
WHERE YR = 1980 AND subject NOT IN ('Chemistry','Medicine')

--#10
/*
Show 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) 

--#11
/*
当查询的内容中出现特殊字符时,使LIKE 语句进行查询。
例如:下面的名字中Ü为特殊字符,使用LIKE 语句中的%代替。
Find all details of the prize won by PETER GRÜNBERG
*/
SELECT * FROM nobel
WHERE winner LIKE 'peter gr%nberg'

--#12
/*
Find all details of the prize won by EUGENE O'NEILL
不能直接引用人名中的一个引号。可以在引用的字符串中使用两个单引号。
*/
SELECT * FROM nobel
WHERE winner = 'EUGENE O''NEILL'

--#13
/*
List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
在上面的问题中需要使用ORDER BY 语句对结果进行排序
ORDER BY 语句用于根据指定的列对结果集进行排序。
ORDER BY 语句默认按照升序对记录进行排序。
如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。
针对上面的问题先对结果中的年份进行降序排序(先显示最新的),
如果年份相同再按名字字母顺序排序。
具体操作如下:
*/
SELECT winner,yr,subject
FROM nobel
WHERE winner LIKE 'sir%'
ORDER BY yr DESC,winner

--#14
/*
The expression subject IN ('Chemistry','Physics') can be used as a value - it will be 0 or 1.
Show the 1984 winners ordered by subject and winner name; but list Chemistry and Physics last.
*/
SELECT winner, subject,subject IN ('Physics','Chemistry')
FROM nobel
WHERE yr=1984
ORDER BY subject IN ('Physics','Chemistry'),subject,winner

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,523评论 25 708
  • 什么是数据库? 数据库是存储数据的集合的单独的应用程序。每个数据库具有一个或多个不同的API,用于创建,访问,管理...
    chen_000阅读 4,059评论 0 19
  • 安装 创建gulpfile.js配置文件 安装插件 一个使用gulp的栗子
    greenteaObject阅读 272评论 0 1
  • 清晨,一杯温热的清水刷过身体的皮囊,冲洗着各处血管的污杂,还一身通透清爽,来开启一天新的时光! ...
    李惜珠阅读 211评论 0 0
  • 所有高飞的鸟都怀有归巢的夙愿所有远航的帆都祈求泊岸的平安 让流浪的人能够归来抖落异地的尘土踏进风雨中的屋檐 让牵挂...
    蓝天大海之间阅读 426评论 0 3