(三) 通用试题——SQL相关

数据库花式查询语句题目

-- 查询当年及以前年份的累计薪资, 如2000年开始, 2000年对应查询出2000年的薪资,
-- 2001年对应查询出2000年与2001年薪资之和, 2002查询出00,01,02三年的薪资之和
-- 方法1
select b.year 年份, sum(a.salary) 累计薪资
from salary as a, salary as b
where a.year<=b.year
group by b.year;
-- 方法2
select a.year,
(select sum(b.salary) from salary as b where b.year<=a.year)
from salary as a;

select b., "", a.
from salary as a, salary as b
where a.year<=b.year
order by b.year;

-- 查询出不同年份组合 如00-01 00-02 00-03 01-02...
select a.year 1队, b.year 2队
from salary a, salary b
where a.year<b.year;

-- 查询出所有月份销售额都比A同学相应月份销售额高的科目???
select a.*
from TestDB a,
(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b
where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur

/*
从(yy年份, mm月份, amount数值)
查成这样一个结果
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
*/
select ymca.yy,
(select a1.amount from ymca as a1 where a1.yy=ymca.yy and a1.mm=1) m1,
(select a2.amount from ymca as a2 where a2.yy=ymca.yy and a2.mm=2) m2,
(select a3.amount from ymca as a3 where a3.yy=ymca.yy and a3.mm=3) m3,
(select a4.amount from ymca as a4 where a4.yy=ymca.yy and a4.mm=4) m4
from ymca
group by ymca.yy;

-- 分组查询

-- 查询各天比赛胜负数量
select win.day, win.win, lose.lose
from
(select count(1) win, day from champion as chmp2 where chmp2.res='胜' group by chmp2.day) win
inner join
(select count(1) lose, day from champion as chmp2 where chmp2.res='败' group by chmp2.day) lose
on
win.day= lose.day;

-- 方法2
select win.day, win.win win, count(chmp1.day)-win.win
from
champion as chmp1 inner join
(select count(1) win, day from champion as chmp2 where chmp2.res='胜' group by chmp2.day) win
on
win.day= chmp1.day
group by chmp1.day;

-- 方法3
select day,
sum(case when chmp.res='胜' then 1 else 0 end) win,
sum(case when chmp.res!='胜' then 1 else 0 end) lose
from champion as chmp
group by chmp.day;

-- 表中有A B C三列, 用SQL语句实现:当A列大于B列时选择A列否则选择B列, 当B列大于C列时选择B列否则选择C列


select (case when a>b then a else b end ),
(case when b>c then b esle c end)
from table_name;

select curdate();

select * from era where datediff(era.date, curdate())=0;

-- sql语句执行顺序

-- 查询各班平均成绩大于80分的学生数量和班级号
-- 1
select class, count(scoretmp.sname)
from stu inner join
(select sname
from score group by score.sname
having avg(score.score)>80
) as scoretmp
on stu.sname=scoretmp.sname
group by class
-- 2

-- ?
-- 查询各班平均分大于80分的人名和平均分
-- 1
select class, stu.sname, scoretmp.scoreavg
from stu inner join
(select sname, avg(score.score) as scoreavg from score
-- where score.score is not null
group by score.sname
having avg(score.score)>80
) as scoretmp
on stu.sname=scoretmp.sname
-- 2
select class, stu.sname, avg(score.score)
from stu inner join score
on stu.sname=score.sname
group by score.sname
having avg(score.score)>80

--
select * from score;
-- 查询英语成绩比语文成绩好的学生
select sname,
sum(case when score.course='语文' then score.score end) Chinese,
sum(case when score.course='英语' then score.score end) English
from score
group by score.sname
having Chinese<English;
-- failed
select score1.sname, score1.score ch, score2.score en
from (select * from score where score.course='语文') score1,
(select * from score where score.course='英语') score2
where score1.sname=score2.sname
and score1.score < score2.score

-- 查询选修了某两门课的学生
select sname
from score
where score.course='语文'
and score.sname
in (select sname from score where score.course='物理');

select sname
from score
where score.course='语文'
and exists
(select sname from score as sc2 where sc2.sname=score.sname and sc2.course='物理')

--查没选所有课程的学生
select sname from
score group by sname
having count(course)<(select count(distinct course) from score)

SQL查询-职员-部门emp-dept表查询
-- 创建员工信息表
CREATE TABLE emp (
empno varchar(10) NOT NULL,
ename varchar(10) DEFAULT NULL,
job varchar(10) DEFAULT NULL,
mgr varchar(10) DEFAULT NULL,
sal varchar(10) DEFAULT NULL,
deptno varchar(10) DEFAULT NULL,
PRIMARY KEY (empno)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 创建部门信息表
CREATE TABLE dept (
deptno varchar(10) NOT NULL,
dname varchar(10) DEFAULT NULL,
PRIMARY KEY (deptno)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- 转存表中的数据 dept
--
INSERT INTO dept (deptno, dname) VALUES
('1', '事业部'),
('2', '销售部'),
('3', '技术部');

--
-- 转存表中的数据 emp
--

INSERT INTO emp (empno, ename, job, mgr, sal, deptno) VALUES
('01', 'jacky', 'clerk', 'tom', '1000', '1'),
('02', 'tom', 'clerk', 'tom', '2000', '1'),
('03', 'jenny', 'sales', 'pretty', '1000', '2'),
('04', 'pretty', 'sales', '', '800', '2'),
('05', 'buddy', 'jishu', 'canndy', '1000', '3'),
('06', 'canddy', 'jishu', '', '1000', '3'),
('07', 'biddy', 'clerk', 'tom', '2000', '1');

-- 1.查询各部门中薪资最低和最高的数据,包括的字段有部门编号、部门名称、员工名称、最高薪资、最第薪资.
select dept.deptno, dept.dname, max(emp.sal) 最高薪水, min(emp.sal) 最低薪水
from dept inner join emp on dept.deptno=emp.deptno
group by dept.deptno;

-- 2.查询出各部门中job为CLERK的最高薪资和最低薪资
select dept.deptno, dept.dname, max(emp.sal)最高薪水, min(emp.sal) 最低薪水
from dept inner join emp on dept.deptno=emp.deptno
where emp.job='clerk'
group by dept.deptno;

-- 3.查询出emp表中最低薪资小于2000的部门且job为clerk的部门编号、最低薪资和最高薪资.
select
emp.deptno, min(emp.sal)最低薪资, max(emp.sal) 最高薪资
from emp inner join dept
on emp.deptno=dept.deptno
where emp.job = 'clerk'
group by dept.deptno
having min(emp.sal)<2000;

-- 4.查询出emp表中薪资小于2000的 且job为clerk的部门编号、最低薪资和最高薪资.
select dept.deptno, dept.dname, max(emp.sal)最高薪水, min(emp.sal) 最低薪水
from dept inner join emp on dept.deptno=emp.deptno
where emp.job='clerk' and emp.sal<2000
group by dept.deptno;

-- 5.查询出emp表中员工编号、员工名称、员工薪资并按照薪资由低到高、编号有高到低的顺序进行排序.
select emp.empno, emp.ename, emp.sal
from emp
order by emp.sal, emp.empno;

-- 6.查询出emp中名字为buddy的所在部门编号以及该部门所有的员工,只查询部门编号与员工名词.
select emp.deptno, emp.ename
from emp where
emp.deptno=(select emp.deptno from emp where emp.ename='buddy');

-- 7.查询emp中job为clerk的员工信息.
select *
from emp where emp.job='clerk';

-- 8.查询emp中员工有管理者的员工姓名、管理者的信息
select emp.ename, emp.mgr from emp
where emp.mgr!='' and emp.mgr is not null;

-- 9.查询出emp表中,部门名称、部门编号并且工作内容为clerk的员工名称和员工岗位.
select emp.empno, dept.dname, emp.ename, emp.job from emp, dept
where emp.job='clerk' and emp.deptno=dept.deptno;

-- 10.查询本部门中,高于平均薪资的员工的员工编号、员工名称、部门名称、员工工资
select emp.empno empno, emp.ename ename, dept.dname dname, emp.sal sal
from dept inner join emp on dept.deptno=emp.deptno
where sal> (select avg(emp2.sal) from emp as emp2 where emp2.deptno = emp.deptno)
order by dname;

-- 11.对于emp中工资高于本部门平均水平,人数多于1人的,列出部门号,人数,平均工资,按部门号排序.
select count(emp.empno) 全部员工数量, emp.deptno 部门编号, avg(emp.sal) 平均工资
from emp
where
(
select count(emp2.empno) from emp as emp2
where emp2.deptno=emp.deptno
and emp2.sal>(
select avg(sal) from emp as emp3
where emp2.deptno=emp3.deptno
)
)>1
group by emp.deptno
order by emp.deptno;

-- 12.查询工资高于本部门平均水平的员工,列出部门号,姓名,工资,按部门号排序
select emp.empno empno, emp.ename ename, dept.dname dname, emp.sal sal
from dept inner join emp on dept.deptno=emp.deptno
where sal> (select avg(emp2.sal) from emp as emp2 where emp2.deptno = emp.deptno)
order by dname;

-- 13.查询出各个部门中工资高于本部门平均工资的员工数和部门号,按照部门号进行排序.
select empAllNum-count(1) 高于平均工资人数, emp.deptno 部门号
from
emp inner join
(select dept1.deptno deptNo, avg(emp1.sal) deptAvg, count(1) empAllNum
from dept as dept1 inner join emp as emp1 on dept1.deptno=emp1.deptno
group by dept1.deptno) as dept2
on emp.deptno = dept2.deptNo
where emp.sal<dept2.deptAvg
group by emp.deptno
order by emp.deptno;

;
;
;
;
;
;

全部评论

相关推荐

03-18 09:45
莆田学院 golang
牛客749342647号:佬,你这个简历模板是哪个,好好看
点赞 评论 收藏
分享
03-15 14:55
已编辑
门头沟学院 golang
bg:双非学院本&nbsp;ACM银&nbsp;go选手timeline:3.1号开始暑期投递3.7号第二家公司离职顽岩科技&nbsp;ai服务中台方向&nbsp;笔试➕两轮面试,二面挂(钱真的好多😭)厦门纳克希科技&nbsp;搞AI的,一面OC猎豹移动&nbsp;搞AIGC方向&nbsp;一面OC北京七牛云&nbsp;搞AI接口方向&nbsp;一面OC上海古德猫宁&nbsp;搞AIGC方向&nbsp;二面OC上海简文&nbsp;面试撞了直接拒深圳图灵&nbsp;搞AIGC方向一面后无消息懒得问了,面试官当场反馈不错其他小厂没记,通过率80%,小厂杀手😂北京字节&nbsp;具体业务不方便透露也是AIGC后端方向2.28约面&nbsp;(不知道怎么捞的我,我也没在别的地方投过字节简历哇)3.6一面&nbsp;一小时&nbsp;半小时拷打简历(主要是AIGC部分)剩余半小时两个看代码猜结果(经典go问题)➕合并二叉树(秒a,但是造case造了10分钟哈哈)一天后约二面3.12&nbsp;二面,让我挑简历上两个亮点说,主要说的docker容器生命周期管理和raft协议使用二分法优化新任leader上任后与follower同步时间。跟面试官有共鸣,面试官还问我docker底层cpu隔离原理和是否知道虚拟显存。之后一道easy算法,(o1空间解决&nbsp;给定字符串含有{和}是否合法)秒a,之后进阶版如何用10台机加快构建,想五分钟后a出来。面试官以为45分钟面试时间,留了18分钟让我跟他随便聊,后面考了linux&nbsp;top和free的部分数据说什么意思(专业对口了只能说,但是当时没答很好)。因为当时手里有7牛云offer,跟面试官说能否快点面试,马上另外一家时间到了。10分钟后约hr面3.13,上午hr面,下午走完流程offer到手3.14腾讯技术运营约面,想直接拒😂感受:&nbsp;因为有AIGC经验所以特别受AI初创公司青睐,AIGC后端感觉竞争很小(指今年),全是简历拷打,基本没有人问我八股(八股吟唱被打断.jpeg),学的东西比较广的同时也能纵向深挖学习,也运气比较好了哈哈可能出于性格原因,没有走主流Java路线,也没有去主动跟着课写项目,项目都是自己研究和写的哈哈
烤点老白薯:你根本不是典型学院本的那种人,贵了你这能力
查看7道真题和解析
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务