Database / SQL Code
Student(Sid,Sname,Sage,Ssex) 学生表
Sid:学号;Sname:学生姓名;Sage:学生年龄;Ssex:学生性别
Course(Cid,Cname,Tid) 课程表
Cid,课程编号;Cname:课程名字;Tid:教师编号
SC(Sid,Cid,score) 成绩表
Sid:学号;Cid,课程编号;score:成绩
Teacher(Tid,Tname) 教师表
Tid:教师编号; Tname:教师名字
CREATE TABLE Student
(
Sid INT PRIMARY KEY NOT NULL,
Sname nvarchar(32) NOT NULL,
Sage DATETIME NOT NULL,
Ssex nvarchar(8) NOT NULL
)
CREATE TABLE Course
(
Cid INT PRIMARY KEY,
Cname nvarchar(32) NOT NULL,
Tid INT NOT NULL
)
CREATE TABLE SC
(
Sid INT NOT NULL,
Cid INT NOT NULL,
score INT NOT NULL,
PRIMARY KEY(Sid , Cid)
)
CREATE TABLE Teacher
(
Tid INT PRIMARY KEY NOT NULL,
Tname nvarchar(16) NOT NULL
)
问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
SELECT Sid FROM SC AS a , SC AS b
where a.Sid = b.Sid
And a.Cid = "001"
And b.Cid = "002"
And a.score > b.score;
2、查询平均成绩大于60分的同学的学号和平均成绩;
SELECT Sid , AVG(Score) FROM SC
GROUP BY Sid HAVING AVG(Score) > 60;
3、查询所有同学的学号、姓名、选课数、总成绩;
SELECT Sid , Sname , COUNT(SC.Cid) as ClassNum , SUM(SC.Score) As Score
FROM Student
LEFT JOIN SC
ON Student.Sid = SC.Sid
GROUP BY Sid , Sname;
4、查询姓“李”的老师的个数;
SELECT COUNT(DISTINCT(Tname)) From Teacher WHERE Tname LIKE '李%';
5、查询没学过“叶平”老师课的同学的学号、姓名;
SELECT Sid , Sname FROM Student
WHERE Sid NOT IN(
SELECT DISTINCT Sid FROM SC , Course , Teacher
WHERE SC.Cid = Course.Cid
AND Teacher.Tid = Course.Tid
AND Teacher.Tname = '叶平'
)
);
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
SELECT Sid , Student.Sname FROM Student , SC
WHERE Sid = SC.Sid AND EXISTS(
Select * from SC as SC_2 where SC_2.SID = SC.SID and SC_2.Cid ='002'
);
7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
SELECT Sid , Sname FROM Student
WHERE Sid IN(
SELECT DISTINCT Sid FROM SC
WHERE cid IN (
SELECT Cid FROM course
WHERE Tid = (
SELECT Tid FROM Teacher
WHERE Tname = "叶平"
)
)
);
8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
SELECT DISTINCT Sid , Sname FROM Student , SC AS a , SC AS b
WHERE a.Sid = b.Sid
AND a.Cid = '001'
AND b.Cid = '002'
AND a.Score > b.Score
9、查询所有课程成绩小于60分的同学的学号、姓名;
SELECT Sid , Student.Sname FROM Student , SC
WHERE Sid = SC.Sid AND Sc.Score < 60;
13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
UPDATE SC
SET Score = (SELECT AVG(a.Score) FROM SC AS a WHERE a.Cid = SC.Cid)
FROM Course , Teacher
WHERE Teacher.Tid = Course.Tid
AND Course.Cid = SC.Cid
AND Teacher.Tname = '叶平';
14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
SELECT Sid , Student.Sname FROM Student , SC
WHERE Student.Sid = SC.Sid
AND Cid IN (SELECT Cid FROM SC WHERE Sid ='1002')
GROUP BY Sid
HAVING COUNT(*) = (SELECT COUNT(*) FROM SC WHERE Sid ='1002');
15、删除学习“叶平”老师课的SC表记录;
DELETE SC
FROM Course , Teacher
WHERE Course.Cid = SC.Cid
AND Teacher.Tid = Course.Tid
AND Teacher.Tname = '叶平';
17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分
SELECT Sid as StuID ,
(SELECT score FROM SC WHERE SC.Sid = t.id AND Cname = 'Database') AS DataBaseSYS ,
(SELECT score FROM SC WHERE SC.Sid = t.Sid AND Cname ='Management') AS Management ,
(SELECT score FROM SC WHERE SC.Sid = t.Sid AND Cname ='English ') AS English ,
COUNT(*) AS 有效课程数, AVG(t.score) AS 平均成绩
FROM SC AS t
GROUP BY Sid
ORDER BY AVG(t.score)
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT a.Cid AS CourseID , a.Score AS MaxScore , b.Score AS MinScore
FROM SC AS a , SC AS b
WHERE a.Cid = b.Cid
AND MaxScore = (
SELECT MAX(c.Score) FROM SC AS c , Student
WHERE c.Sid = Student.Sid
AND a.Cid = c.Cid
GROUP BY c.Cid
)
AND MinScore = (
SELECT MIN(d.Score) FROM SC AS d
WHERE b.Cid = d.Cid
GROUP BY d.Cid
);
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
SELECT SC.Cid , MAX(Course.Cname) , AVG(SC.SCORE) AS AVERAGE ,
100 * SUM(CASE WHEN SC.score >= 60 THEN 1 ELSE 0 END)/COUNT(*) AS PassRate
FROM SC , Course
WHERE SC.Cid = Course.Cid,
GROUP BY Cid ,
ORDER BY AVERAGE ASC, PassRate DESC;
21、查询不同老师所教不同课程平均分从高到低显示
SELECT MAX(Teacher.Tid) , MAX(Teacher.Tname) ,Course.cid , MAX(Course.Cname) ,AVG(SC.Score) AS AVERAGE
FROM SC , Teacher , Course
WHERE Teacher.Tid = Course.Tid
AND Course.Cid = SC.Cid
GROUP BY Course.Cid
ORDER BY AVG(Score) DESC;
23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
SELECT Cid , Cname ,
SUM(CASE WHEN SC.score < 60 THEN 1 ELSE 0 END) AS LowerThan60
SUM(CASE WHEN SC.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS Between60And70
SUM(CASE WHEN SC.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS Between70And85
SUM(CASE WHEN SC.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS Between85And100
FROM Course , SC
WHERE SC.Cid = Course.Cid
GROUP BY SC.Cid , Cname;
24、查询学生平均成绩及其名次
SELECT 1 + (SELECT COUNT(DISTINCT AVERAGE) FROM (
SELECT Sid , AVG(SCORE) AS AVERAGE
FROM SC
GROUP BY Sid
) AS T1 WHERE AVERAGE > T2.AVERAGE) AS Rank , Sid AS StuID ,
FROM (SELECT Sid, AVG(SCORE) AS AVERAGE
FROM SC
GROUP BY Sid
) AS T2
ORDER BY AVERAGE DESC;
25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT Sid , Cid , Score FROM SC a
WHERE SCORE IN (SELECT TOP 3 Score From SC
WHERE a.Cid = SC.Cid
ORDER BY Score DESC
)
ORDER BY Cid;
26、查询每门课程被选修的学生数
SELECT Cid ,COUNT(Sid) FROM SC GROUP BY Cid;
27、查询出只选修了一门课程的全部学生的学号和姓名
SELECT Sid , Sname , COUNT(Cid) AS numOfClass FROM Student , SC
WHERE Student.Sid = SC.Sid
GROUP BY Sid , Sname
HAVING COUNT(Cid) = 1;
28、查询男生、女生人数
SELECT COUNT(a.Ssex) AS Male FROM Student GROUP BY Ssex HAVING Ssex = '男';
SELECT COUNT(b.Ssex) AS Female FROM Student GROUP BY Ssex HAVING Ssex = '女';
29、查询姓“张”的学生名单
SELECT Sname FROM Student WHERE Sname like '张%';
30、查询同名同性学生名单,并统计同名人数
SELECT Sname , Ssex , COUNT(*) FROM Student
GROUP BY Sname
HAVING COUNT(*) > 1;
31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
SELECT Sname , Sid , CONVERT(char (11),DATEPART(year,Sage) AS AGE FROM Student
WHERE AGE = '1981';
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
SELECT Cid ,AVG(Score) FROM SC GROUP BY Cid ORDER BY AVG(Score), Cid DESC ;
33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
SELECT Sid , Sname , AVG(Score) AS AVERAGE FROM Student , SC
WHERE Student.Sid = SC.Sid
GROUP BY SC.Sid , Sname
HAVING AVG(Score) > 85;
34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
SELECT Sname , Score FROM Student , SC , Course
WHERE Student.Sid = SC.Sid
AND Course.Cid = SC.Cid
AND Course.Cname = '数据库'
AND Score < 60;
35、查询所有学生的选课情况;
SELECT Sid , Sname , Cid , Cname FROM Student , Course , SC
WHERE Student.Sid = SC.Sid
AND Course.Cid = SC.Cid;
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
SELECT DISTINCT Student.Sid , Student.Sname , SC.Cid , SC.Score FROM Student , SC
WHERE SC.score >= 70
AND SC.Sid = Student.Sid;
37、查询不及格的课程,并按课程号从大到小排列
SELECT Cid FROM SC WHERE Score < 60 ORDER BY Cid;
38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
SELECT Sid , Sname FROM Student , SC
WHERE Student.Sid = SC.Sid
AND SC.Cid = '003'
AND SC.Score > 80;
40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
SELECT Student.Sname , Score FROM Student , SC , Course
WHERE Student.Sid = SC.Sid
AND Course.Cid = SC.Cid
AND Course.Tname = '叶平'
AND Score = (SELECT Max(Score) FROM SC WHERE Cid = SC.Cid )
41、查询各个课程及相应的选修人数
SELECT COUNT(*) FROM SC
GROUP BY Cid;
43、查询每门功成绩最好的前两名
SELECT Sid , Cid , Score FROM SC AS a
WHERE Score IN (
SELECT TOP 2 Score FROM SC
WHERE a.Cid = SC.Cid
ORDER BY Score DESC
)
ORDER BY a.Cid;
44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT Cid as ClassId, COUNT(*) AS counts
FROM SC
GROUP BY Cid
HAVING COUNT(*) > 10
ORDER BY COUNT(*) DESC , Cid ASC;
45、检索至少选修两门课程的学生学号
SELECT Sid FROM SC
GROUP BY Sid
HAVING COUNT(*) >= 2;
46、查询全部学生都选修的课程的课程号和课程名
SELECT Cid , Cname
FROM Course
WHERE Cid in (SELECT Cid FROM SC
GROUP BY Cid
);
47、查询没学过“叶平”老师讲授的任一门课程的学生姓名
SELECT Sname FROM Student
WHERE Sid NOT IN (SELECT Sid FROM SC , Course , Teacher
WHERE Course.Cid = SC.Cid
AND Teacher.Tid = Course.Tid
AND Teacher.Tname = '叶平';
);
48、查询两门以上不及格课程的同学的学号及其平均成绩
SELECT Sid , AVG(isnull(Score , 0)) FROM SC
WHERE Sid in (SELECT Sid FROM SC
WHERE Score < 60
GROUP BY Sid
HAVING count(*) > 2
)
GROUP BY Sid;
49、检索“004”课程分数小于60,按分数降序排列的同学学号
SELECT Sid FROM SC
WHERE Score < 60
AND Cid = "004"
ORDER BY Score DESC;
50、删除“002”同学的“001”课程的成绩
DELETE From SC
WHERE Sid = "002" AND Cid = '001'