wonderful-sql 作业

Sql 作业

作业1:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vqZ3C6jP-1691141831237)(Sql 作业.assets/image-20230802160920037.png)]

答:

create table Employee
(Id integer not null,
Name varchar(32) ,
Salary integer,
departmentId integer,
primary key (Id)
);create table Department(
Id integer primary key,
Name varchar(30) not null
);insert into employee values (1,"Joe",70000,1);
insert into employee values (2,"Henry",80000,2);
insert into employee values (3,"Sam",60000,2);
insert into employee values (4,"Max",90000,1);
insert into department values(1,"IT");
insert into department values(2,"Sales");SELECT departmentId,  (SELECT Name FROM employeeWHERE departmentId = e.departmentId AND salary = MAX(e.salary)
) AS Employee,MAX(salary) AS max_salary
FROM employee e
GROUP BY departmentId

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7KndS7ds-1691141831238)(Sql 作业.assets/image-20230804092217531.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-17BJfyBJ-1691141831238)(Sql 作业.assets/image-20230804092225469.png)]

答:

create table seat(
id int primary key,
Name varchar(30) not null
);
insert into seat values(1,"Abbot");
insert into seat values(2,"Doris");
insert into seat values(3,"Emerson");
insert into seat values(4,"Green");
insert into seat values(5,"Jeames");
select * from seat;SELECT(CASE WHEN MOD(id,2)=1 AND id=(SELECT COUNT(id) FROM seat) THEN idWHEN MOD(id,2)=0 THEN id-1ELSE id+1END
) AS id,name
FROM seat
ORDER BY id;

解决相关方法:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yT05u3Wg-1691141831240)(Sql 作业.assets/image-20230804101845265.png)]

答:

create table score(
class int primary key,
score_avg int 
);
insert into score values(1,93);
insert into score values(2,93);
insert into score values(3,93);
insert into score values(4,91);
select class,score_avg,
rank() over (order by score_avg desc) as rank1,
dense_rank() over (order by score_avg desc) as rank2,
row_number() over(order by score_avg desc) as rank3
from score;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SWrD1UBj-1691141831241)(Sql 作业.assets/image-20230804114101982.png)]

答:

create table number(
id int primary key,
Num int
);
insert into number values(1,1); 
insert into number values(2,1);
insert into number values(3,1);
insert into number values(4,2);
insert into number values(5,1);
insert into number values(6,2);
insert into number values(7,2);
select * from number;
select distinct num as ConsecutiveNums
from (select num,if(@prev_num = num,@count:=@count+1,@count:=1)as consecutive_count, @prev_num:=numfrom numbercross join(select @count:=0,@prev_num:=NULL) as varsorder by id
)as t
where consecutive_count>=3

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NLUtMolU-1691141831242)(Sql 作业.assets/image-20230804114118880.png)]
在这里插入图片描述

答:

create table tree(
id int primary key,
p_id int
);
insert into tree values(1,null);
insert into tree values(2,1);
insert into tree values(3,1);
insert into tree values(4,2);
insert into tree values(5,2);
select * from tree;
select id , casewhen p_id is null then "Root"when id in (select p_id from tree) then "Inner"else "Leaf"end as Typefrom tree;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W1YkJBYP-1691141831243)(Sql 作业.assets/image-20230804151012952.png)]

答:

create table Employee2(
Id int primary key,
name varchar(50),
Department varchar(50),
ManagerId int
);
insert into employee2 values(101,"John","A",null);
insert into employee2 values(102,"Dan","A",101);
insert into employee2 values(103,"James","A",101);
insert into employee2 values(104,"Amy","A",101);
insert into employee2 values(105,"Anne","A",101);
insert into employee2 values(106,"Ron","B",101);
select * from employee2;
select name
from (select ManagerId , count(ManagerId) as M_count
from employee2
group by ManagerId
) as ManagerCount , employee2
where ManagerCount.M_count = 5 and ManagerCount.ManagerId = employee2.Id

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UK1eXmVC-1691141831244)(Sql 作业.assets/image-20230804163219597.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ldo6XIsO-1691141831244)(Sql 作业.assets/image-20230804163229875.png)]

答:

drop table if exists survey_log;
create table survey_log(
uid int ,
action varchar(20) check(action = "show" or action="answer" or action = "skip"),
question_id int,
answer_id int,
q_num int,
timestamp timestamp
);
insert into survey_log values(5,"show",285,null,1,123);
insert into survey_log values(5,"answer",285,124124,1,124);
insert into survey_log values(5,"show",369,null,2,125);
insert into survey_log values(5,"skip",369,null,2,126);
select * from survey_log;
select * from survey_log 
where action = "show" or action = "answer";
select question_id, count(*) as show1 from  survey_log
where action = "show"
group by question_id;select count_answer.question_id,max(answer/show1)
from
(select question_id, count(*) as show1 from  survey_log
where action = "show"
group by question_id) as count_show,
(select question_id, count(*) as answer from  survey_log
where action = "answer"
group by question_id) as count_answer
where count_answer.question_id  = count_show.question_id
group by count_answer.question_id

在这里插入图片描述
答:

