SQLZOO练习7--Using NULL
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的更多相关文章
- 《深入理解JAVA虚拟机》笔记1
java程序运行时的内存空间,按照虚拟机规范有下面几项: )程序计数器 指示下条命令执行地址.当然是线程私有,不然线程怎么能并行的起来. 不重要,占内存很小,忽略不计. )方法区 这个名字很让我迷惑. ...
- sqlzoo练习题答案
title: SQL-Learning date: 2019-03-12 20:37:21 tags: SQL --- 这是关于在一个SQL学习网站的练习题答案记录:SQL教程 SQL基础 由一些简单 ...
- sqlzoo易错题
https://sqlzoo.net/wiki/SELECT_names 答案在:https://github.com/codyloyd/sqlzoo-solutions/blob/master/SQ ...
- SQLZOO 习题
https://sqlzoo.net 8. 美國.印度和中國(USA, India, China)是人口又大,同時面積又大的國家.排除這些國家. 顯示以人口或面積為大國的國家,但不能同時兩者.顯示國家 ...
- SELECT within SELECT Tutorial -- SQLZOO
SELECT within SELECT Tutorial 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.List each count ...
- 【小计】新人Tostring前忘记Null判断的处理
ToString和string.Concat(可屏蔽Null的异常)性能相差不大,一些中小项目完全可以用Concat(新人容易忘记判断Null的情况,遇到太多了,所以建议重写tostring方法,内部 ...
- 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. ...
- 异步 HttpContext.Current 为空null 另一种解决方法
1.场景 在导入通讯录过程中,把导入的失败.成功的号码数进行统计,然后保存到session中,客户端通过轮询显示状态. 在实现过程中,使用的async调用方法,出现HttpContext.Curren ...
- js中的null 和undefined
参考链接:http://blog.csdn.net/qq_26676207/article/details/53100912 http://www.ruanyifeng.com/blog/2014/0 ...
随机推荐
- 如何用C/C++实现去除字符串头和尾指定的字符
编程时我们经常需要对字符串进行操作,其中有一项操作就是去除字符串的头(尾)指定的字符,比如空格.通常我们会使用封装好的库函数或者类函数的Trim方法来实现,如果自己动手写一个TrimHead和Trim ...
- SpringBoot项目刚刚创建就报异常,Failed to configure a DataSource: 'url' attribute is not specified and no embedded 的解决办法
错误信息: Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedde ...
- Java 对象头那点事
概览 对象头 存放:关于堆对象的布局.类型.GC状态.同步状态和标识哈希码的基本信息.Java对象和vm内部对象都有一个共同的对象头格式. (后面做详细介绍) 实例数据 存放:类的数据信息,父类的信息 ...
- VMware 虚拟机图文安装和配置 Rocky Linux 8.5 教程
前言 2020 年,CentOS 宣布:计划未来将重心从 CentOS Linux 转移到 CentOS Stream.CentOS 8 的生命周期已于 2021 年 12 月 31 日终止,而 Ce ...
- 923. 3Sum With Multiplicity - LeetCode
Question 923. 3Sum With Multiplicity Solution 题目大意: 给一个int数组A和一个目标值target,求满足下面两个条件的组合个数,其中i,j,k分别为数 ...
- JavaScript数组操作常用方法
@ 目录 数组基础遍历方法. for for of for in 数组的基础操作方法. push:尾部追加元素 pop:尾部移出元素 unshift:头部追加元素 shift:头部移出元素 splic ...
- unity---监听物体被点击
脚本 public void OnPointerClick(PointerEventData eventData) { Debug.LogFormat("{0} is Click" ...
- python面向对象双下划线方法与元类
目录 双下划线方法(__) 元类简介 产生类的两种表现形式 元类的基本使用 元类进阶操作 __new__方法 双下划线方法(__) 面向对象中的双下方法也有一些人称之为是魔法方法,有些双下方法不需要刻 ...
- Link-Cut-Tree(1)
参考论文 求解范围:(动态树问题) 树上路径查询.修改 动态连边.删边 换根 lca 算法逻辑 概念: 类似树链剖分,把一棵树拆成许多链,每个链用splay维护(链上的为实边,否则为虚边),splay ...
- Linux下添加MySql组件后报无权限问题解决
Tomcat日志报错如下: Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using pas ...