-- 新建表(学生表,教师表,课程表,成绩表) /* create table student( sid int, sname varchar(255), sage int, ssex varchar(255) ); create table teacher( tid int, tname varchar(255) ); create table course( cid varchar(255), cname varchar(255), tid int ); create table sc( sid int, cid varchar(255), score int ); */ -- 基础信息录入 /* insert into student values(1,"张三1",17,"男"); insert into student values(2,"张三2",18,"女"); insert into student values(3,"张三3",16,"男"); insert into student values(4,"张三6",17,"男"); insert into student values(5,"李四1",17,"女"); insert into student values(6,"李四2",18,"男"); insert into student values(7,"李四3",16,"男"); insert into student values(8,"李四6",17,"男"); insert into student values(9,"王五1",17,"女"); insert into student values(10,"王五2",19,"女"); insert into student values(11,"王五2",17,"男"); */ /* insert into teacher values(1,"李铎"); insert into teacher values(2,"叶平"); insert into teacher values(3,"邢鹏辉"); insert into teacher values(4,"王波"); */ /* insert into course values("001","离散数学",1); insert into course values("002","线性代数",1); insert into course values("003","具体数学",3); insert into course values("004","数值分析",2); insert into course values("005","高等数学",3); */ insert into sc values(1,"001",89); insert into sc values(1,"002",95); insert into sc values(1,"003",68); insert into sc values(1,"004",73); insert into sc values(1,"005",79); insert into sc values(2,"001",99); insert into sc values(2,"002",98); insert into sc values(2,"003",97); insert into sc values(2,"004",96); insert into sc values(2,"005",95); insert into sc values(3,"001",43); insert into sc values(3,"002",26); insert into sc values(3,"003",35); insert into sc values(3,"004",51); insert into sc values(3,"005",6); insert into sc values(4,"001",63); insert into sc values(4,"002",66); insert into sc values(4,"003",65); insert into sc values(4,"004",61); insert into sc values(4,"005",66); insert into sc values(5,"001",81); insert into sc values(5,"002",91); insert into sc values(5,"003",61); insert into sc values(5,"004",71); insert into sc values(5,"005",72); insert into sc values(6,"001",85); insert into sc values(6,"002",94); insert into sc values(6,"003",63); insert into sc values(6,"004",72); insert into sc values(6,"005",71); insert into sc values(7,"001",75); insert into sc values(7,"002",64); insert into sc values(7,"003",83); insert into sc values(7,"004",42); insert into sc values(7,"005",1); insert into sc values(8,"001",81); insert into sc values(8,"002",92); insert into sc values(8,"003",63); insert into sc values(8,"004",73); insert into sc values(8,"005",54); insert into sc values(9,"001",41); insert into sc values(9,"002",62); insert into sc values(9,"003",73); insert into sc values(9,"004",54); insert into sc values(9,"005",25); insert into sc values(10,"001",86); insert into sc values(10,"002",79); insert into sc values(10,"003",99); insert into sc values(10,"004",28); insert into sc values(10,"005",10);
1、查询("001"课程比"001"课程成绩高)的所有学生的学号。
as起别名,可以给查询的列起别名,子查询结果集必须起别名,as可以省略用“ ”空格代替。
from后面如果是多张表,用“,”连接
多个表连接条件where里面直接写就可以
select a.c 学号 from (select sid c ,score from sc where cid = '001') a, (select sid c, score from sc where cid = '002') b where a.c = b.c and a.score>b.score;
2.查询平均成绩大于等于60分的同学的学号和平均成绩。
select sid ,avg(score)from sc GROUP BY sid HAVING avg(score)>=60;
3.查询所有同学的学号、姓名、选课数、总成绩。
聚合函数:count()、sum()、max()、min()、avg()。
select student.sid 学号,sname 姓名,count(score) 选课数,sum(score) 成绩总和 from student ,sc where student.sid=sc.sid GROUP BY student.sid;
4.查询性“李”的老师的个数。
通配符 % 和
%是0~n个任意字符
_ 是一个字符
如果长度至少是两个字符,设计为“李_%”
select count(tid) 老师个数 from teacher where tname LIKE "李_%";
5.查询没学过“叶平”老师的课程的同学的学号、姓名。
not in 和 in 描述差集和交集
问题转化为学过“叶平”老师课程的同学
/* select sid from sc,course,teacher where sc.cid=course.cid and teacher.tid = course.tid and teacher.tname="叶平"; */
select sid,sname from student WHERE sid not in (select sid from sc,course,teacher where sc.cid=course.cid and teacher.tid = course.tid and teacher.tname="叶平");
附上查询结果:
6.查询所有有挂科的学生和姓名。
(1)DISTINCT 去重复(不建议使用)
select DISTINCT(sc.sid),sname from sc,student where sc.sid = student.sid and score<60 ;
使用group by 分组聚合
select sc.sid,sname from sc,student where sc.sid = student.sid and score<60 GROUP BY student.sid ;
7.查询至少一门课程与学生学号为“2”的同学所学课程相同的学生学号和姓名
(1)求学号为2同学课程cid集合
select cid from sc where sc.sid=2;
select student.sid,sname from student,sc where student.sid=sc.sid and cid in(select cid from sc where sc.sid=2) GROUP BY student.sid HAVING student.sid<>2;
8、统计列印各个科目的成绩,各个科目按分数段 课程ID,课程名称,【100-85】,【85-70】,【70-60】,【60-0】
case语句
case when 1>2 then 10 else 20 end;
sum(select case when 1<2 then 1 else 0 end);
select sum(case when score >=85 and score<=100 then 1 else 0 end)from sc;
select sc.cid 课程ID,cname 课程名称, sum(case when score>=85 and score<=100 then 1 else 0 end) as '[85-100]', sum(case when score>=75 and score<85 then 1 else 0 end) as '[75-85]', sum(case when score>=60 and score<75 then 1 else 0 end) as '[60-75]', sum(case when score>=0 and score<60 then 1 else 0 end) as '[0-60]' from course,sc where course.cid = sc.cid GROUP BY sc.cid;查询结果如下:
9.查询每门课程被选修的学生数。
select sc.cid,cname,count(sc.cid) as'总数' from sc,course where sc.cid = course.cid group by sc.cid;
10.查询出只选修了一门课程的学生的学号、姓名。
select student.sid,sname from student,sc where student.sid = sc.sid GROUP BY sc.sid having count(*)=1;
11.查询男生、女生的总人数。
性别 人数
男生 3
女生 4
#第一种 select ssex ,count(*)as "人数" from student GROUP BY ssex; #第二种 select case when ssex='男' then '男生' else '女生' end as "性别",count(*) as '人数' from student GROUP BY ssex; #第三种 (select "男生" as "性别",count(*) as "人数" from student where ssex='男') UNION (select "女生" as "性别",count(*) as "人数" from student where ssex='女')
12.姓李的师生名单。
(select sname as '姓名' from student where sname like '李_%') UNION (select tname as '姓名' from teacher where tname like '李_%');
13.查询出同名同姓的学生名单,并统计人数。
select sname ,count(*) from student GROUP BY sname having count(*)>1;
14.查询每门课程的平均成绩,结果按平均成绩升序排列,如果平均成绩相同,则按课程号降序排列。
select cid,avg(score) from sc GROUP BY cid ORDER BY avg(score), cid desc;15、查询平均成绩大于85的所有的学生的学号、姓名、平均成绩.
select student.sid,sname,avg(score) from student,sc where student.sid=sc.sid GROUP BY student.sid having avg(score)>85;16、查询课程名称为“离散数学”,且分数低于60分的学生姓名和分数
select student.sname,score from student,sc,course where student.sid=sc.sid and sc.cid = course.cid and score<60 and course.cname="离散数学"
17、查询所有学生的选课情况:包括学号、课程号、姓名、课程名。
select student.sid 学号,course.cid 课程号,sname 姓名,cname 课程名 from student,sc,course where student.sid=sc.sid and sc.cid = course.cid ;18、查询有学生不及格的课程的课程号和课程名,并按照课程号从大到小排列.
select sc.cid,cname from course,sc where course.cid=sc.cid and score<60 GROUP BY sc.cid ORDER BY cid desc;
19、查询课程号为003且成绩在80分以上的学生的学号、姓名.
select student.sid,sname from student,sc where student.sid=sc.sid and score>80 and sc.cid="003";
20、查询各个老师相应的学生总人数.
select teacher.tname,count(DISTINCT(sc.sid)) from teacher,sc,course where teacher.tid = course.tid and course.cid = sc.cid group by teacher.tid;
21、查询不同课程成绩相同的学生的学号、课程号、学生成绩(自连接)。
select a.sid,a.cid,a.score from sc a ,sc b where a.score = b.score and a.cid<>b.cid and a.sid=b.sid;22、查询选“001”课程的所有学生的平均分(去掉一个最高分,去掉一个最低分)
select (sum(score)-max(score)-min(score))/(count(score)-1) from sc where cid="001";
23.关于join使用外连接和内连接
(1)左连接
select * from teacher left JOIN course on teacher.tid=course.tid;
(2)右连接
select * from course RIGHT JOIN teacher ON teacher.tid = course.tid;