首先新建数据库,新建数据表,插入数据
CREATE DATABASE PRACTICE;
CREATE TABLE STUDENT(SNO varchar(10),SNAME nvarchar(10),SAGE datetime,SSEX nvarchar(10));
INSERT INTO STUDENT VALUES
('01' , N'赵雷' , '1990-01-01' , N'男'),
('02' , N'钱电' , '1990-12-21' , N'男'),
('03' , N'孙风' , '1990-05-20' , N'男'),
('04' , N'李云' , '1990-08-06' , N'男'),
('05' , N'周梅' , '1991-12-01' , N'女'),
('05' , N'周梅' , '1991-12-01' , N'女'),
('06' , N'吴兰' , '1992-03-01' , N'女'),
('07' , N'郑竹' , '1989-07-01' , N'女'),
('08' , N'王菊' , '1990-01-20' , N'女');
CREATE TABLE COURSE(CNO varchar(10),CNAME nvarchar(10),TNO varchar(10));
INSERT INTO COURSE VALUES
('01' , N'语文' , '02'),
('02' , N'数学' , '01'),
('03' , N'英语' , '03');
CREATE TABLE TEACHER(TNO varchar(10),TNAME nvarchar(10));
INSERT INTO TEACHER VALUES
('01' , N'张三'),
('02' , N'李四'),
('03' , N'王五');
CREATE TABLE SC(SNO varchar(10),CNO varchar(10),SCORE decimal(18,1));
INSERT INTO SC VALUES
('01' , '01' , 80),
('01' , '02' , 90),
('01' , '03' , 99),
('02' , '01' , 70),
('02' , '02' , 60),
('02' , '03' , 80),
('03' , '01' , 80),
('03' , '02' , 80),
('03' , '03' , 80),
('04' , '01' , 50),
('04' , '02' , 30),
('04' , '03' , 20),
('05' , '01' , 76),
('05' , '02' , 87),
('06' , '01' , 31),
('06' , '03' , 34),
('07' , '02' , 89),
('07' , '03' , 98);
- 查询”01”课程比”02”课程成绩高的学生的信息及课程分数;
答案一
select A.*,B.SCORE '01',C.SCORE '02'
from student A inner join (sc B,sc C) on A.SNO=B.SNO and B.CNO='01'and A.SNO=C.SNO and C.CNO='02'
where C.SCORE> B.SCORE;
select A.*,B.SCORE '01',C.SCORE '02'
from (student A inner join sc B on A.SNO=B.SNO and B.CNO='01')
inner join sc C on A.SNO=C.SNO and C.CNO='02'
where C.SCORE> B.SCORE;
答案二
inner join可以分开写,但是前面这个需要加(),并且on也要分开写
select A.*,B.SCORE '01',C.SCORE '02'
from (student A inner join sc B on A.SNO=B.SNO and B.CNO='01')
inner join sc C on A.SNO=C.SNO and C.CNO='02'
where C.SCORE> B.SCORE;
- 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
答案一
SELECT A.SNO,B.SNAME,AVG(SCORE) AVG_SCORE
FROM SC A INNER JOIN STUDENT B
ON A.SNO=B.SNO
GROUP BY SNO
HAVING AVG(SCORE)>=60;
无法运行,报错Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘practice.B.SNAME’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
答案二
嵌套式查询,效率会变低
select t.*,B.SNAME
from (select SNO,avg(SCORE) '平均分'
from sc
group by SNO
having avg(SCORE)>=60)t inner join student B on t.SNO=B.SNO;
- 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select A.SNO ,B.SNAME,count(CNO) '课程门数',SUM(SCORE) '总得分'
from sc A inner join student B on A.SNO=B.SNO
group by A.SNO;
- 查询“李”姓老师的数量
select count(TNO)
from teacher
where left(TNAME,1)='李';
- 查询学过“张三”老师授课的同学的信息
select distinct A.*
from student A inner join (sc B,course C,teacher D)
on A.SNO=B.SNO and B.CNO=C.CNO and C.TNO=D.TNO
where D.TNAME='张三';
- 查询没学过“张三”老师授课的同学的信息
注意点: 没XX的内容如果不好判断,可以先查询符合条件的,然后使用 not in
select * from student where SNO not in
(select distinct A.SNO
from student A inner join (sc B,course C,teacher D)
on A.SNO=B.SNO and B.CNO=C.CNO and C.TNO=D.TNO
where D.TNAME = '张三');
- 查询学过变化为“01"并且也学过编号为”02“的课程的同学的信息;
select distinct t.* from student t
where t.SNO in
(select distinct A.SNO from sc A inner join sc B on A.SNO=B.SNO
where A.CNO='01' and B.CNO='02'
)
- 查询学过编号为”01”但是没有学过编号为”02”的课程的同学的信息;
select distinct t.* from student t
where t.SNO in (select SNO from sc where CNO='01' )
and t.SNO not in (select SNO from sc where CNO='02' )
- 查询没有学全所有课程的同学的信息;
select distinct t.* from student t
where t.SNO not in(select SNO from sc group by SNO having count(distinct CNO)=3 )
- 查询至少有一门课与学号为”01”的同学所学相同的同学的信息;
select distinct t.* from student t
where SNO in (select distinct SNO from sc
where CNO in (select CNO from sc A where SNO='01' ) and SNO <>'01')
- 查询和”01”号的同学学习的课程完全相同的其他同学的信息 ;
SELECT A.SNO FROM STUDENT A INNER JOIN SC B ON A.SNO=B.SNO
WHERE A.SNO<>01 AND B.CNO IN (SELECT CNO FROM SC WHERE SNO=01)
GROUP BY A.SNO HAVING COUNT(B.CNO)=(SELECT count(*) FROM SC WHERE SNO=01)
- 查询没学过”张三”老师讲授的任一门课程的学生姓名;
select SNAME from student
where SNO not in (select A.SNO from course B inner join sc A on A.CNO=B.CNO
inner join teacher C on C.TNO=B.TNO where C.TNAME='张三')
- 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩;
select A.SNO,B.SNAME,avg(SCORE)
from sc A inner join student B on A.SNO=B.SNO and A.SCORE <60
group by SNO
having count(CNO)>=2
- 检索”01”课程分数小于60,按分数降序排列的学生信息;
select A.* from student A inner join sc B on A.SNO=B.SNO
where B.CNO='01' and B.SCORE <60
order by B.SCORE desc
- 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩;
答案一
使用group_concat()函数,将groupby以外的字段拼接起来
SELECT SNO,GROUP_CONCAT(CNO),GROUP_CONCAT(SCORE),AVG(SCORE) FROM SC
GROUP BY SNO ORDER BY AVG(SCORE) desc
答案二
此种方式只能显示groupby的第一条数据
select *,avg(SCORE)
from sc
group by SNO order by avg(SCORE) desc
- 查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率;
注意点: 在select后面可以直接写公式,要注意和下面的衔接,groupby要放在最下面,而不是公式内
select A.CNO '课程ID',CNAME '课程名称',max(SCORE) '最高分',min(SCORE) '最低分',avg(SCORE) '平均分',(select COUNT(*) FROM SC WHERE CNO=A.CNO AND SCORE>=60)/(SELECT COUNT(*) FROM SC WHERE CNO=A.CNO)*100 '及格率%'
from sc A inner join course B on A.CNO=B.CNO
group by A.CNO
- 按各科成绩进行排序,并显示排名
答案一:使用 row_number() OVER(partition by 分组字段 order by 排序字段 DESC) 函数
select CNO,SNO,SCORE,row_number() OVER(partition by CNO order by SCORE DESC) '排名' from sc
答案二:
SELECT SNO,CNO,SCORE,(SELECT COUNT(1)+1 FROM SC WHERE SCORE> A.SCORE AND CNO=A.CNO) '排名'
FROM SC A
ORDER BY CNO,SCORE DESC;
- 查询学生的总成绩并进行排名
答案一:
select SNO,sum(SCORE),row_number() over( order by sum(SCORE) desc)'排名'
from sc group by SNO
答案二:
SELECT SNO,SUM(SCORE)
FROM SC
GROUP BY SNO
ORDER BY SUM(SCORE) DESC
- 查询不同老师所教不同课程平均分从高到低显示
select TNAME,a.CNO,avg(SCORE)
from sc a inner join (course b,teacher c) on a.CNO=B.CNO and b.TNO=C.TNO
group by TNAME
order by avg(SCORE) desc
- 查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
注意点: 有单独筛选时可选择嵌套查询
select distinct t.*, b.*
from (select CNO,SNO,SCORE,row_number() OVER(partition by CNO order by SCORE desc) as num
from sc )t inner join student b on t.SNO=b.SNO where t.num in(2,3)
- 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
注意点: 使用case when统计不同情况下的数据
答案一:
1.使用case when为数据打上标签,然后利用group by分组统计不同标签下的数据。
2.因为有两层分组统计(课程和分值区间),因此要统计在第一层分组下的第二层占比时,可以使用join表的方式。也就是说另建表2,只计算第一层分组的总值,和具有两层分组的表1进行连接后计算占比。
select B.CNAME, A.CNO,A.num'分值',A.a'人数',A.a/B.total'占比'
from(
select CNO,num,count(*) as a
from(
select *,
case
when score<=100 and score>=85 then '[100-85]'
when score< 85 and score>=70 then '[85-70]'
when score<70 and score>=60 then '[70-60]'
else'[0-60]' end as num
from SC
order by score desc)t1
group by CNO,num)A left join
(select sc.CNO,course.CNAME,count(*) as total from sc inner join course on sc.CNO=course.CNO group by CNO)B on A.CNO=B.CNO
答案二
1.使用case when 筛选符合条件的数据,同时使用SUM函数统计数量。
2.占比则根据数量/count(*)计算,最外层利用group by分组统计CNO的数据。
select sc.CNO,course.CNAME,
sum(case when score<=100 and score>=85 then 1 else 0 end)'100-85人数',
sum(case when score<=100 and score>=85 then 1 else 0 end)/count(*)'100-85占比',
sum(case when SCORE<85 AND SCORE>=70 then 1 else 0 end)'100-85人数',
sum(case when SCORE<85 AND SCORE>=70 then 1 else 0 end)/count(*)'100-85占比',
sum(case when SCORE<70 AND SCORE>=60 then 1 else 0 end)'100-85人数',
sum(case when SCORE<70 AND SCORE>=60 then 1 else 0 end)/count(*)'100-85占比',
sum(case when SCORE<60 then 1 else 0 end)'100-85人数',
sum(case when SCORE<60 then 1 else 0 end)/count(*)'100-85占比'
from sc left join course on sc.CNO =course.CNO
group by sc.CNO