-- sql joins Concept map

-- 仅供学习交流,有不对的地方 欢迎指正 谢谢~

-- Only for learning and communication, if there is something wrong, welcome to correct. Thank you ,Any similarities are purely coincidental~

--表code:

sql语句练习(mysql)下⾯是练习表数据和结构
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`id` int(11) NOT NULL,
`name` varchar(22) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('30001', '物理');
INSERT INTO `course` VALUES ('30002', '政治');
INSERT INTO `course` VALUES ('30003', '语⽂');
INSERT INTO `course` VALUES ('30004', '⾼数');
INSERT INTO `course` VALUES ('30005', '英语');
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(22) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('10001', 'tom');
INSERT INTO `student` VALUES ('10002', 'json');
INSERT INTO `student` VALUES ('10003', 'ak');
INSERT INTO `student` VALUES ('10004', 'km');
INSERT INTO `student` VALUES ('10005', 'mk');
-- ----------------------------
-- Table structure for student_course
-- ----------------------------
DROP TABLE IF EXISTS `student_course`;
CREATE TABLE `student_course` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`teacher_id` int(11) NOT NULL,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student_course
-- ----------------------------
INSERT INTO `student_course` VALUES ('4', '10001', '30002', '20002', '82'); INSERT INTO `student_course` VALUES ('6', '10001', '30003', '20003', '82'); INSERT INTO `student_course` VALUES ('8', '10001', '30004', '20005', '82'); INSERT INTO `student_course` VALUES ('10', '10001', '30005', '20005', '82'); INSERT INTO `student_course` VALUES ('12', '10002', '30001', '20001', '90'); INSERT INTO `student_course` VALUES ('14', '10002', '30002', '20002', '92'); INSERT INTO `student_course` VALUES ('16', '10002', '30003', '20003', '62'); INSERT INTO `student_course` VALUES ('18', '10002', '30004', '20004', '82'); INSERT INTO `student_course` VALUES ('20', '10002', '30005', '20005', '82'); INSERT INTO `student_course` VALUES ('22', '10003', '30001', '20001', '69'); INSERT INTO `student_course` VALUES ('24', '10003', '30002', '20002', '89'); INSERT INTO `student_course` VALUES ('26', '10003', '30003', '20003', '99'); INSERT INTO `student_course` VALUES ('28', '10003', '30004', '20004', '82'); INSERT INTO `student_course` VALUES ('30', '10003', '30005', '20005', '82'); INSERT INTO `student_course` VALUES ('32', '10004', '30001', '20001', '92'); INSERT INTO `student_course` VALUES ('34', '10004', '30002', '20002', '93'); INSERT INTO `student_course` VALUES ('36', '10004', '30003', '20003', '73'); INSERT INTO `student_course` VALUES ('38', '10004', '30004', '20004', '82'); INSERT INTO `student_course` VALUES ('40', '10004', '30005', '20005', '82'); INSERT INTO `student_course` VALUES ('42', '10005', '30001', '20001', '95'); INSERT INTO `student_course` VALUES ('44', '10005', '30002', '20002', '75'); INSERT INTO `student_course` VALUES ('46', '10005', '30003', '20003', '79'); INSERT INTO `student_course` VALUES ('48', '10005', '30004', '20004', '82'); INSERT INTO `student_course` VALUES ('50', '10005', '30005', '20005', '82');
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL,
`name` varchar(22) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('20001', 'su');
INSERT INTO `teacher` VALUES ('20002', 'wang');
INSERT INTO `teacher` VALUES ('20003', 'zhou');
INSERT INTO `teacher` VALUES ('20004', 'yang');
INSERT INTO `teacher` VALUES ('20005', 'liu');
4个表,包括student,course,student_course,teacher

--详细练习 code :

