–2.课程表
Course(C,Cname,T) --C --课程编号,Cname 课程名称,T 教师编号
–3.教师表
Teacher(T,Tname) --T 教师编号,Tname 教师姓名
–4.成绩表
SC(S,C,score) --S 学生编号,C 课程编号,score 分数
学生表 Student——
-- 学生表
# Student(S,Sname,Sage,Ssex) --S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
CREATE TABLE Student ( S varchar ( 10 ),Sname varchar ( 10 ), Sage datetime, Ssex varchar ( 20 ) );
insert into Student values('01' , '赵雷' , '1990-01-01' , N'男');
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' , '女');
-- 2.课程表
# Course(C,Cname,T) --C --课程编号,Cname 课程名称,T 教师编号
CREATE TABLE Course ( C VARCHAR ( 20 ), Cname varchar ( 20 ), T varchar ( 20 ) );
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 3.教师表
# Teacher(T,Tname) --T 教师编号,Tname 教师姓名
CREATE TABLE Teacher ( T varchar( 10 ), Tname varchar( 10 ));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 4.成绩表
# SC(S,C,score) --S 学生编号,C 课程编号,score 分数
CREATE TABLE SC ( S varchar ( 10 ), C varchar ( 10 ), score DECIMAL (18, 1 ) );
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
-- 1.查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT
a.S AS "学生编号",
Sname AS "学生姓名",
a.score AS "01课程成绩",
b.score AS "02课程成绩"
FROM
( SELECT S, C, score FROM SC WHERE C = '01' ) AS a #a 01成绩子表
INNER JOIN ( SELECT S, C, score FROM SC WHERE C = '02' ) AS b ON a.S = b.S #b 02成绩子表
INNER JOIN Student AS c ON c.S = b.S #c表 内连接获得学生姓名
WHERE
a.score > b.score #01成绩大于02成绩
-- 2.查询平均成绩大于60分的学生学号和平均成绩
SELECT
S as "学生编号",
avg( score ) as "平均成绩(>60)"
FROM
SC
GROUP BY #分组查询,把每个学生做为一个组
S
HAVING
avg( score )> 60 #每个学生的3科平均分
-- 3.查询所有学生的学号、姓名、选课数、总成绩
# Student 、SC
SELECT
a.S AS "学生编号",
a.Sname AS "姓名",
count( b.C ) AS "选课数",
sum( CASE WHEN b.score IS NULL THEN 0 ELSE b.score END ) AS "总成绩"
# 王菊的成绩为null 让null默认为0。case when 值 is null then 默认值 else 值 end 值为空 则为默认值0 ,不为0则为总成绩
FROM
Student AS a
LEFT JOIN SC AS b ON a.S = b.S #左连接 可能存在学生表里的人 但他没有成绩 或者选课 所以把学生表保留
GROUP BY
a.S,
a.Sname #严谨写法 加上a.name 因为一般情况select,只允许出现group by内参数有关的统计值
-- 4. 查询姓张的老师个数
SELECT
count( T )
FROM
teacher
WHERE
Tname LIKE '张%'
# 可能有去重需求 用distinct (会略减低sql效率)
SELECT
count(distinct T ) #distinct 删除重复的值
FROM
teacher
WHERE
Tname LIKE '张%' # %表示任意字符 。若找名字中带三字的 '%三%'
# 多个子查询嵌套 。找到学过张三老师的课,再not in
#注意 不能再成绩表中 找课程编号不等于张三的课程编号。这样只过滤了不等于01的记录。输出的结果课程为02、03的同学也输出了。就错了
SELECT S AS "学号",Sname AS "姓名" FROM student
WHERE S NOT IN
(SELECT S from SC
where C=(
select C from Course
where T=(
select T from teacher
where Tname='张三' )))
#另一种方法 表连接。也是先找出学过张三,再取反
SELECT S AS "学号",Sname AS "姓名" FROM student
WHERE S NOT IN
(SELECT S FROM SC AS a
INNER JOIN course AS b ON a.C = b.C
INNER JOIN teacher AS c ON b.T = c.T
where Tname='张三')