草庐IT

SQLZOO练习7--Using NULL

徐若离 2023-03-28 原文

 

teacher表:

iddeptnamephonemobile
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表:

idname
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. 牛客网专项练习30天Pytnon篇第02天 - 2

    1.在Python3中,下列关于数学运算结果正确的是:(B)a=10b=3print(a//b)print(a%b)print(a/b)A.3,3,3.3333...B.3,1,3.3333...C.3.3333...,3.3333...,3D.3.3333...,1,3.3333...解析:    在Python中,//表示地板除(向下取整),%表示取余,/表示除(Python2向下取整返回3)2.如下程序Python2会打印多少个数:(D)k=1000whilek>1:    print(k)k=k/2A.1000 B.10C.11D.9解析:    按照题意每次循环K/2,直到K值小于等

  2. ruby-on-rails - Rails for Zombies Lab 4 > 练习 3 - 2

    我在第三个练习中停留在第四个RailsforZombies实验室。这是我的任务:创建将创建新僵尸的操作,然后重定向到创建的僵尸的显示页面。我有以下参数数组:params={:zombie=>{:name=>"Greg",:graveyard=>"TBA"}}我写了下面的代码作为解决方案:defcreate@zombie=Zombie.create@zombie.name=params[:zombie[:name]]@zombie.graveyard=params[:zombie[:graveyard]]@zombie.saveredirect_to(create_zombie_path

  3. javascript - Eloquent JavaScript 2nd Edition 递归练习解答 - 2

    我试图解决在线书籍eloquentjavascript2ndedition的递归练习:问题是这样的:We’veseenthat%(theremainderoperator)canbeusedtotestwhetheranumberisevenoroddbyusing%2tocheckifit’sdivisiblebytwo.Here’sanotherwaytodefinewhethera(positive,whole)numberisevenorodd:Zeroiseven.Oneisodd.ForanyothernumberN,itsevennessisthesameasN-2.De

  4. javascript - 对javascript练习的困惑 - 2

    我刚拿到DouglasCrockford的Javascript:TheGoodParts,我在理解他关于原型(prototype)的示例之一时遇到了一些困难。书中代码如下:if(typeofObject.create!=="function"){Object.create=function(o){varF=function(){}F.prototype=o;returnnewF;};}我假设此代码用于定位函数的原型(prototype)。但为什么要使用如此复杂的方法呢?为什么不直接使用variable.prototype?Crockford是Javascript方面的领先专家,因此我确

  5. javascript - 练习 Javascript 的最佳环境 - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭11年前。我目前有Notepad++和AptanaStudio。是否有任何其他开发环境可以简化javascript代码的编写?谢谢。

  6. Javascript 练习 - 反转二维数组 - 2

    反转二维数组的值,可以扩展n次。[1,[2,[3,...[n,null]]]]给定:所有数组的长度始终为2列表中的最后一个数组将包含一个null索引1示例:[1,[2,[3,null]]]将输出[3,[2,[1,null]]][1,[2,[3,[4,null]]]]会输出[4,[3,[2,[1,null]]]]我不确定我描述的是否正确,但我今天遇到了这个练习并想出了一个相当明显的解决方案。varars=[1,[2,[3,null]]],rev=null;functionr(x){rev=(rev==null)?[x[0]]:[x[0],rev];if(x[1]!==null)r(x[1

  7. 华为eNSP网络配置综合练习一(vlan +MSTP+VLANif+VRRP+ 静态路由+单臂路由+STP+BFD) - 2

    综合练习一题目要求:实验范图实现PC机之间互通配置思路:配置过程:配置终端设备及3700交换机实现此案例需要按照如下步骤进行。1)配置PC的IP地址和网关2)配置SW1/5/6的vlan为10/20/30,交换机之间的链路为Trunk,与PC间为Access3)配置SW2/3/7的vlan为40/50,交换机之间的链路为Trunk,与PC间为Access4)配置SW4/8/9的vlan为60/70/80,交换机之间的链路为Trunk,与PC间为Access5)配置R1/R2/R3的接口IP地址6)配置每个VLAN的网关接口IP地址SW1为vlan10/20/30的网关设备:interfacev

  8. 智能合约学习笔记一 、——{Solidity语言详解——(1—2)小练习} - 2

    1.要求:1.根据提示,在指定位置写出编译版本,要求使用^符号,版本要求在0.6.0及以上。2.根据提示,在指定位置写出所定义的合约名称。3.为了查看程序的效果,我们使用在线Solidity开发工具RemixIDE编译和运行Solidity程序。中文在线版:在浏览器打开下方链接: Remix-中文版-智谷星图。第1步–在文件浏览器选项卡下,新建一个Firstapp.sol文件,把我们补充完整的代码直接复制过来。第2步–在SOLIDITY编译器选项卡下,选择0.6.5的那个编译器版本并单击 编译Firstapp.sol 按钮,开始编译。编译成功后会根据本地客户端和版本内容弹出提示,可以不用处理。

  9. javascript - 我需要一些初级程序员的简单逻辑/编程练习 - 2

    关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于StackOverflow来说是偏离主题的,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,describetheproblem以及迄今为止为解决该问题所做的工作。关闭9年前。Improvethisquestion我目前正在教员工ECMA脚本,因为维护我们使用的工作流系统需要它,我需要一些挑战作为练习。我们已经涵盖了大部分语言,他现在非常熟悉语法,所以我只需要他开始使用它。我需要给他提供练习,让他进行逻辑思考。例如,他了解什么是if和switch

  10. 软件测试Selenium-API 操作(上机练习文档)分享 - 2

    目录目标一、元素定位目标1. 如何进行元素定位?2. 浏览器开发者工具2.1 如何使用浏览器开发者工具3. 元素定位方式3.1 id 定位3.2 name 定位3.3 class_name 定位3.4 tag_name 定位3.5 link_text 定位3.6 partial_link_text 定位4. 定位一组元素 4.1 find_elements_by_xxx()4.2 案例4.3 示例代码二、XPath、CSS 定位目标为什么要学习XPath、CSS 定位?1. 什么是XPath?2. XPath 定位策略(方式)2.1 路径定位(绝对路径、相对路径)2.2 利用元素属性2.3 属

随机推荐