select * from student;  学生表
select * from course;   课程表
select * from student_course; 学生课程分数表
select * from teacher;   教师表-- 1、查询名字'o'开头的⽼师的个数
select COUNT(DISTINCT(name))
FROM teacher where name like 'o%';--2.老师的个数select count(DISTINCT(id)) 老师人数
from teacher;--3.查询'su'老师教的课程分数最高的学生信息select * from  student_course a,teacher b where
a.teacher_id = b.id
and name ='su'
ORDER BY a.score desc LIMIT 0,1-- 4.查询物理成绩比英语成绩高的所有学生SELECTstudent.name
FROM(SELECT*FROM(select * from student_course WHERE course_id=30001) a,(select id as idb , student_id as student_idb, course_id as course_idb, teacher_id as teacher_idb ,score as scoreb from student_course WHERE course_id=30005) bWHEREa.score>b.scoreb and a.student_id=b.student_idb) c left join student on c.student_id = student.id ;-- 5.查询平均成绩⼤于60分的同学的学号和平均成绩select a.student_id,avg(a.score) as '平均分'
from student_course as a
group by a.student_id
having avg(a.score)>60-- 6 查询所有同学的学号,姓名、选课数、总成绩select b.student_id,a.name,count(b.course_id),sum(b.score)
from student a, student_course b
where a.id = b.student_id
group by b.student_id-- -- 7,查询没学过“su”⽼师课的同学的学号、姓名SELECT*
FROMstudent
WHEREid NOT IN ( SELECT DISTINCT ( student_id ) FROM student_course WHERE teacher_id = ( SELECT DISTINCT ( id ) FROM teacher WHERE NAME = "su" ) );- 8 学过30001和30002课程的同学的学号、姓名 select name from student where id in (
select DISTINCT(student_id) from student_course where course_id = '30001' or course_id ='30002'
)-- 9  学过30001和30002课程的同学的学号
pass
-- 10 、查询学过“song”⽼师所教的课的所有同学的学号、姓名;
pass-- 11查询课程编号“30002”的成绩⽐课程编号“30001”课程低的所有同学的学号、姓名select * from student where id in(
select  distinct(a.student_id) from
(select * from student_course where course_id=30002) a
inner join
(select * from student_course where course_id=30001) b
on a.score<b.score
)
-- 12 查询没有学全所有课的同学的学号、姓名select a.student_id,b.name
from student_course a,student b
where a.student_id=b.id
group by a.student_id
having count(a.course_id) <> (select count(*) from course);-- 13查询所有课程成绩⼩于90分的同学的学号、姓名select * from student where id not in (
select distinct(student_id) from student_course  where score>90
)-- 14 查询与学号为“10001”同学至少学过一门课程的其他同学学号和姓名
--方案一
select *from student where id in (
SELECT distinct(student_id) FROM student_course where student_id <> 10001 and course_id in
(
SELECT course_id FROM student_course where student_id=10001
)
)
;
--方案二
select distinct a.id,a.name from student a, student_course b where a.id=b.student_id and b.student_id <> 10001 and
b.course_id in(
SELECT course_id FROM student_course where student_id=10001
);-- 15查询“su”老师 教的学生 信息
select student.`name` from student ,student_course ,teacher
where student.id = student_course.student_id
and student_course.teacher_id = teacher.id
and teacher.`name` = 'su';-- -- 16查询和“10002”号的同学学习的课程数一样的其他同学学号和姓名
SELECTb.id,b.NAME
FROMstudent_course a,student b
WHEREa.course_id IN ( SELECT course_id FROM student_course WHERE student_id = 10002 ) AND a.student_id = b.id AND a.student_id <> 10002
GROUP BYb.id
HAVINGcount( * ) = ( SELECT count( * ) FROM student_course WHERE student_id = 10002 );-- 17   按平均成绩从⾼到低显⽰所有学⽣的“物理”、“政治”、“英语”三门的课程成绩
-- 并且 按如下形式显⽰: 学⽣ID,物理,政治,英语,有效课程数,有效平均分SELECTt.student_id AS 学生 ID,( SELECT score FROM student_course
WHERE t.student_id = student_id AND course_id = "30001" ) AS 物理,( SELECT score FROM student_course
WHERE t.student_id = student_id AND course_id = "30002" ) AS 政治,( SELECT score FROM student_course
WHERE t.student_id = student_id AND course_id = "30005" ) AS 英语,count( * ) AS 有效课程数,AVG( t.score ) AS 平均成绩
FROMstudent_course t
GROUP BYt.student_id
ORDER BYavg( t.score );-- 18 查询各科成绩最⾼和最低的分:以如下形式显⽰:课程ID,最⾼分,最低分select course_id as 课程ID,max(score) as 最高分,min(score) as 最低分
from student_course
group by course_id;-- 19 按各科平均成绩从低到⾼和及格率
select course_id as 课程ID,avg(score) as 平均成绩,
100*sum(case when score>=60 then 1 else 0 end) /count(*) as  及格百分数
from student_course  t
group by course_id
order by avg(score)  desc;-- 20.查询如下课程平均成绩和及格率的百分数(⽤"1⾏"显⽰):
-  物理(30001),政治(30002),语⽂ (30003),⾼数(30004)select
(select  avg(score) from student_course where course_id="30001") as 物理,
(select  avg(score) from student_course where course_id="30002") as 政治,
(select  avg(score) from student_course where course_id="30003") as 语⽂,
(select  avg(score) from student_course where course_id="30004") as ⾼数,
100*sum(case when score>=60 then 1 else 0 end)/count(*) as 及格率,
avg (score) as 平均成绩
from student_course
group by course_id;-- 21 查询不同⽼师所教不同课程平均分从⾼到低显⽰select student_course.teacher_id,student_course.course_id,avg(score)
from student_course
group by teacher_id
order by avg(score) desc;-- 22 统计列 印各科成绩,各分数段⼈数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] ;select a.course_id as 课程ID,
b.name as 课程名称,
sum(case when a.score between 85 and 100 then 1 else 0 end) as "[100-85]",
sum(case when a.score between 70 and 85 then 1 else 0 end) as "[85-70]",
sum(case when a.score between 65 and 70 then 1 else 0 end) as "[70-60]",
sum(case when a.score <60 then 1 else 0 end) as "[<60]"
from student_course a,course b
where a.course_id=b.id
group by a.course_id,b.name;-- 23 查询学⽣平均成绩select a.student_id as 学⽣ID,b.name as 姓名,avg(a.score)  as 平均成绩
from student_course a,student b
where a.student_id=b.id
group by a.student_id
order by avg(a.score) desc;-- 24 查询各科成绩第一名的记录:(不考虑成绩并列情况)
SELECTt1.student_id AS 学⽣ ID,t1.course_id AS 课程 ID,t1.score AS 分数
FROMstudent_course t1
WHEREt1.id IN ( SELECT t.id FROM ( SELECT id FROM student_course ORDER BY score DESC LIMIT 3 ) AS t )
ORDER BYt1.score DESC;-- 25查询每门课程被选修的学⽣数select count(student_id)
from student_course
group by course_id;-- 26 查询出只选修了⼀门课程的全部学⽣的学号和姓名SELECTa.student_id,b.NAME
FROMstudent_course a,student b
WHEREb.id = a.student_id
GROUP BYstudent_id
HAVINGcount( course_id ) = 1;-- 27 查询姓“s”的学⽣名单
select * from student where name like "s%" ;- 28 查询同名同性学⽣名单,并统计同名⼈数
select name ,count(name) from student group by name;-- 29 查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
SELECTavg( score ),course_id
FROMstudent_course
GROUP BYcourse_id
ORDER BYavg( score ),course_id DESC;-- 31 查询课程名称为“政治”,且分数高于60的学⽣姓名和分数select  a.student_id,a.score,b.name
from student_course a,student b
where a.student_id=b.id and a.course_id=(
select id from course where name ="政治"
)
group by  a.student_id,a.score
having a.score >60;-- 32查询所有学⽣的选课情况;
SELECTa.student_id,a.course_id,b.NAME,c.NAME
FROMstudent_course a,student b,course c
WHEREa.student_id = b.id AND a.course_id = c.id
GROUP BYa.student_id,a.course_id;-- 33 查询任何⼀门课程成绩在70分以上的姓名、课程名称和分数;SELECTa.student_id,a.course_id,a.score,b.NAME,c.NAME
FROMstudent_course a,student b,course c
WHEREa.student_id = b.id AND a.course_id = c.id
GROUP BYa.student_id,a.course_id,a.score
HAVINGa.score > 70;-- 34 查询不及格的课程,并按课程号从⼤到⼩排列
select course_id,score
from student_course
group by course_id,score
having score<60
order by course_id;-- 35 查询课程编号为30003且课程成绩在80分以上的学⽣的学号和姓名;
SELECTa.student_id,a.score,a.course_id,b.NAME
FROMstudent_course a,student b
WHEREa.student_id = b.id AND a.course_id = "30003"
GROUP BYa.student_id,a.score,a.course_id
HAVINGa.score > 80;-- 36求选了课程的学⽣⼈数
SELECTcount( DISTINCT student_id )
FROMstudent_course;-- 37 查询选修“liu”⽼师所授课程的学⽣中,成绩最⾼的学⽣姓名及其成绩
SELECTb.NAME,a.score
FROMstudent_course a,student b,course c,teacher d
WHEREa.course_id = c.id AND a.student_id = b.id AND a.teacher_id = d.id AND d.NAME = "liu" AND a.score = ( SELECT max( score ) FROM student_course WHERE course_id = c.id );-- 38查询各个课程及相应的选修⼈数
select course_id,count(student_id)
from student_course
group by course_id;-- 39 查询不同课程成绩相同的学⽣的学号、课程号、学⽣成绩
SELECTa.student_id,a.course_id,a.score
FROMstudent_course a,student_course b
WHEREa.score = b.score AND a.course_id <> b.course_id;-- 40 查询每门功成绩最好的前两名
SELECT*
FROMstudent_course
WHEREscore IN ( SELECT score FROM ( SELECT score FROM student_course LIMIT 2 ) AS a )
ORDER BYcourse_id DESC;-- 41 统计每门课程的学⽣选修⼈数(超过4⼈的课程才统计)。
-- 要求输出课程号和选修⼈数,查询结果按⼈数降序排列,查询结果按⼈数降序排列,
-- 若⼈数相同,按课程号升序排列
select course_id, count(*)
from student_course
group by course_id
having count(*)>4
order by count(*),course_id desc;-- 42 检索⾄少选修两门课程的学⽣学号
select student_id
from student_course
group by student_id
having count(*)>=2;-- 43 、查询全部学⽣都选修的课程的课程号和课程名
select *
from course
where id in(
select course_id from student_course group by course_id having count(*)=(select count(*) from student) );- 44 查询没学过“yang”⽼师讲授的课程的学⽣姓名
select name from student where id not in(
select distinct a.student_id
from student_course a,teacher c
where  a.teacher_id=c.id and c.name = "yang"
);-- 45 查询两门以上不及格课程的同学的学号及其平均成绩
select student_id,avg(score)
from student_course
where score <60
group by student_id
having count(*) >=2;-- 46 检索“30004”课程分数⼩于60,按分数降序排列的同学学号
select student_id
from student_course
where course_id="30004" and score<60
group by student_id,score
order by score desc;-- 47 删除“10003”同学的“30001”课程的成绩
delete from student_course where student_id=10003 and course_id=30002;

