SQL-SQLZOO学习笔记

Tutorials: Learn SQL in stages

虽然日常工作中经常用到SQL,不过没有系统性的训练就难以有效的提升查询效率。本文记录每一节的常用函数及应用方法,以备以后查找。
https://sqlzoo.net/wiki/SQL_Tutorial

SELECT names

函数 用法
like(’’) 模糊匹配,可以用’%‘和’_'占位符,前者可表白空值或多个任意字符,后者代表一个字符且不代表空字符,以及not like()用法
concat(str1,str2,…) 可将多个字符串拼接在一起,和"两个竖杠"符号作用相同
locate(a,b) 判断a是否在b中,返回0或1,用法类似于instr()
replace(‘vessel’,‘e’,‘a’) 将字符串中’vessel’的’e’用’a’代替,最终输出’vassal’

SELECT from World

函数 用法
xor 区别于or,or是指满足一个条件即可,包括同时满足条件的;xor是指只需满足一个条件即可,但不能同时满足
round(,n) 四舍五入将值保留指定n位数,n可以为负值
<> 和!=效果相同
left(str,1) 从左边第一个字符开始,取出现的前几个字符,相似的有right(str,1),substr(str,i,j) ,在ORACLE中无此项用法

SELECT from Nobel Tutorial

函数 用法
str IN (‘str1’,‘str2’,…) The expression str IN (‘str1’,‘str2’) can be used as a value - it will be 0 or 1,即in函数可以作为一个整体来用

注意“与”和“或”的逻辑关系
eg.
3. Pick the code that shows the amount of years where no Medicine awards were given.
案例图片
eg.
6. Select the code which shows the years when a Medicine award was given but no Peace or Literature award was.
案例图

SELECT within SELECT Tutorial

本节主要练习嵌套查询,嵌套查询中子查询的结果既可以在父查询的条件中使用也可以在结果中使用,更复杂的用法需将查询表命别名(将内外表连起来,相当于创建了一个查询条件)。

函数 用法
ALL >ALL:代表父查询中的结果集大于子查询中每一个结果集中的值,则为真
ANY >ANY:代表父查询中的结果集大于子查询中任意一个结果集中的值,则为真;=ANY:与子查询IN相同;<>ANY:类NOT IN

eg.
7.Find the largest country (by area) in each continent, show the continent, the name and the area:

select continent,name,area from world x
where area >=all(
select area from world y
where x.continent=y.continent)

eg.
10.Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continen

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值