大部分参考自http://blog.sina.com.cn/s/blog_7e5541250100ygb1.html,在此基础上,所有的代码我都自己敲了一遍!

1、问题及描述:

学生表


Student(Sid,Sname,Sage,Ssex) --Sid 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别


课程表


Course(Cid,Cname,Tid) --Cid --课程编号,Cname 课程名称,T# 教师编号


教师表


Teacher(Tid,Tname) --Tid 教师编号,Tname 教师姓名


成绩表


SC(Sid,Cid,score) --Sid 学生编号,Cid 课程编号,score 分数



2、创建测试数据


create table Student(Sid varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
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' , '女');
create table Course(Cid varchar(10),Cname varchar(10),Tid varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
create table Teacher(Tid varchar(10),Tname nvarchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
create table SC(Sid varchar(10),Cid 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);

3、例子


--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
--1.1、查询同时存在"01"课程和"02"课程的情况

SELECT
a.* , b.score, c.score 
FROM Student a, sc b, sc c
WHERE a.Sid = b.Sid AND a.Sid = c.Sid AND b.Cid = "01" AND c.Cid = "02" AND b.score > c.score

--1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)


SELECT
a.* , b.score,c.score 
FROM Student a 
LEFT JOIN SC b ON a.Sid = b.Sid AND b.Cid = '01'
LEFT JOIN SC c ON a.Sid = c.Sid AND c.Cid = '02'
where b.score IS NOT NULL



--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数


--2.1、查询同时存在"01"课程和"02"课程的情况


SELECT
a.* , b.score, c.score 
FROM Student a, sc b, sc c
WHERE a.Sid = b.Sid AND a.Sid = c.Sid AND b.Cid = "01" AND c.Cid = "02" AND b.score < c.score

--2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况


SELECT
a.* , b.score,c.score 
FROM Student a 
LEFT JOIN SC b ON a.Sid = b.Sid AND b.Cid = '01'
LEFT JOIN SC c ON a.Sid = c.Sid AND c.Cid = '02'
where c.score IS NOT NULL



--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩


SELECT
a.Sid, a.Sname , AVG(b.score)
FROM Student a, sc b
WHERE a.Sid = b.Sid
GROUP BY a.Sid
HAVING AVG(b.score)>60



--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩


--4.1、查询在sc表存在成绩的学生信息的SQL语句。


SELECT
a.Sid, a.Sname , AVG(b.score)
FROM Student a, sc b
WHERE a.Sid = b.Sid
GROUP BY a.Sid
HAVING AVG(b.score)<60


--4.2、查询在sc表中不存在成绩的学生信息的SQL语句。

SELECT
a.Sid, a.Sname , AVG(b.score)
FROM Student a
LEFT JOIN sc b ON a.Sid = b.Sid
GROUP BY a.Sid
HAVING ISNULL(AVG(b.score))



--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩


--5.1、查询所有有成绩的SQL。


SELECT
a.Sid, a.Sname, count(b.Cid) , SUM(b.score)
FROM Student a
LEFT JOIN sc b ON a.Sid = b.Sid
GROUP BY a.Sid



--6、查询"李"姓老师的数量


--方法1


SELECT
count(a.Tid)
FROM teacher a
WHERE a.Tname LIKE "李%"

--方法2


SELECT
count(a.Tid)
FROM teacher a
WHERE LEFT(a.Tname,1) LIKE "李%"



--7、查询学过"张三"老师授课的同学的信息


SELECT
a.*
FROM student a, teacher b, sc c, course d
WHERE c.Sid = a.Sid AND c.Cid = d.Cid AND d.Tid = b.Tid AND b.Tname = "张三"




--8、查询没学过"张三"老师授课的同学的信息

SELECT
a.*
FROM student a
WHERE a.Sid 
NOT IN
(
SELECT DISTINCT b.Sid FROM sc b, course c, teacher d
WHERE b.Cid = c.Cid AND c.Tid = d.Tid AND d.Tname = "张三"
)



--9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息


--方法1


SELECT
a.*
FROM student a, sc b, sc c
WHERE a.Sid = b.Sid AND a.Sid = c.Sid AND b.Cid = "01" AND c.Cid = "02"


--方法2

SELECT a.* 
FROM student a, sc b
WHERE a.Sid = b.Sid AND b.Cid = '01' AND 
EXISTS (SELECT 1 FROM SC c WHERE c.Sid = b.Sid AND c.Cid = '02')


--方法3

SELECT a.* 
FROM student a
WHERE Sid in(
SELECT Sid FROM
(SELECT DISTINCT Sid FROM sc WHERE Cid = "01"
UNION ALL
SELECT Sid FROM sc WHERE Cid = "02"
) t 
GROUP BY Sid
HAVING count(1) = 2 )



--10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息


--方法1


SELECT a.* 
FROM student a, sc b
WHERE a.Sid = b.Sid AND b.Cid = "01" AND 
NOT EXISTS(
SELECT 1 
FROM sc c
WHERE a.Sid = c.Sid AND c.Cid = "02" )

--方法2


SELECT a.* 
FROM student a, sc b
WHERE a.Sid = b.Sid AND b.Cid = "01" AND 
a.Sid NOT IN(
SELECT c.Sid 
FROM sc c
WHERE a.Sid = c.Sid and c.Cid ="02"
)



--11、查询没有学全所有课程的同学的信息


--11.1、没有学习课程则不会列出来


SELECT a.* 
FROM student a, sc b
WHERE a.Sid = b.Sid
GROUP BY a.Sid
HAVING count(b.score) < (SELECT count(1) FROM course)

--11.2没学过课程的也会列出来


SELECT a.* 
FROM student a
LEFT JOIN sc b ON a.Sid = b.Sid
GROUP BY a.Sid
HAVING count(b.score) < (SELECT count(1) FROM course)



--12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息


SELECT DISTINCT a.* 
FROM student a, sc b 
WHERE a.Sid = b.Sid AND b.Cid IN
(
SELECT DISTINCT c.Cid 
FROM sc c
WHERE c.Sid = "01"
) 
AND a.Sid <> "01"



--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 (开始还想着一个个比较的)


SELECT a.* 
FROM student a, sc b 
WHERE a.Sid = b.Sid AND a.Sid <> "01" AND b.Cid IN
(
SELECT DISTINCT c.Cid 
FROM sc c
WHERE c.Sid = "01"
) 
GROUP BY(a.Sid)
HAVING count(1) = (SELECT COUNT(1) FROM sc  WHERE sc.Sid = "01")



--14、查询没学过"张三"老师讲授的任一门课程的学生姓名 


--方法1

SELECT a.* 
FROM student a
WHERE a.Sid NOT IN
(
SELECT DISTINCT a.Sid
FROM sc b, course c, teacher d
WHERE a.Sid = b.Sid AND b.Cid = c.Cid AND c.Tid = d.Tid AND d.Tname = "张三"
)

--方法2(使用not exists速度更快)


SELECT a.* 
FROM student a
WHERE  NOT EXISTS
(
SELECT DISTINCT a.Sid
FROM sc b, course c, teacher d
WHERE a.Sid = b.Sid AND b.Cid = c.Cid AND c.Tid = d.Tid AND d.Tname = "张三"
)



--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩


SELECT a.Sid, a.Sname, AVG(b.score) 
FROM student a, sc b
WHERE a.Sid = b.Sid AND b.score < 60
GROUP BY a.Sid
HAVING count(1) >= 2



--16、检索"01"课程分数小于60,按分数降序排列的学生信息


SELECT a.*
FROM student a, sc b
WHERE a.Sid = b.Sid AND b.Cid = "01" AND b.score < 60
ORDER BY b.score DESC



--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩


SELECT a.Sid ,a.Sname, 
MAX(CASE c.Cname WHEN "语文" THEN b.score ELSE NULL END) "[语文]",
MAX(CASE c.Cname WHEN "数学" THEN b.score ELSE NULL END) "[数学]",
MAX(CASE c.Cname WHEN "英语" THEN b.score ELSE NULL END) "[英语]",
AVG(b.score) "[平均分]"
FROM student a
LEFT JOIN sc b on a.Sid = b.Sid
LEFT JOIN course c on b.Cid = c.Cid
GROUP BY a.Sid
ORDER BY AVG(b.score) DESC


为什么没有MAX函数的话每行的成绩只有一个有数据???