-- 仅供学习交流,有不对的地方 欢迎指正 欢迎交流 谢谢~

-- Only for learning and communication, if there is something wrong, welcome to correct. Thank you ~

SQL Based on practice相关推荐

  1. 发如雪SQL Based on practice(二)

    发如雪 - 周杰伦 作词:方文山 作曲:周杰伦 编曲:林迈可 狼牙月 伊人憔悴 我举杯 饮尽了风雪 是谁打翻前世柜 惹尘埃是非 缘字诀 几番轮回 你锁眉 哭红颜唤不回 纵然青史已经成灰 我爱不灭 繁华 ...

  2. SharePoint 2010 Form Authentication (SQL) based on existing database

    博客地址 http://blog.csdn.net/foxdave SharePoint 2010 表单认证,基于现有数据库的用户信息表 本文主要描述本人配置过程中涉及到的步骤,仅作为参考,不要仅限于 ...

  3. @sql 单元测试_10个最常见SQL单元测试错误

    @sql 单元测试 This SQL unit testing article is based on the fact that we often learn from the mistakes s ...

  4. 免费的sql工具_免费SQL工具

    免费的sql工具 Adminer ApexSQL CI/CD toolkit ApexSQL Compare ApexSQL Complete ApexSQL Decrypt ApexSQL Plan ...

  5. 使用SQL Server行级安全性的分片多租户数据库

    目录 介绍 背景 创建Catalog 数据库 关于分片方法的简要介绍 创建共享多租户数据库 关于聚集索引的简短说明 创建安全谓词函数 创建DDL触发器以强制执行安全谓词 创建租户 最终测试 附录 1 ...

  6. 2019 6月编程语言_今年六月您可以开始学习650项免费的在线编程和计算机科学课程...

    2019 6月编程语言 Seven years ago, universities like MIT and Stanford first opened up free online courses ...

  7. 夏天和空调_您可以在今年夏天开始学习650项免费的在线编程和计算机科学课程...

    夏天和空调 Seven years ago, universities like MIT and Stanford first opened up free online courses to the ...

  8. 计算机编程课程顺序_您可以在6月开始参加630项免费的在线编程和计算机科学课程...

    计算机编程课程顺序 Six years ago, universities like MIT and Stanford first opened up free online courses to t ...

  9. 全栈入门_启动数据栈入门包(2020)

    全栈入门 I advise a lot of people on how to build out their data stack, from tiny startups to enterprise ...

