美文网首页
sql面试题

sql面试题

作者: 呆呆猿 | 来源:发表于2020-08-29 00:14 被阅读0次
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 <b.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<b.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

相关文章

  • Mysql行转列

    遇到的一个Sql面试题: 建表: Sql: 结果:

  • SQL 基础语法

    http://www.w3school.com.cn/sql/sql_syntax.asp IOS 面试题 htt...

  • 面试官问你 SQL 注入攻击了吗?

    为什么要聊 SQL 注入攻击? 什么是 SQL 注入攻击? 如何进行 SQL 注入攻击? 如何防范? 常见面试题 ...

  • sql面试题大全

    sql面试题大全 1、[10条SQL优化技巧](http://www.wityx.com/post/250_1_1...

  • 2020-05-28SQL笔试 I 经典20题及答案解析(上)

    面试经常碰到SQL面试题,今天给大家收集了20道SQL经典面试题,三人行必有我师! 01 建表语句 ```crea...

  • sql面试题大全

    sql面试题大全1、10条SQL优化技巧2、58到家MySQL数据库开发规范3、java.sql.Date和jav...

  • SQL面试题书目录

    SQL面试题书目录 - 简书 http://www.jianshu.com/p/5c305e9a0435 SQL...

  • Oracle面试题之SQL tunting

    Oracle面试题之SQL tunting 1:列举几种表连接方式 答:一共有三种连接方式(SQL优化),嵌套循环...

  • 2019年公司内部Java面试题

    公司招实习生,内部面试题这是最基础的 java 基础面试题,还有 SQL 方面,线程也会提及,Redis、spri...

  • SQL面试72题

    ​ SQL面试72题 大家好,这一期呢,我们来看一下sql的面试题。 第1题,什么是sql? 结构化查询语言。用来...

网友评论

      本文标题:sql面试题

      本文链接:https://www.haomeiwen.com/subject/kvrksktx.html