SQLZOO:Using Null

ops/2024/9/24 7:25:06/

数据表:teacher-dept

teacher
iddeptnamephonemobile
1011Shrivell275307986 555 1234
1021Throd275407122 555 1920
1031Splint2293
104Spiregrain3287
1052Cutflower321207996 555 6574
106Deadyawn3345
...
dept
idname
1Computing
2Design
3Engineering
...

Q1

List the teachers who have NULL for their department.

sql">SELECT name FROM teacher WHERE dept IS NULL
IS NULL | IS NOT NULL

Q2

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

sql">SELECT teacher.name,dept.name 
FROM teacher INNER JOIN dept ON (teacher.dept=dept.id)
INNER JOIN

INNER JOIN 关键字在表中存在至少一个匹配时返回行

只返回那些在两个表中都有匹配的记录

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

参数说明:

  • columns:要显示的列名
  • table1:表1的名称
  • table2:表2的名称
  • column_name:表中用于连接的列名

注释:INNER JOIN 与 JOIN 是相同的

Q3

Use a different JOIN so that all teachers are listed.

sql">SELECT teacher.name,dept.name 
FROM teacher LEFT JOIN dept ON (teacher.dept=dept.id)
LEFT JOIN

LEFT JOIN 关键字从左表返回所有的行,即使右表中没有匹配

如果右表中没有匹配,则结果为 NULL

Q4

Use a different JOIN so that all departments are listed.

sql">SELECT teacher.name,dept.name 
FROM teacher RIGHT JOIN dept ON (teacher.dept=dept.id)
RIGHT JOIN

RIGHT JOIN 关键字从右表返回所有的行,即使左表中没有匹配

如果左表中没有匹配,则结果为 NULL


Using the COALESCE function

Q5

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'

sql">SELECT name,COALESCE(mobile,'07986 444 2266') FROM teacher

COALESCE

COALESCE接受任意数量参数并返回第一个非空值

  COALESCE(x,y,z) = x if x is not NULLCOALESCE(x,y,z) = y if x is NULL and y is not NULLCOALESCE(x,y,z) = z if x and y are NULL but z is not NULLCOALESCE(x,y,z) = NULL if x and y and z are all NULL

Q6

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.

sql">SELECT teacher.name,COALESCE(dept.name,'None') 
FROM teacher LEFT JOIN dept ON teacher.dept=dept.id

Q7

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

sql">SELECT COUNT(name),COUNT(mobile) FROM teacher

Q8

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.

sql">SELECT dept.name,COUNT(teacher.name) 
FROM teacher RIGHT JOIN dept ON teacher.dept=dept.id 
GROUP BY dept.name


Using CASE

CASE

CASE语句遍历条件并在满足第一个条件时返回一个值

一旦条件为真,它将停止读取并返回结果

如果没有条件为 true,则返回 ELSE 子句中的值

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END;


Q9

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

sql">SELECT name,CASE WHEN dept IN (1,2) THEN 'Sci'ELSE 'Art'END
FROM teacher

Q10

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.

 

sql">SELECT name,CASE WHEN dept IN (1,2) THEN 'Sci'WHEN dept=3 THEN 'Art'ELSE 'None'END
FROM teacher


http://www.ppmy.cn/ops/40267.html

相关文章

经典的设计模式和Python示例(一)

目录 一、工厂模式(Factory Pattern) 二、单例模式(Singleton Pattern) 三、观察者模式(Observer Pattern) 一、工厂模式(Factory Pattern) 工厂模式(Factory Pattern…

uniapp引用第三方组件样式无法穿透

在通过uniapp编写小程序过程中发现,引用第三方组件库的样式无法穿透修改。微信小程序文档也给出对应的解决思路自定义组件样式穿透 组件样式隔离 默认情况下,自定义组件的样式只受到自定义组件 wxss 的影响。除非以下两种情况: 指定特殊的…

云计算的优势与未来发展

随着数字化转型的蓬勃发展,云计算作为信息技术应用的基础设施,逐渐成为企业的首选。云计算以其诸多优势和未来发展趋势,为企业带来了更高效、灵活和创新的IT解决方案,助力企业实现数字化转型和业务发展。 云计算的优势 首先&…

81.网络游戏逆向分析与漏洞攻防-移动系统分析-飞天遁地的实现与面向计算

免责声明:内容仅供学习参考,请合法利用知识,禁止进行违法犯罪活动! 如果看不懂、不知道现在做的什么,那就跟着做完看效果,代码看不懂是正常的,只要会抄就行,抄着抄着就能懂了 内容…

Oracle 修改数据库的字符集

Oracle 修改数据库的字符集 alter system enable restricted session; alter database "cata" character set ZHS16CGB231280; alter database "cata" national character set ZHS16CGB231280; alter system enable restricted session; alter database…

渗透之sql注入----二次注入

目录 二次注入的原理: 实战: 第一步:找注入点 找漏洞: 注入大概过程: 第二步:开始注入 二次注入的原理: 二次注入是由于对用户输入的数据过滤不严谨,导致存在异常的数据被出入…

MySQL运维总结

以下是个人工作中用到的mysql运维总结。 基本运维命令 看下死锁的语句:show engine innodb status \G; 修改最大连接数:set global max_connections1400; 使用profile查询sql执行耗时: 1、set profiling 1 ; 启用profile , session级别的配…

Qt宏和关键字

Q_GADGET 这个宏是Q_OBJECT宏的弱化版本吧,不过它可以继续 使用属性机制、反射等特性,但是不能使用信号与槽QT_INCLUDE_COMPAT 兼容版本QT_BEGIN_NAMESPACE 防止命名污染Q_GUI_EXPORT 为了实现跨平台以及插件Q_FLAG(RenderHint),Q_DECLARE_FL…