最新文章

  1. MySQL 5.7 vs 8.0,哪个性能更牛?
  2. CentOS 7 学习(一) 配置LAMP和Nginx
  3. 5、Linux系统的目录结构
  4. 二十、“安化辞骚千万卒,康吾故土祭吾躯”(2021.6.14)
  5. how is Fiori launchpad host name and port number determine
  6. 部署用于生产的Exceptionlees(一个强大易用的日志收集服务)
  7. Complex类与运算符重载
  8. oracle中日期相减及显示几天几小时几分钟
  9. 读懂金融危机必看的十四本书
  10. 工作流系统之三十四 集成用户系统
  11. opencv-python对大视频切割成小视频
  12. 冬季无锡旅游攻略,无锡太湖、鼋头渚之游小记!!!!
  13. 生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
  14. python判断三位数水仙花数_Python如何判断一个数字是否为水仙花数
  15. 一个计算机网络典型系统可由,计算机网络基础试题2.doc
  16. 网银互联携手Aryaka,全托付方案亮相SD-WAN峰会
  17. 大一学计算机心得,大一计算机学习心得范文
  18. echo输出字符串显示不同颜色和背景色
  19. 手写体数字识别(Python+TensorFlow)
  20. 大规模裁员?计算机会成为下一个土木吗?

热门文章

  1. 让Vista 的 墨球游戏在 XP下运行
  2. 心系冬奥 翰墨传情 |当代书画名家为奥运加油书画推介展【邱建华篇】
  3. 自己对“为什么不同挂载点的inode号码都是2”的理解
  4. 上节Pandas学会了吗?那我可教你进阶啦~
  5. seo和python_Seo技术篇之python
  6. 玻色量子荣为CCF量子计算专业组金牌合作伙伴
  7. linux最简单的命令,Linux 简单命令整理
  8. 一分钟快速简化代码,使用Lombok消灭getter、setter和equals方法
  9. 入职一个月,全网最帅工程师离职
  10. ps---万能调色方法和ps-----去掉扫描的文字灰蒙蒙感觉 -------色阶讲解