1.round(x)函数:返回离x最近的整数,也就是对x进行四舍五入处理。
round(x,y)函数:返回x保留到小数点后y位的值,截断时需要进行四舍五入处理
例1:
使用round(x)函数得到离1.7和1.3最近的整数,也就是用round(x)函数对1.7和1.3进行四舍五入;使用round(x,y)函数得到2.4789996保留到小数点后3位的值。
select round(1.7),round(1.3),round(2.4789996,3)
例2:
除以為1000000(6個零)是以百萬計。除以1000000000(9個零)是以十億計。使用 ROUND函數來顯示的數值到小數點後兩位。
例3:
顯示國家有至少一個萬億元國內生產總值(萬億,也就是12個零)的人均國內生產總值。四捨五入這個值到最接近1000。
顯示萬億元國家的人均國內生產總值,四捨五入到最近的$ 1000。
select name,round(gdp/population, -3) from world where gdp > 1000000000000
2.DESC ASC
使用order by 可以对查询结果进行升序(ASC)或降序(DESC)排列,默认情况下,order by 按升序输出结果,如果要按降序排列可以使用DESC来实现。
说明:对含有NULL值的列进行排序时,如果是按升序排列,NULL值将会出现在最前面,如果是按降序排列,NULL值将会出现在最后。
3.单引号引用
例:
查找尤金•奧尼爾EUGENE O'NEILL得獎的所有細節 Find all details of the prize won by EUGENE O'NEILL
select * from nobel where winner = 'EUGENE O''NEILL'
4.带ALL关键字的子查询
ALL关键字表示满足所有条件,使用ALL关键字时,只有满足内层查询语句返回的所有结果,才可以执行外层查询语句。
例1:
查詢找到世界上最大的國家(以人口計算)
select name from world where population >= all(select population from world where population >0)
例2:
哪些國家的GDP比Europe歐洲的全部國家都要高呢? [只需列出 name 。] (有些國家的記錄中,GDP是NULL,沒有填入資料的。)
select name from world where gdp > all(select gdp from world where continent = ' Europe' and gdp >0)
例3:
在每一個州中找出最大面積的國家,列出洲份 continent, 國家名字 name 及面積 area。 (有些國家的記錄中,AREA是NULL,沒有填入資料的。)
select continent, name, area from world x where area >= all(select area from world y where x.continent = y.continent and area > 0)
例4:
列出洲份名稱,和每個洲份中國家名字按子母順序是排首位的國家名。(即每洲只有列一國)
select continent, name from world x where name <= all(select name from world y where x.continent = y.continent order by name)
例5:
找出洲份,當中全部國家都有少於或等於 25000000 人口. 在這些洲份中,列出國家名字name,continent 洲份和population人口。
select name, continent, population from world x where 25000000 >= all (select population from world y where x.continent = y.continent)
例6:
有些國家的人口是同洲份的所有其他國的3倍或以上。列出 國家名字name 和 洲份 continent。
select name, continent from world x where population/3 >= all(select population from world y where x.continent = y.continent and x.name != y.name)