表设计
Student(Sid, Sname, Sage, Ssex) 学生表
Course(Cid, Cname, Tid) 课程表
SC(Sid, Cid, score) 成绩表
Teacher(Tid, Tname) 教师表
create table Student
(
Sid int,
Sname nvarchar(32),
Sage int,
Ssex nvarchar(8)
);
create table Course
(
Cid int,
Cname nvarchar(32),
Tid int
);
create table SC
(
Sid int,
Cid int,
score int
);
create table Teacher
(
Tid int,
Tname nvarchar(16)
);
insert into Student
select 1,N'张一',18,N'男' union all
select 2,N'吕二',19,N'女' union all
select 3,N'岑三',17,N'男' union all
select 4,N'李四',18,N'女' union all
select 5,N'王五',17,N'男' union all
select 6,N'金六',19,N'女';
insert into Teacher
select 1,N'叶伟' union all
select 2,N'贺远' union all
select 3,N'李康' union all
select 4,N'王强';
insert into Course
select 1,N'语文',1 union all
select 2,N'数学',2 union all
select 3,N'英语',3 union all
select 4,N'化学',4;
insert into SC
select 1,1,56 union all
select 1,2,58 union all
select 1,3,47 union all
select 1,4,58 union all
select 2,1,89 union all
select 2,2,81 union all
select 2,3,92 union all
select 2,4,88 union all
select 3,1,91 union all
select 3,2,47 union all
select 3,3,78 union all
select 3,4,76 union all
select 4,2,88 union all
select 4,3,92 union all
select 4,4,93 union all
select 5,1,46 union all
select 5,3,78 union all
select 5,4,55 union all
select 6,1,54 union all
select 6,2,63 union all
select 6,4,78;
1、查询“1”课程比“2”课程成绩高的所有学生的学号;
select a.Sid
from (select * from SC where Cid=1) a, (select * from SC where Cid=2) b
where a.Sid = b.Sid and a.score > b.score;
2、查询平均成绩大于60分的同学的学号和平均成绩;
select sc.Sid, avg(sc.score)
from SC sc
group by sc.Sid
having avg(sc.score) > 60;
3、查询所有同学的学号、姓名、选课数、总成绩;
select st.Sid, st.Sname, count(sc.Cid), sum(sc.score)
from Student st join SC sc on st.Sid = sc.Sid
group by st.Sid, st.Cid;
4、查询姓“李”的老师的个数;
select count(*)
from Teacher t
where t.Tname like '李%';
5、查询没学过“李康”老师课的同学的学号、姓名;
select Sid, Sname
from Student
where Sid not in (
select distinct(sc.Sid)
from SC sc, Course c, Teacher t
where sc.Cid = c.Cid and c.Tid = t.Tid and t.Tname = '李康');
6、查询学过“1”课程也学过“2”课程的同学的学号、姓名;
select st.Sid, st.Sname
from Student st join SC sc1 on st.Sid = sc1.Sid join SC sc2 on sc1.Sid = sc2.Sid
where sc1.Cid = 1 and sc2.Cid = 2;
7、查询学过“王强”老师所教的所有课的同学的学号、姓名;
select Sid, Sname
from Student
where Sid in (
select sc.Sid
from SC sc, Course c, Teacher t
where sc.Cid = c.Cid and c.Tid = t.Tid and t.Tname = "王强"
group by sc.Sid
having count(sc.Cid)=(
select count(Cid)
from Course,Teacher
where Teacher.Tid=Course.Tid and Tname = '王强'));
8、查询课程“2”的成绩比课程“1”低的所有同学的学号、姓名;
select st.Sid, st.Sname
from Student st, (select * from SC where Cid=2) a, (select * from SC where Cid=1) b
where a.Sid = b.Sid and a.score < b.score and a.Sid = st.Sid;
9、查询所有课程成绩小于60分的同学的学号、姓名;
select distinct(st.Sid), st.Sname
from Student st join SC sc on st.Sid = sc.Sid
where sc.score < 60;
10、查询没有学全所有课的同学的学号、姓名;
select st.Sid, st.Sname
from Student st left join SC sc on stu.Sid = sc.Sid
group by st.Sid
having count(sc.Cid) < (select count(distinct Cid) from Course);
11、查询至少有一门课与“1”号同学所学相同的同学的学号和姓名;
select distinct st.Sid, st.Sname
from Student st, SC sc
where st.Sid = sc.Sid and sc.Sid != 1 and sc.Cid in (
select Cid from SC where Sid = 1);
12、查询至少学过“2”号同学所有一门课的其他同学学号和姓名;
select distinct st.Sid, st.Sname
from Student st, SC sc
where st.Sid = sc.Sid and sc.Sid != 2 and sc.Cid in (
select Cid from SC where Sid = 2);
13、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
select Cid as Cid, max(score) as maxScore, min(score) as minScore
from SC
group by Cid;
14、查询各科成绩前三名的记录:(不考虑成绩并列情况);
select sc.Sid, sc.Cid, sc.score
from SC sc
where score in (
select top 3 score
from SC
where sc.Cid = Cid
order by score desc)
order by sc.Cid;
15、查询每门课程被选修的学生数;
select Cid, count(Sid)
from SC
group by Cid;
16、查询出只选修了一门课程的全部学生的学号和姓名;
select st.Sid, st.Sname
from Student st left join SC sc on st.Sid = sc.Sid
group by st.Sid
having count(sc.Cid) = 1;
17、查询男生、女生人数;
select Ssex, count(*)
from Student
group by Ssex;
18、查询同名同性学生名单,并统计同名人数;
select Sname, count(*)
from Student
group by Sname, Ssex
having count(*) > 1;
19、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
select Cid, avg(score)
from SC
group by Cid
order by avg(score), Cid desc;
20、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select st.Sid, st.Sname, avg(sc.score)
from Student st, SC sc
where st.Sid = sc.Sid
group by sc.Sid, st.Sname
having avg(sc.score) > 85;
21、查询课程名称为“数据库”,且分数低于60 的学生姓名和分数;
select Sname
from Student st, SC sc, Course c
where st.Sid = sc.Sid and sc.Cid = c.Cid and c.Cname = '数据库' and sc.score < 60;
22、查询所有学生的选课情况;
selse st.Sid, st.Sname, c.Cname
from Student st, SC sc, Course c
where st.Sid = sc.Sid and sc.Cid = c.Cid;
23、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;
select st.Sname, c.Cname, sc.score
from Student st, SC sc, Course c
where st.Sid = sc.Sid and sc.Cid = c.Cid and sc.score > 80;
24、查询不及格的课程,并按课程号从大到小排列;
select Cid
from sc
where score < 60
order by Cid;
25、查询课程"1"且课程成绩在80分以上的学生的学号和姓名;
select st.Sid, st.Sname
from Student st, SC sc
where st.Sid = sc.Sid and sc.Cid = 1 and sc.score > 80;
26、求选了课程的学生人数;
select count(distinct(Sid)) from SC;
27、查询选修“叶伟”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
select st.Sname, sc.score
from Student st, SC sc, Course c, Teacher t
where st.Sid = sc.Sid and sc.Cid = c.Cid and c.Tid = t.Tid and t.Tname = "叶伟" and sc.score = (
select max(sc.score) from sc where sc.Cid = c.Cid);
28、查询各个课程及相应的选修人数;
select sc.Cid, count(distinct sc.Sid)
from SC sc
group by sc.Cid;
29、查询不同课程成绩相同的学生的学号、课程号、学生成绩;
select a.*
from sc a, sc b;
where a.score = b.score and a.Cid != b.Cid;
30、查询每门功课成绩最好的前两名;
select sc.Sid, sc.Cid, sc.score
from SC sc
where score in (
select top 2 score
from SC
where sc.Cid = Cid
order by score desc)
order by sc.Cid;
31、统计每门课程的学生选修人数(超过10 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列;
select Cid, count(*)
from SC
group by Cid
order by count(*) desc, Cid;
32、检索至少选修两门课程的学生学号;
select Sid
from SC
group by Sid
having count(*) >= 2;
33、查询全部学生都选修的课程的课程号和课程名;
select Cid, Cname
from Course
where Cid in (
select Cid
from SC
group by Cid
having count(*) = (
select count(*)
from Student));
34、查询全部有被选修的课程的课程号和课程名;
select Cid, Cname
from Course
where Cid in (
select Cid from SC group by Cid);
35、查询没学过“王强”老师讲授的任一门课程的学生姓名;
select Sname
from Student
where Sid not in (
select Sid
from SC
where Cid in (
select Cid
from Course, Teacher
where Course.Tid = Teacher.Tid and Teacher.Tname = "王强"));
36、查询两门以上不及格课程的同学的学号及其平均成绩;
select Sid, avg(score)
from SC
where Sid in (
select Sid
from SC
where score < 60 group by Sid having count(*) > 2)
group by Sid;
37、查询课程"4"分数小于60,按分数降序排列的同学学号;
select Sid
from SC
where score < 60 and Cid = 4
order by Sid desc;
SQL查询语句练习
最新推荐文章于 2023-04-27 15:08:04 发布