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

本文是作者学习SQLZOO教程的笔记,涵盖从基础的SELECT操作到复杂的JOIN操作,记录了每个阶段的关键知识点和实例,旨在提升SQL查询效率。内容包括:SELECT函数应用、嵌套查询、SUM和COUNT、JOIN操作等。
最低0.47元/天 解锁文章
551

被折叠的 条评论
为什么被折叠?



