查询忽略空值 sql_[数库练习]丧病刷SQL·sqlzoo笔记七篇

a6661ec594f51afc9f5a56fa198bde33.png

点进本篇并阅读你可以收获:

  • [sql在线练习平台 "sqlzoo"]
  • [sql进阶 inner,left,right 三者关联的区别]
  • [sql进阶 coalesce()函数,替换null值]

Opening

本片是 丧病刷SQL的第7篇,sqlzoo 部分也要临近尾声了,

但实际上还有很多SQL 语句没有刷到,

在sqlzoo部分结束之后,我会抽出一部分时间将前面的文章进行整理订正。

DQL[1] 练习当然也不能只局限于sqlzoo给出的查询题来提升

本篇主要的刷题药店就是关联Join,主要是左右链接这些的区别以及coalesce函数,

再有就是case when的使用。

那么还是闲话少说,开始本篇~

例题网址 Using Null - SQLZOO

Previous Articles

往期文章(如果看到这篇有不懂的地方建议先刷往期文章):

Crow·Lu:[数库练习]丧病刷SQL·sqlzoo笔记六篇

Crow·Lu:[数库练习]丧病刷SQL·sqlzoo笔记五篇

Crow·Lu:[数库练习]丧病刷SQL·sqlzoo笔记四篇

Crow·Lu:[数库练习]丧病刷SQL·sqlzoo笔记三篇(上)

Crow·Lu:[数库练习]丧病刷SQL·sqlzoo笔记三篇(下)

Crow·Lu:[数库练习]丧病刷SQL·sqlzoo笔记二篇

Crow·Lu:[数库练习]丧病刷SQL·sqlzoo笔记一篇

1. List the teachers who have NULL for their department.

列出没有部门(dept)的老师

<correct answer>
select name
from teacher
where dept is null

这题条件写出dept is null 就会输出部门为空

你也可以尝试加上dept来select 看下是不是null

比如

select name,dept
from teacher
where dept is null
2. Note the INNER JOIN misses the teachers with no department and the departments with no teacher.

内联接 inner join 可以忽略掉有null值的行,内联 teacher 和 dept 表

<correct answer>

select tr.name, dept.name from 
teacher tr inner join dept
on (tr.dept = dept.id)

这题 做出来其实inner的作用已经卸载题目里了

忽略所有的null行关联结果。

总之拿下它了,

3. Use a different JOIN so that all teachers are listed.

使用其他关联查询,列出所有教师名称

其他关联查询其实无非那几种

再看他的要求 :要让老师全部出现不用管dept是不是null

那就以有老师名字的表为左来联接即可

<correct answer>

select tr.name, dept.name from 
teacher tr left join dept
on (tr.dept = dept.id)

ok拿下

4. Use a different JOIN so that all departments are listed.

使用其他链接,列出列表要求所有部门名

这题前边inner join 和left joindouble有了,

用哪个不言而喻。

<correct answer>

select tr.name, dept.name from 
teacher tr right join dept
on (tr.dept = dept.id)

应届做题技巧~拿下~

5. Show teacher name and mobile number or '07986 444 2266' (Use COALESCE to print the mobile number. Use the number '07986 444 2266' if there is no number given. )

列出教师名 和 对应的手机号码, null的填 '07986 444 2266'

填补空值用 coalesce() 函数

这题其实主要是coalesce()填补或者说是替换空值,

贴一个sqlzoo的coalesce()讲解

COALESCE - SQLZOO​sqlzoo.net

主要这个函数其实也不难,

这里简单讲一下coalesce()

coalesce()其实也跟count()这些放在select后面
然后括号里有两个参数,
第一个参数是字段名,
就是要填补null的那列字段
例如这题的mobile
而第二个参数是要填补的值
例如字符串 '07986 444 2266'

<correct answer>

select name, 
coalesce(mobile,'07986 444 2266')
from teacher

ok,拿下

6. Use the COALESCE function and a LEFT JOIN to print the teacher name and department name. Use the string 'None' where there is no department.

使用COALESCE函数和LEFT JOIN打印教师姓名和部门名称。若教师没有dept,请用字符串“None”代替。

解题与上题大体一致,

注意coalesce的参数设置就好

<correct answer>

select tr.name,
coalesce(dept.name,'None')
from 
teacher tr left join dept 
on (tr.dept = dept.id)
7. Use COUNT to show the number of teachers and the number of mobile phones.

使用COUNT显示教师人数和手机数量。

这题,也不用关联

用count即可

count()函数在第四篇有大量使用和讲解

Crow·Lu:[数库练习]丧病刷SQL·sqlzoo笔记四篇

<correct answer>

select 
count(name),
count(mobile)
from teacher

好的,拿下它

8. Use COUNT and GROUP BY dept.name to show each department and the number of staff. Use a RIGHT JOIN to ensure that the Engineering department is listed.

使用计数和分组依据部门名称 以显示每个部门和员工人数。

使用右连接以确保列出工程部门。

这里有个吐槽点就是:

你以为的你以为我会以为的右边就是你以为我要写的我的右边吗?

233333禁止套娃

<correct answer>

select dept.name,count(tr.name) from 
teacher tr right join dept on (tr.dept = dept.id) 
group by dept.name

ok,右联接拿下

9. Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept1 or 2 and 'Art' otherwise.

虽然我很想这篇也想上边coalesce函数掰开揉碎了讲个清楚

但写这篇的时间还是有限,case when 还是在下篇 也就是第8篇来讲一起讲吧

在这里先贴上代码

<correct answer>

select name,
case when 
dept = 1 or dept = 2 then 'Sci' 
else 'Art' end
from teacher
10.
Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2, show 'Art' if the teacher's dept is 3 and 'None' otherwise.

<correct answer>

select name,
case when dept = 1 or dept = 2 then 'Sci'
when dept = 3 then 'Art'
else 'None' end
from teacher

10题的case when then也将和9题一起在下篇开篇讲一下。

ok,至此10全部拿下~

Thanks

关于本篇"丧病刷SQL",

其实目前我写数据文章还有很多不足,

在这里提前感谢未来的读者提供勘误。

勘误校正:

可以在下面的评论区指出或者直接知乎私信留言。

在此提前谢谢未来勘误的朋友啦~

最后感谢:@Rocky0429

感谢你文章分享的sqlzoo,

Rocky0429:在线就能用的 SQL 练习平台我给你找好了!

真的是很好玩很好用好练习的网站,再次感谢!

Epilogue

其实写到这里还是有别的话想说,

虽然这话一篇就写过了:

刷题的各位有没有想过自己刷题的意义是什么?

[自己要走的路]要靠自己的意志来决定
—— Bruno Bucciarati 《JoJo's Bizarre Adventure》

我的答案是,为了更接近并实现自己的梦想。

Ending

希望看文章看到最后的朋友都能给予

点赞,喜欢,收藏,转发支持,

上述支持讲对应收到以下祝福:

跳槽、加薪、转行、晋升成功。

那么,本篇力度稍微差点意思的

[数库练习]丧病刷SQL·sqlzoo笔记七篇

题例 'using null'全10题

完~

参考

  1. ^DQL 主要为Select语句,SQL中依照类型分为DQL,DML,DCL,DDL四个类型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值