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

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.lryc.cn/news/111244.html

相关文章:

  • Go学习第六天
  • opencv-34 图像平滑处理-2D 卷积 cv2.filter2D()
  • Java 克隆技术详解,深拷贝与浅拷贝的区别及实现
  • 包装器function
  • Django Rest_Framework(三)
  • 总结 IO、存储、硬盘、文件系统相关常识
  • JavaScript、深入浅出Node.js前端技能汇总
  • use gnustep objective-c
  • 8.15锁的优化
  • 单片机复位电路分析
  • 公文写作技巧:“三面镜子”写作提纲60例
  • useEffect中的函数会执行2次原因
  • 更新k8s环境支付系统支付证书
  • C#的yield
  • 外卖多门店小程序开源版开发
  • 打印图案、
  • # Windows 环境下载 Android 12源码
  • 【运维面试】Docker技术面试题总结
  • CNN成长路:从AlexNet到EfficientNet(01)
  • 使用IDEA操作Mysql数据库
  • ChatGPT下架官方检测工具,承认无法鉴别AI内容
  • Java通过实例调用getClass()方法、类名.class操作、通过运行时类获取其它信息
  • UE5+Paperzd问题
  • K8S系列文章之 自动化运维利器 Ansible
  • Julia 字典和集合
  • devops-发布vue前端项目
  • 使用正则表达式设置强密码
  • epoll、poll、select的原理和区别
  • 【学习笔记】Java安全之反序列化
  • 算法练习--leetcode 数组