teacher表:

id dept name phone mobile
101 1 Shrivell 2753 07986 555 1234
102 1 Throd 2754 07122 555 1920
103 1 Splint 2293  
104   Spiregrain 3287  
105 2 Cutflower 3212 07996 555 6574
106   Deadyawn 3345

dept表:

id name
1 Computing
2 Design
3 Engineering

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

select name
from teacher
where dept is NULL;

2.Note the INNER JOIN misses the teachers with no department and the departments with no teacher.

SELECT teacher.name, dept.name
FROM teacher INNER JOIN dept
ON teacher.dept=dept.id;

内部联结INNER JOIN…ON 和之前的 JOIN…ON 是一样的。内部联结对NULL值不起作用。若对应的那一列有NULL值,则这些行无法对应到另一个表。只有有值的那些行能和另一表对应,才能被选择。

所谓NULL值,就是有些行不是两个表都有的,有些这个有,另一个没有。

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

 
select t.name,d.name
from teacher t
left join dept d
on t.dept=d.id;

解题思路: left join,以teacher左表为主表,向左连接。和inner join的区别在于,inner join公共列出现null值时,将忽略null值。

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

select t.name,d.name
from teacher t
right join dept d
on t.dept=d.id;

解题思路,right join,以dept右表为主表,向右连接。

Using Coalesce Fuction

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

使用coalesce函数,显示教师姓名和电话号码,如果电话号码是空值,用‘07986 444 2266’填充

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

解题思路,colaesce函数是缺失值处理,针对的是null的情况,注意,coalesce函数对空格不起作用。

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.

select t.name,coalesce(d.name,'None')
from teacher t
left join dept d
on t.dept=d.id;

7.Use COUNT to show the number of teachers and the number of mobile phones.

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.

select d.name,count(t.name)
from teacher t
right join dept d
on t.dept=d.id
group by 1;

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

使用 CASE 显示每位教师的姓名,如果教师在部门 1 或 2,则显示“Sci”,否则显示“Art”。

select name,(case when dept in(1,2) then 'Sci' else 'Art' end) from teacher;

解题思路:考察case when

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.

使用 CASE 显示每位教师的姓名,如果教师在部门 1 或 2,则显示“Sci”,如果教师的部门是 3,则显示“Art”,否则显示“None”。

select name,(case when dept in(1,2) then 'Sci'
when dept=3 then 'Art'
else 'None' end)
from teacher;

解题思路:考察case when 多个条件。

SQLZOO练习7--Using NULL的更多相关文章

  1. 《深入理解JAVA虚拟机》笔记1

    java程序运行时的内存空间,按照虚拟机规范有下面几项: )程序计数器 指示下条命令执行地址.当然是线程私有,不然线程怎么能并行的起来. 不重要,占内存很小,忽略不计. )方法区 这个名字很让我迷惑. ...

  2. sqlzoo练习题答案

    title: SQL-Learning date: 2019-03-12 20:37:21 tags: SQL --- 这是关于在一个SQL学习网站的练习题答案记录:SQL教程 SQL基础 由一些简单 ...

  3. sqlzoo易错题

    https://sqlzoo.net/wiki/SELECT_names 答案在:https://github.com/codyloyd/sqlzoo-solutions/blob/master/SQ ...

  4. SQLZOO 习题

    https://sqlzoo.net 8. 美國.印度和中國(USA, India, China)是人口又大,同時面積又大的國家.排除這些國家. 顯示以人口或面積為大國的國家,但不能同時兩者.顯示國家 ...

  5. SELECT within SELECT Tutorial -- SQLZOO

    SELECT within SELECT Tutorial 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.List each count ...

  6. 【小计】新人Tostring前忘记Null判断的处理

    ToString和string.Concat(可屏蔽Null的异常)性能相差不大,一些中小项目完全可以用Concat(新人容易忘记判断Null的情况,遇到太多了,所以建议重写tostring方法,内部 ...

  7. SQL Server-聚焦NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL性能分析(十八)

    前言 本节我们来综合比较NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL的性能,简短的内容,深入的理解,Always to review the basics. ...

  8. 异步 HttpContext.Current 为空null 另一种解决方法

    1.场景 在导入通讯录过程中,把导入的失败.成功的号码数进行统计,然后保存到session中,客户端通过轮询显示状态. 在实现过程中,使用的async调用方法,出现HttpContext.Curren ...

  9. js中的null 和undefined

    参考链接:http://blog.csdn.net/qq_26676207/article/details/53100912 http://www.ruanyifeng.com/blog/2014/0 ...

随机推荐

  1. 攻防世界-MISC:神奇的Modbus

    这是攻防世界高手进阶区的第三题,题目如下: 点击下载附件一,得到一个流量包,题目中提到的modbus,百度百科的解释如下: 用wireshark打开流量包,搜索modbus 然后鼠标右键选择追踪流,再 ...

  2. npm install xxxx --legacy-peer-deps命令是什么?

    摘要:带着好奇心,研究学习了一番npm install xxxx --legacy-peer-deps命令是什么?为什么可以解决下载时候产生的依赖冲突呢? 本文分享自华为云社区<npm inst ...

  3. 文本框字符限制、focus光标定位

    一.为一个元素的所有子元素设置统一样式:.className * { color: #6666 } 二.正则表达式: 1.去除所有HTML标签只保留文字: /<\/?.+?\/?>/2.去 ...

  4. Django学习——ajax发送其他请求、上传文件(ajax和form两种方式)、ajax上传json格式、 Django内置序列化(了解)、分页器的使用

    1 ajax发送其他请求 1 写在form表单 submit和button会触发提交 <form action=""> </form> 注释 2 使用inp ...

  5. sklearn机器学习实战-KNN

    KNN分类 KNN是惰性学习模型,也被称为基于实例的学习模型 简单线性回归是勤奋学习模型,训练阶段耗费计算资源,但是预测阶段代价不高 首先工作是把label的内容进行二值化(如果多分类任务,则考虑On ...

  6. 函数式接口和@FunctionalInterface

    函数式接口的特点 接口有且仅有一个抽象方法 允许定义静态方法和默认方法(这两个都不是抽象方法) 允许java.lang.Object中的public方法(因为任何一个函数式接口的实现,默认都继承了Ob ...

  7. 在字节跳动,一个更好的企业级SparkSQL Server这么做

    SparkSQL是Spark生态系统中非常重要的组件.面向企业级服务时,SparkSQL存在易用性较差的问题,导致难满足日常的业务开发需求.本文将详细解读,如何通过构建SparkSQL服务器实现使用效 ...

  8. Elasticsearch高级之-集群搭建,数据分片

    目录 Elasticsearch高级之-集群搭建,数据分片 一 广播方式 二 单播方式 三 选取主节点 四 什么是脑裂 五 错误识别 Elasticsearch高级之-集群搭建,数据分片 es使用两种 ...

  9. 关于linux多线程fork的理解和学习

    fork在英文中是"分叉"的意思.为什么取这个名字呢?因为一个进程在运行中,如果使用了fork函数,就产生了另一个进程,于是进程就"分叉"了,所以这个名字取得很 ...

  10. @Inherited 原注解功能介绍

    @Inherited 底层 package java.lang.annotation; /** * Indicates that an annotation type is automatically ...