select * from employee;
select * from department;
insert into employee value(5,"Janet",69000,1);
insert into employee value(6,"Randy",85000,1);
select department.Name as department,rank_employee.name as Employee,Salary
from (select name,salary,departmentId,
row_number() over (partition by departmentId order by salary desc) as rank1
from employee) 
as rank_employee,departmentwhere rank1 <=3 and department.id = rank_employee.departmentId

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/61149.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STM32基于CubeIDE和HAL库 基础入门学习笔记:物联网项目开发流程和思路

文章目录&#xff1a; 第一部分&#xff1a;项目开始前的计划与准备 1.项目策划和开发规范 1.1 项目要求文档 1.2 技术实现文档 1.3 开发规范 2.创建项目工程与日志 第二部分&#xff1a;调通硬件电路与驱动程序 第三部分&#xff1a;编写最基础的应用程序 第四部分&…

Docker desktop使用配置

1. 下载安装 https://www.docker.com/ 官网下载并安装doker desktop 2. 配置镜像 &#xff08;1&#xff09;首先去阿里云网站上进行注册&#xff1a;https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors &#xff08;2&#xff09;注册完成后搜索&#xff1a;容…

java泛型和通配符的使用

泛型机制 本质是参数化类型(与方法的形式参数比较&#xff0c;方法是参数化对象)。 优势:将类型检查由运行期提前到编译期。减少了很多错误。 泛型是jdk5.0的新特性。 集合中使用泛型 总结&#xff1a; ① 集合接口或集合类在jdk5.0时都修改为带泛型的结构② 在实例化集合类时…

MySQL8的特性-MySQL8知识详解

MySQL是一个多用户、多线程的SQL数据库服务器。SQL&#xff08;结构化查询语言&#xff09;是世界上最流行和标准化的数据库语言。下面是MySQL的特性。 1、开源性&#xff1a;MySQL是一个开源的关系型数据库管理系统&#xff0c;可以免费使用和修改。 2、可靠性&#xff1a;M…

MySQL—日志

这里写目录标题 undo logundo log的作用undo log页记录的是什么 Buffer Pool为什么需要Buffer PoolBuffer Pool缓存什么 redo log什么是redo logredo log的作用redo log什么时候刷盘undo和redo的区别 binlogbinlog 作用redo log和binlog区别如果数据数据被删了&#xff0c;能用…

宿舍管理系统--前后端分离式项目架构流程复盘

文章目录 &#x1f412;个人主页&#x1f3c5;JavaEE系列专栏&#x1f4d6;前言&#xff1a;【&#x1f387;前端】先创建Vue-cli项目&#xff08;版本2.6.10&#xff0c;仅包含babel&#xff09;&#xff0c;请选择此项目并创建 【整理简化项目模板】【&#x1f380;创建路由】…

Gradio入门,并搭个鸡兔同笼问题小应用,附源码(MindOpt)

应用链接&#xff1a; https://979427749bc9ceec34.gradio.live 是公开访问链接&#xff0c;3天有效。在modelscope中的创空间怎么长期发布我还在研究。后面补上。 应用图如下&#xff0c;源代码见正文。 知道Gradio AI大模型快速生成应用的工具&#xff0c; 在Huggingface …

week3

题解: 前序遍历性质&#xff1a; 节点按照 [ 根节点 | 左子树 | 右子树 ] 排序。 中序遍历性质&#xff1a; 节点按照 [ 左子树 | 根节点 | 右子树 ] 排序。 通过以上三步&#xff0c;可确定 三个节点 &#xff1a;1.树的根节点、2.左子树根节点、3.右子树根节点。 之后进行…

通讯协议035——全网独有的OPC HDA知识一之聚合(四)平均值

本文简单介绍OPC HDA规范的基本概念&#xff0c;更多通信资源请登录网信智汇(wangxinzhihui.com)。 本节旨在详细说明HDA聚合的要求和性能。其目的是使HDA聚合标准化&#xff0c;以便HDA客户端能够可靠地预测聚合计算的结果并理解其含义。如果用户需要聚合中的自定义功能&…

五、web应用程序技术——web功能

文章目录 一、服务器端功能1.1 SQL1.2 XML1.3 web服务 二、客户端功能2.1 HTML2.2 超链接2.3 表单2.4 CSS2.5 JavaScript2.6 文档对象模型2.7 Ajax2.8 JSON2.9 同源策略2.10浏览器拓展技术 一、服务器端功能 早期的web站点由各种静态资源组成&#xff0c;如HTML页面与图片。当用…

【Linux】线程的概念以及与进程的区别

目录 背景知识 什么是线程&#xff1f; 进程和线程的区别 线程的优缺点 背景知识 在了解线程前&#xff0c;我们要首先知道&#xff0c;OS是可以做到让进程进行细粒度划分的! 比如我们所说的进程地址空间中的堆区&#xff0c;它在进程PCB中的mm_structz中有一个start和…

62.不同路径

一个机器人位于一个 m x n 网格的左上角 &#xff08;起始点在下图中标记为 “Start” &#xff09;。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角&#xff08;在下图中标记为 “Finish” &#xff09;。 问总共有多少条不同的路径&#xff1f; 动态规…