sql面试题

create table `course` (
    `id` int (11),
    `name` varchar (192),
    `teacher` int (11)
); 
insert into `course` (`id`, `name`, `teacher`) values('1','语文','1');
insert into `course` (`id`, `name`, `teacher`) values('2','数学','2');
insert into `course` (`id`, `name`, `teacher`) values('3','英语','3');
insert into `course` (`id`, `name`, `teacher`) values('4','物理','4');
create table `score` (
    `id` bigint (20),
    `stu_id` int (11),
    `course` int (64),
    `achievement` int (11)
); 
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('1','1','1','12');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('2','1','2','76');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('3','1','3','86');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('4','1','4','96');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('5','2','1','34');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('6','2','2','86');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('7','2','3','46');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('8','2','4','84');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('10','3','1','45');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('11','3','2','57');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('12','3','3','45');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('13','3','4','45');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('14','4','1','46');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('15','4','2','38');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('16','4','3','67');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('17','4','4','96');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('18','5','1','34');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('19','5','2','96');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('20','5','3','46');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('21','5','4','95');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('23','6','4','46');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('24','7','2','35');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('25','7','3','89');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('26','8','1','56');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('27','8','2','79');
insert into `score` (`id`, `stu_id`, `course`, `achievement`) values('28','8','3','45');
create table `student` (
    `id` bigint (20),
    `name` varchar (192),
    `birth` varchar (96),
    `sex` varchar (6)
); 
insert into `student` (`id`, `name`, `birth`, `sex`) values('1','小红','1991-11','男');
insert into `student` (`id`, `name`, `birth`, `sex`) values('2','小明','1992-3','男');
insert into `student` (`id`, `name`, `birth`, `sex`) values('3','小张','1991-4','女');
insert into `student` (`id`, `name`, `birth`, `sex`) values('4','小吴','1991-5','女');
insert into `student` (`id`, `name`, `birth`, `sex`) values('5','小张','1991-2','男');
insert into `student` (`id`, `name`, `birth`, `sex`) values('6','aa','1998-2',NULL);
insert into `student` (`id`, `name`, `birth`, `sex`) values('7','bb','1991-2',NULL);
insert into `student` (`id`, `name`, `birth`, `sex`) values('8','cc',NULL,NULL);
insert into `student` (`id`, `name`, `birth`, `sex`) values('9','dd',NULL,NULL);
insert into `student` (`id`, `name`, `birth`, `sex`) values('10','ee',NULL,NULL);

create table `teacher` (
    `id` int (11),
    `name` varchar (192)
); 
insert into `teacher` (`id`, `name`) values('1','1老师');
insert into `teacher` (`id`, `name`) values('2','2老师');
insert into `teacher` (`id`, `name`) values('3','3老师');
insert into `teacher` (`id`, `name`) values('4','4老师 ');

查询出每门课程的及格人数 where必须写在GROUP BY 前面

SELECT course,COUNT(stu_id) FROM score WHERE achievement >=60 GROUP BY course

查询两门以上不及格课程的同学的学号及其平均成绩 group by 后面只能跟having 替代having

having是在前面执行完成后从查询结果级中在查询的,可以使用运算符

SELECT stu_id,AVG(achievement) FROM score WHERE achievement<60 GROUP BY stu_id HAVING COUNT(stu_id) >=2

!!!错误示范!!!查询每门课程分数最高的前2名 错误示范,

当右边的值为空时,内连接不会展示数据 ,

此条sql查询不到第一名的数据

SELECT a.course,a.stu_id,a.achievement FROM score AS a JOIN score AS b ON a.course = b.course AND a.achievement GROUP BY a.course,a.stu_id HAVING COUNT(b.course) <2

正确示范查询每门课程分数最高的前2名

SELECT a.course,a.stu_id,a.achievement FROM score AS a LEFT JOIN score AS b ON a.course = b.course AND a.achievement GROUP BY a.course, a.stu_id
HAVING COUNT(b.course)<2
ORDER BY a.course;

查询出每门课程的及格人数和不及格人数

SELECT course,SUM(CASE WHEN achievement>=60 THEN 1 ELSE 0 END ) AS 及格人数, SUM(CASE WHEN achievement<60 THEN 1 ELSE 0 END ) AS 不及格人数 FROM score GROUP BY course

使用分段[100-80] 优,[80-60] 良,[<60]差 来统计各科成绩,分别统计:各分数段人数,课程号和课程名称

SELECT course.id,course.name, SUM(CASE WHEN score.achievement >=80 THEN 1 ELSE 0 END) AS 优,SUM(CASE WHEN score.achievement <80 AND score.achievement>=60 THEN 1 ELSE 0 END) AS 良,
SUM(CASE WHEN score.achievement <60 THEN 1 ELSE 0 END) AS 差 FROM score JOIN course ON score.course = course.id GROUP BY course.id,course.name


左连接 table1 left join tab2 tab2 on 右边数据为空时也会展示,左边数据为为空就不展示
右连接 table1 right join tab2 on
完全连接:select a.,b. from a full join b on a.id=b.parent_id
左连接、右连接 、完全连接 都是外连接

内连接 table1 join tab2 on table1.xxx = tab2.sss
交叉连接?
SELECT O.ID, O.xxxx, C.ID, C.xx FROM tab1 a cross table2 WHERE a.xxx=1;

count(字段),空字段不会+1
count(id),根据id主键取值,累加返回值,也是server层 “ +1 ”
count(1),同样会遍历,但不取值,引擎告诉不为空那我就 “+1”
count(*),也不取值,而且人家还是经过优化的

https://www.cnblogs.com/yyjie/p/7788413.html
https://zhuanlan.zhihu.com/p/38354000

你可能感兴趣的:(sql面试题)