当前位置: 首页 > news >正文

SQL Zoo 8.Using Null

以下数据均来自SQL Zoo

1.List the teachers who have NULL for their department.(列出所属部门为NULL的教师)

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.(INNER JOIN遗漏了没有部门的教师和没有教师的部门)

SELECT teacher.name, dept.nameFROM teacher INNER JOIN deptON (teacher.dept=dept.id)

3.Use a different JOIN so that all teachers are listed.(使用不同的JOIN,以便列出所有教师)

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

4.Use a different JOIN so that all departments are listed.(使用不同的JOIN,以便列出所有部门)

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

5.Use COALESCE to print the mobile number. Show teacher name and mobile number or '07986 444 2266'.(使用COALESCE打印手机号码。显示老师的姓名和手机号码或'07986 444 2266')

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

注:coalesce替换结果集中的 NULL 值

6.Use the COALESCE function and a LEFT JOIN to print the teacher name and department name. (使用COALESCE函数和LEFT JOIN来打印教师姓名和系名)

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

7.Use COUNT to show the number of teachers and the number of mobile phones.(使用COUNT显示教师数量和手机数量)

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

8.Use COUNT and GROUP BY dept.name to show each department and the number of staff. (使用COUNT和GROUP BY department .name显示每个部门和员工人数)

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

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.(用例显示每个老师的名字,如果老师是在1或2部门,后面跟着“Sci”,否则是“Art”)

select name,if(dept = 1 or dept = 2,'Sci','Art') 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.(用例显示每个教师的姓名,如果教师在部门1或2后面跟着'Sci',如果教师的部门是3则显示'Art',否则显示'None')

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

http://www.lryc.cn/news/421509.html

相关文章:

  • LeetCode274. H 指数
  • 概述:Dubbo、Nacos、 Zookeeper 等分布式服务协调与治理等技术
  • 【LINUX】小工具降耦合,全内核函数插入宏摸索测试中。。
  • 24/8/12算法笔记 复习_线性回归
  • Linux系统驱动(十四)输入子系统
  • 力扣(2024.08.12)
  • 最新版的AutoGPT,我搭建好了
  • [SWPUCTF 2021 新生赛]PseudoProtocols(构造伪协议)
  • 基于STM32开发的智能语音助手系统
  • 基于python的图像去雾算法研究系统设计与实现
  • 自定义 View 可以播放一段视频
  • LVS负载均衡集群部署之—NAT模式的介绍及搭建步骤
  • 【算法】浅析哈希算法【附代码示例】
  • 2024.8.12
  • 使用Python解析pdf、docx等格式文件。
  • Linux网络通信基础API
  • Python爬虫:下载4K壁纸
  • 2024年【北京市安全员-B证】新版试题及北京市安全员-B证免费试题
  • python爬取B站视频实验
  • 10步搞定Python爬虫从零到精通!
  • SpringMVC学习笔记---带你快速入门和复习
  • Linux系统编程 day09 线程同步
  • Vue快速入门(四)——Vue3及组合式API(一)
  • vue项目名修改、webstorm和idea创建的项目重命名、重构项目、修改项目名称
  • 【MySQL】数据库约束和多表查询
  • 抖店飞鸽客服自动回复软件开发教程与下载体验(.NET版)
  • 如何关闭redis的自动清理缓存,声明式事务(含有redis)如何解决,redis setnx锁的使用。
  • C#中抽象类的使用
  • 揭秘网络攻击:深入理解JavaScript中的跨站点请求伪造(CSRF)
  • 【项目实战】C++视频共享点播系统