#### Sql语句50条案例demo
一. 提示:
根据文章:
(https://zhuanlan.zhihu.com/p/43289968)
和<br>
(https://zhuanlan.zhihu.com/p/38354000)
完成;
表结构关系:
<div align="left">
<img width="500px" height="280px" src="https://pic1.zhimg.com/80/v2-86fd263583a6cead51675982c1735e68_hd.jpg" />
</div>
二. 建表语句:
```
/*学生表*/
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
/*课程表*/
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
/*教师表*/
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
/*成绩表*/
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
```
插入数据:
```
/*插入学生表测试数据*/
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
/*课程表测试数据*/
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
/*教师表测试数据*/
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
/*成绩表测试数据*/
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
```
三. 练习案例:
1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号
> 三个表连接,s_score分为两列:
```
SELECT st.*, a.s_score, b.s_score
FROM student st
INNER JOIN (SELECT s_id, s_score FROM Score WHERE c_id='01') a ON st.s_id=a.s_id
INNER JOIN (SELECT s_id, s_score FROM Score WHERE c_id='02') b ON a.s_id=b.s_id
WHERE a.s_score > b.s_score;
```
<br>
2、查询平均成绩大于60分的学生的学号和平均成绩
```
SELECT s_id, AVG(s_score)
FROM Score
GROUP BY s_id
HAVING AVG(s_score) > 60;
```
<br>
3、查询所有学生的学号、姓名、选课数、总成绩
```
SELECT A.s_id, A.s_name, COUNT(B.c_id) AS 选课数, SUM(B.s_score) AS 总成绩
FROM student A
JOIN score B
ON A.s_id=B.s_id
GROUP BY A.s_id, A.s_name;
```
<br>
4、查询姓“猴”的老师的个数
```
SELECT COUNT(t_id)
From teacher
where t_name like '猴%';
```
<br>
5、查询没学过“张三”老师课的学生的学号、姓名
```
SELECT *
FROM Student
WHERE s_id NOT IN
(SELECT sc.s_id
FROM Score sc
INNER JOIN Course co ON sc.c_id=co.c_id
INNER JOIN Teacher te ON co.t_id=te.t_id
WHERE te.t_name='张三');
```
<br>
6、查询学过“张三”老师所教的所有课的同学的学号、姓名
```
SELECT *
FROM Student
WHERE s_id IN
(SELECT sc.s_id
FROM Score sc
INNER JOIN Course co ON sc.c_id=co.c_id
INNER JOIN Teacher te ON co.t_id=te.t_id
WHERE te.t_name='张三');
```
<br>
7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名
```
SELECT *
FROM student
where s_id IN
(SELECT a.s_id
FROM
(SELECT *
FROM Score
WHERE c_id='01') a
INNER JOIN
(SELECT *
FROM Score
WHERE c_id='02') b
ON a.s_id=b.s_id);
```
<br>
8、查询学过编号为“01”的课程, 但没学过编号为“02”的课程的学生的学号、姓名
```
SELECT *
FROM student
WHERE
s_id IN (SELECT s_id FROM Score WHERE c_id='01')
AND
s_id NOT IN (SELECT s_id FROM Score WHERE c_id='02');
```
<br>
9、查询课程编号为“02”的总成绩
```
SELECT SUM(s_score)
FROM Score
WHERE c_id='02';
/*或*/
SELECT c_id, SUM(s_score)
FROM score
GROUP BY c_id
HAVING c_id = '02';
```
<br>
10、查询所有课程成绩小于60分(全部课程不及格)的学生的学号、姓名
```
/*1.得出同学课程成绩小于60分的课程数*/
/*2.统计同学总共学了几门课程*/
/*3.如果相等就是全部课程不及格*/
SELECT a.s_id, t.s_name
FROM
(SELECT s_id, COUNT(c_id) AS cnt
FROM score
WHERE s_score<60
GROUP BY s_id) a
INNER JOIN
(SELECT s_id, COUNT(c_id) AS cnt
FROM score
GROUP BY s_id) b
ON a.s_id=b.s_id
INNER JOIN student t ON a.s_id=t.s_id
WHERE a.cnt=b.cnt;
/*另一种简便方法,如果最大成绩都小于60,那么这个同学所有课程不及格*/
SELECT s_id, s_name
FROM student
WHERE s_id IN
(SELECT s_id FROM score
GROUP BY s_id
HAVING MAX(s_score)<60);
```
<br>
11.查询没有学全所有课的学生的学号、姓名
```
SELECT st.*, sc.*
FROM student AS st
LEFT JOIN score AS sc ON st.s_id=sc.s_id
GROUP BY st.s_id
HAVING COUNT(DISTINCT sc.c_id) < (SELECT COUNT(DISTINCT c_id) FROM course);
```
<br>
12、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名
```
SELECT DISTINCT st.*
FROM student st INNER JOIN score sc ON st.s_id=sc.s_id
WHERE sc.c_id IN
(SELECT c_id FROM score WHERE s_id='01') AND st.s_id != '01';
```
13.查询和“01”号同学所学课程完全相同的其他同学的学号
```
/*第一步是查出和01号同学(不包括自己)所学课程数相同的学生id*/
/*第二步是查出至少有一门课程和01号同学不同的学生id*/
/*最后AND两个条件, 表示课程数相等,且学的课程相同即为完全相同*/
SELECT * FROM student
WHERE s_id IN (
SELECT s_id FROM score
WHERE s_id != '01'
GROUP BY s_id
HAVING COUNT(DISTINCT c_id)=(SELECT COUNT(DISTINCT c_id) FROM score where s_id='01')
)
AND s_id NOT IN (
SELECT DISTINCT s_id FROM score
WHERE c_id NOT IN (
SELECT sc.c_id
FROM score sc WHERE sc.s_id='01'
)
);
```
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
```
SELECT * FROM student st
WHERE st.s_id NOT IN (
SELECT s.s_id FROM score s
INNER JOIN course c ON s.c_id=c.c_id
INNER JOIN teacher t ON t.t_id=c.t_id AND t.t_name='张三'
);
```
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
```
SELECT st.s_id, st.s_name, AVG(sc.s_score) FROM student st
INNER JOIN score sc ON st.s_id=sc.s_id
WHERE st.s_id IN (
SELECT s_id FROM score
WHERE s_score<60
GROUP BY s_id
HAVING COUNT(DISTINCT c_id)>=2
)
GROUP BY st.s_id, st.s_name;
```
16、检索"01"课程分数小于60,按分数降序排列的学生信息
```
SELECT * FROM student st
INNER JOIN score sc ON sc.s_id=st.s_id
WHERE sc.s_score<60 AND sc.c_id='01'
ORDER BY sc.s_score DESC;
```
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
>备注:<br>
>1.因为要选出需要的字段 用case when 当c_id='01' then 可以得到对应课程的s_core和课程名<br>
>2.因为GROUP BY 要与select 列一致,所以用统计函数MAX加case when, 得到课程分数列<br>
>3.因为每科成绩只有一个, 所以MAX取到的是
没有合适的资源?快使用搜索试试~ 我知道了~
Sql语句50条案例demo.zip
共2个文件
png:1个
md:1个
需积分: 1 0 下载量 136 浏览量
2024-06-03
16:07:34
上传
评论
收藏 51KB ZIP 举报
温馨提示
SELECT st.*, a.s_score, b.s_score FROM student st INNER JOIN (SELECT s_id, s_score FROM Score WHERE c_id='01') a ON st.s_id=a.s_id INNER JOIN (SELECT s_id, s_score FROM Score WHERE c_id='02') b ON a.s_id=b.s_id WHERE a.s_score > b.s_score;
资源推荐
资源详情
资源评论
收起资源包目录
Sql语句50条案例demo.zip (2个子文件)
Db-sql-demo-master
README.md 18KB
小王.png 46KB
共 2 条
- 1
资源评论
小王毕业啦
- 粉丝: 3848
- 资源: 2259
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功