2019独角兽企业重金招聘Python工程师标准>>>
SQL语句多表查询,转自一位大神几年前的文章,方便自己查看!!
-问题及描述:
--1.学生表
--Student(S#,Sname,Sage,Ssex) --S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表
--Course(C#,Cname,T#) --C# --课程编号,Cname 课程名称,T# 教师编号
--3.教师表
--Teacher(T#,Tname) --T# 教师编号,Tname 教师姓名
--4.成绩表
--SC(S#,C#,score) --S# 学生编号,C# 课程编号,score 分数
--创建测试数据
CREATE TABLE Student
(
S# VARCHAR(10) ,
Sname NVARCHAR(10) ,
Sage DATETIME ,
Ssex NVARCHAR(10)
)
INSERT INTO Student
VALUES ( '01', N'赵雷', '1990-01-01', N'男' )
INSERT INTO Student
VALUES ( '02', N'钱电', '1990-12-21', N'男' )
INSERT INTO Student
VALUES ( '03', N'孙风', '1990-05-20', N'男' )
INSERT INTO Student
VALUES ( '04', N'李云', '1990-08-06', N'男' )
INSERT INTO Student
VALUES ( '05', N'周梅', '1991-12-01', N'女' )
INSERT INTO Student
VALUES ( '06', N'吴兰', '1992-03-01', N'女' )
INSERT INTO Student
VALUES ( '07', N'郑竹', '1989-07-01', N'女' )
INSERT INTO Student
VALUES ( '08', N'王菊', '1990-01-20', N'女' )
CREATE TABLE Course
(
C# VARCHAR(10) ,
Cname NVARCHAR(10) ,
T# VARCHAR(10)
)
INSERT INTO Course
VALUES ( '01', N'语文', '02' )
INSERT INTO Course
VALUES ( '02', N'数学', '01' )
INSERT INTO Course
VALUES ( '03', N'英语', '03' )
CREATE TABLE Teacher
(
T# VARCHAR(10) ,
Tname NVARCHAR(10)
)
INSERT INTO Teacher
VALUES ( '01', N'张三' )
INSERT INTO Teacher
VALUES ( '02', N'李四' )
INSERT INTO Teacher
VALUES ( '03', N'王五' )
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 )
go
--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
--1.1、查询同时存在"01"课程和"02"课程的情况
SELECT a.* ,
b.score [课程'01'的分数] ,
c.score [课程'02'的分数]
FROM Student a ,
SC b ,
SC c
WHERE a.S# = b.S#
AND a.S# = c.S#
AND b.C# = '01'
AND c.C# = '02'
AND b.score > c.score
--1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)
SELECT a.* ,
b.score [课程"01"的分数] ,
c.score [课程"02"的分数]
FROM Student a
LEFT JOIN SC b ON a.S# = b.S#
AND b.C# = '01'
LEFT JOIN SC c ON a.S# = c.S#
AND c.C# = '02'
WHERE b.score > ISNULL(c.score, 0)
--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数 --3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 --4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 --5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 --6、查询"李"姓老师的数量 --8、查询没学过"张三"老师授课的同学的信息 --9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 --10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 --11、查询没有学全所有课程的同学的信息 --12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 --13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 --14、查询没学过"张三"老师讲授的任一门课程的学生姓名 --15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 --16、检索"01"课程分数小于60,按分数降序排列的学生信息 --17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 SELECT t1.* , SELECT t.* , --26、查询每门课程被选修的学生数 --27、查询出只有两门课程的全部学生的学号和姓名 --28、查询男生、女生人数 --29、查询名字中含有"风"字的学生信息 --30、查询同名同性学生名单,并统计同名人数 --31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime) --32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 --33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 --34、查询课程名称为"数学",且分数低于60的学生姓名和分数 --35、查询所有学生的课程及分数情况; --36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; --37、查询不及格的课程 --38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; --39、求每门课程的学生人数 --40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩 --41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 --42、查询每门功成绩最好的前两名 --43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 --44、检索至少选修两门课程的学生学号 --45、查询选修了全部课程的学生信息 --46、查询各学生的年龄 --47、查询本周过生日的学生 --48、查询下周过生日的学生 --49、查询本月过生日的学生 --50、查询下月过生日的学生 DROP TABLE Student,Course,Teacher,SC
--2.1、查询同时存在"01"课程和"02"课程的情况
SELECT a.* ,
b.score [课程'01'的分数] ,
c.score [课程'02'的分数]
FROM Student a ,
SC b ,
SC c
WHERE a.S# = b.S#
AND a.S# = c.S#
AND b.C# = '01'
AND c.C# = '02'
AND b.score
SELECT a.* ,
b.score [课程"01"的分数] ,
c.score [课程"02"的分数]
FROM Student a
LEFT JOIN SC b ON a.S# = b.S#
AND b.C# = '01'
LEFT JOIN SC c ON a.S# = c.S#
AND c.C# = '02'
WHERE ISNULL(b.score, 0)
SELECT a.S# ,
a.Sname ,
CAST(AVG(b.score) AS DECIMAL(18, 2)) avg_score
FROM Student a ,
sc b
WHERE a.S# = b.S#
GROUP BY a.S# ,
a.Sname
HAVING CAST(AVG(b.score) AS DECIMAL(18, 2)) >= 60
ORDER BY a.S#
--4.1、查询在sc表存在成绩的学生信息的SQL语句。
SELECT a.S# ,
a.Sname ,
CAST(AVG(b.score) AS DECIMAL(18, 2)) avg_score
FROM Student a ,
sc b
WHERE a.S# = b.S#
GROUP BY a.S# ,
a.Sname
HAVING CAST(AVG(b.score) AS DECIMAL(18, 2)) <60
ORDER BY a.S#
--4.2、查询在sc表中不存在成绩的学生信息的SQL语句。
SELECT a.S# ,
a.Sname ,
ISNULL(CAST(AVG(b.score) AS DECIMAL(18, 2)), 0) avg_score
FROM Student a
LEFT JOIN sc b ON a.S# &#61; b.S#
GROUP BY a.S# ,
a.Sname
HAVING ISNULL(CAST(AVG(b.score) AS DECIMAL(18, 2)), 0) <60
ORDER BY a.S#
--5.1、查询所有有成绩的SQL。
SELECT a.S# [学生编号] ,
a.Sname [学生姓名] ,
COUNT(b.C#) 选课总数 ,
SUM(score) [所有课程的总成绩]
FROM Student a ,
SC b
WHERE a.S# &#61; b.S#
GROUP BY a.S# ,
a.Sname
ORDER BY a.S#
--5.2、查询所有(包括有成绩和无成绩)的SQL。
SELECT a.S# [学生编号] ,
a.Sname [学生姓名] ,
COUNT(b.C#) 选课总数 ,
SUM(score) [所有课程的总成绩]
FROM Student a
LEFT JOIN SC b ON a.S# &#61; b.S#
GROUP BY a.S# ,
a.Sname
ORDER BY a.S#
--方法1
SELECT COUNT(Tname) ["李"姓老师的数量]
FROM Teacher
WHERE Tname LIKE N&#39;李%&#39;
--方法2
SELECT COUNT(Tname) ["李"姓老师的数量]
FROM Teacher
WHERE LEFT(Tname, 1) &#61; N&#39;李&#39;
--7、查询学过"张三"老师授课的同学的信息
SELECT DISTINCT
Student.*
FROM Student ,
SC ,
Course ,
Teacher
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; Course.C#
AND Course.T# &#61; Teacher.T#
AND Teacher.Tname &#61; N&#39;张三&#39;
ORDER BY Student.S#
SELECT m.*
FROM Student m
WHERE S# NOT IN ( SELECT DISTINCT
SC.S#
FROM SC ,
Course ,
Teacher
WHERE SC.C# &#61; Course.C#
AND Course.T# &#61; Teacher.T#
AND Teacher.Tname &#61; N&#39;张三&#39; )
ORDER BY m.S#
--方法1
SELECT Student.*
FROM Student ,
SC
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; &#39;01&#39;
AND EXISTS ( SELECT 1
FROM SC SC_2
WHERE SC_2.S# &#61; SC.S#
AND SC_2.C# &#61; &#39;02&#39; )
ORDER BY Student.S#
--方法2
SELECT Student.*
FROM Student ,
SC
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; &#39;02&#39;
AND EXISTS ( SELECT 1
FROM SC SC_2
WHERE SC_2.S# &#61; SC.S#
AND SC_2.C# &#61; &#39;01&#39; )
ORDER BY Student.S#
--方法3
SELECT m.*
FROM Student m
WHERE S# IN ( SELECT S#
FROM ( SELECT DISTINCT
S#
FROM SC
WHERE C# &#61; &#39;01&#39;
UNION ALL
SELECT DISTINCT
S#
FROM SC
WHERE C# &#61; &#39;02&#39;
) t
GROUP BY S#
HAVING COUNT(1) &#61; 2 )
ORDER BY m.S#
--方法1
SELECT Student.*
FROM Student ,
SC
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; &#39;01&#39;
AND NOT EXISTS ( SELECT 1
FROM SC SC_2
WHERE SC_2.S# &#61; SC.S#
AND SC_2.C# &#61; &#39;02&#39; )
ORDER BY Student.S#
--方法2
SELECT Student.*
FROM Student ,
SC
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; &#39;01&#39;
AND Student.S# NOT IN ( SELECT SC_2.S#
FROM SC SC_2
WHERE SC_2.S# &#61; SC.S#
AND SC_2.C# &#61; &#39;02&#39; )
ORDER BY Student.S#
--11.1、
SELECT Student.*
FROM Student ,
SC
WHERE Student.S# &#61; SC.S#
GROUP BY Student.S# ,
Student.Sname ,
Student.Sage ,
Student.Ssex
HAVING COUNT(C#) <( SELECT COUNT(C#)
FROM Course
)
--11.2
SELECT Student.*
FROM Student
LEFT JOIN SC ON Student.S# &#61; SC.S#
GROUP BY Student.S# ,
Student.Sname ,
Student.Sage ,
Student.Ssex
HAVING COUNT(C#) <( SELECT COUNT(C#)
FROM Course
)
SELECT DISTINCT
Student.*
FROM Student ,
SC
WHERE Student.S# &#61; SC.S#
AND SC.C# IN ( SELECT C#
FROM SC
WHERE S# &#61; &#39;01&#39; )
AND Student.S# <> &#39;01&#39;
SELECT Student.*
FROM Student
WHERE S# IN ( SELECT DISTINCT
SC.S#
FROM SC
WHERE S# <> &#39;01&#39;
AND SC.C# IN ( SELECT DISTINCT
C#
FROM SC
WHERE S# &#61; &#39;01&#39; )
GROUP BY SC.S#
HAVING COUNT(1) &#61; ( SELECT COUNT(1)
FROM SC
WHERE S# &#61; &#39;01&#39;
) )
SELECT student.*
FROM student
WHERE student.S# NOT IN ( SELECT DISTINCT
sc.S#
FROM sc ,
course ,
teacher
WHERE sc.C# &#61; course.C#
AND course.T# &#61; teacher.T#
AND teacher.tname &#61; N&#39;张三&#39; )
ORDER BY student.S#
SELECT student.S# ,
student.sname ,
CAST(AVG(score) AS DECIMAL(18, 2)) avg_score
FROM student ,
sc
WHERE student.S# &#61; SC.S#
AND student.S# IN ( SELECT S#
FROM SC
WHERE score <60
GROUP BY S#
HAVING COUNT(1) >&#61; 2 )
GROUP BY student.S# ,
student.sname
SELECT student.* ,
sc.C# ,
sc.score
FROM student ,
sc
WHERE student.S# &#61; SC.S#
AND sc.score <60
AND sc.C# &#61; &#39;01&#39;
ORDER BY sc.score DESC
--17.1 SQL 2000 静态
SELECT a.S# 学生编号 ,
a.Sname 学生姓名 ,
MAX(CASE c.Cname
WHEN N&#39;语文&#39; THEN b.score
ELSE NULL
END) [语文] ,
MAX(CASE c.Cname
WHEN N&#39;数学&#39; THEN b.score
ELSE NULL
END) [数学] ,
MAX(CASE c.Cname
WHEN N&#39;英语&#39; THEN b.score
ELSE NULL
END) [英语] ,
CAST(AVG(b.score) AS DECIMAL(18, 2)) 平均分
FROM Student a
LEFT JOIN SC b ON a.S# &#61; b.S#
LEFT JOIN Course c ON b.C# &#61; c.C#
GROUP BY a.S# ,
a.Sname
ORDER BY 平均分 DESC
--17.2 SQL 2000 动态
DECLARE &#64;sql NVARCHAR(4000)
SET &#64;sql &#61; &#39;select a.S# &#39; &#43; N&#39;学生编号&#39; &#43; &#39; , a.Sname &#39; &#43; N&#39;学生姓名&#39;
SELECT &#64;sql &#61; &#64;sql &#43; &#39;,max(case c.Cname when N&#39;&#39;&#39; &#43; Cname
&#43; &#39;&#39;&#39; then b.score else null end) [&#39; &#43; Cname &#43; &#39;]&#39;
FROM ( SELECT DISTINCT
Cname
FROM Course
) AS t
SET &#64;sql &#61; &#64;sql &#43; &#39; , cast(avg(b.score) as decimal(18,2)) &#39; &#43; N&#39;平均分&#39;
&#43; &#39; from Student a left join SC b on a.S# &#61; b.S# left join Course c on b.C# &#61; c.C#
group by a.S# , a.Sname order by &#39; &#43; N&#39;平均分&#39; &#43; &#39; desc&#39;
EXEC(&#64;sql)
--24、查询学生平均成绩及其名次
--24.1 查询学生的平均成绩并进行排名&#xff0c;sql 2000用子查询完成&#xff0c;分平均成绩重复时保留名次空缺和不保留名次空缺两种。
SELECT t1.* ,
px &#61; ( SELECT COUNT(1)
FROM ( SELECT m.S# [学生编号] ,
m.Sname [学生姓名] ,
ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)),
0) [平均成绩]
FROM Student m
LEFT JOIN SC n ON m.S# &#61; n.S#
GROUP BY m.S# ,
m.Sname
) t2
WHERE 平均成绩 > t1.平均成绩
) &#43; 1
FROM ( SELECT m.S# [学生编号] ,
m.Sname [学生姓名] ,
ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)), 0) [平均成绩]
FROM Student m
LEFT JOIN SC n ON m.S# &#61; n.S#
GROUP BY m.S# ,
m.Sname
) t1
ORDER BY px
px &#61; ( SELECT COUNT(DISTINCT 平均成绩)
FROM ( SELECT m.S# [学生编号] ,
m.Sname [学生姓名] ,
ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)),
0) [平均成绩]
FROM Student m
LEFT JOIN SC n ON m.S# &#61; n.S#
GROUP BY m.S# ,
m.Sname
) t2
WHERE 平均成绩 >&#61; t1.平均成绩
)
FROM ( SELECT m.S# [学生编号] ,
m.Sname [学生姓名] ,
ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)), 0) [平均成绩]
FROM Student m
LEFT JOIN SC n ON m.S# &#61; n.S#
GROUP BY m.S# ,
m.Sname
) t1
ORDER BY px
--24.2 查询学生的平均成绩并进行排名&#xff0c;sql 2005用rank,DENSE_RANK完成&#xff0c;分平均成绩重复时保留名次空缺和不保留名次空缺两种。
SELECT t.* ,
px &#61; RANK() OVER ( ORDER BY [平均成绩] DESC )
FROM ( SELECT m.S# [学生编号] ,
m.Sname [学生姓名] ,
ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)), 0) [平均成绩]
FROM Student m
LEFT JOIN SC n ON m.S# &#61; n.S#
GROUP BY m.S# ,
m.Sname
) t
ORDER BY px
px &#61; DENSE_RANK() OVER ( ORDER BY [平均成绩] DESC )
FROM ( SELECT m.S# [学生编号] ,
m.Sname [学生姓名] ,
ISNULL(CAST(AVG(score) AS DECIMAL(18, 2)), 0) [平均成绩]
FROM Student m
LEFT JOIN SC n ON m.S# &#61; n.S#
GROUP BY m.S# ,
m.Sname
) t
ORDER BY px
--25、查询各科成绩前三名的记录
--25.1 分数重复时保留名次空缺
SELECT m.* ,
n.C# ,
n.score
FROM Student m ,
SC n
WHERE m.S# &#61; n.S#
AND n.score IN ( SELECT TOP 3
score
FROM sc
WHERE C# &#61; n.C#
ORDER BY score DESC )
ORDER BY n.C# ,
n.score DESC
--25.2 分数重复时不保留名次空缺&#xff0c;合并名次
--sql 2000用子查询实现
SELECT *
FROM ( SELECT t.* ,
px &#61; ( SELECT COUNT(DISTINCT score)
FROM SC
WHERE C# &#61; t.C#
AND score >&#61; t.score
)
FROM sc t
) m
WHERE px BETWEEN 1 AND 3
ORDER BY m.c# ,
m.px
--sql 2005用DENSE_RANK实现
SELECT *
FROM ( SELECT t.* ,
px &#61; DENSE_RANK() OVER ( PARTITION BY c# ORDER BY score DESC )
FROM sc t
) m
WHERE px BETWEEN 1 AND 3
ORDER BY m.C# ,
m.px
SELECT c# ,
COUNT(S#) [学生数]
FROM sc
GROUP BY C#
SELECT Student.S# ,
Student.Sname
FROM Student ,
SC
WHERE Student.S# &#61; SC.S#
GROUP BY Student.S# ,
Student.Sname
HAVING COUNT(SC.C#) &#61; 2
ORDER BY Student.S#
SELECT COUNT(Ssex) AS 男生人数
FROM Student
WHERE Ssex &#61; N&#39;男&#39;
SELECT COUNT(Ssex) AS 女生人数
FROM Student
WHERE Ssex &#61; N&#39;女&#39;
SELECT SUM(CASE WHEN Ssex &#61; N&#39;男&#39; THEN 1
ELSE 0
END) [男生人数] ,
SUM(CASE WHEN Ssex &#61; N&#39;女&#39; THEN 1
ELSE 0
END) [女生人数]
FROM student
SELECT CASE WHEN Ssex &#61; N&#39;男&#39; THEN N&#39;男生人数&#39;
ELSE N&#39;女生人数&#39;
END [男女情况] ,
COUNT(1) [人数]
FROM student
GROUP BY CASE WHEN Ssex &#61; N&#39;男&#39; THEN N&#39;男生人数&#39;
ELSE N&#39;女生人数&#39;
END
SELECT *
FROM student
WHERE sname LIKE N&#39;%风%&#39;
SELECT *
FROM student
WHERE CHARINDEX(N&#39;风&#39;, sname) > 0
SELECT Sname [学生姓名] ,
COUNT(*) [人数]
FROM Student
GROUP BY Sname
HAVING COUNT(*) > 1
SELECT *
FROM Student
WHERE YEAR(sage) &#61; 1990
SELECT *
FROM Student
WHERE DATEDIFF(yy, sage, &#39;1990-01-01&#39;) &#61; 0
SELECT *
FROM Student
WHERE DATEPART(yy, sage) &#61; 1990
SELECT *
FROM Student
WHERE CONVERT(VARCHAR(4), sage, 120) &#61; &#39;1990&#39;
SELECT m.C# ,
m.Cname ,
CAST(AVG(n.score) AS DECIMAL(18, 2)) avg_score
FROM Course m ,
SC n
WHERE m.C# &#61; n.C#
GROUP BY m.C# ,
m.Cname
ORDER BY avg_score DESC ,
m.C# ASC
SELECT a.S# ,
a.Sname ,
CAST(AVG(b.score) AS DECIMAL(18, 2)) avg_score
FROM Student a ,
sc b
WHERE a.S# &#61; b.S#
GROUP BY a.S# ,
a.Sname
HAVING CAST(AVG(b.score) AS DECIMAL(18, 2)) >&#61; 85
ORDER BY a.S#
SELECT sname ,
score
FROM Student ,
SC ,
Course
WHERE SC.S# &#61; Student.S#
AND SC.C# &#61; Course.C#
AND Course.Cname &#61; N&#39;数学&#39;
AND score <60
SELECT Student.* ,
Course.Cname ,
SC.C# ,
SC.score
FROM Student ,
SC ,
Course
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; Course.C#
ORDER BY Student.S# ,
SC.C#
SELECT Student.* ,
Course.Cname ,
SC.C# ,
SC.score
FROM Student ,
SC ,
Course
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; Course.C#
AND SC.score >&#61; 70
ORDER BY Student.S# ,
SC.C#
SELECT Student.* ,
Course.Cname ,
SC.C# ,
SC.score
FROM Student ,
SC ,
Course
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; Course.C#
AND SC.score <60
ORDER BY Student.S# ,
SC.C#
SELECT Student.* ,
Course.Cname ,
SC.C# ,
SC.score
FROM Student ,
SC ,
Course
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; Course.C#
AND SC.C# &#61; &#39;01&#39;
AND SC.score >&#61; 80
ORDER BY Student.S# ,
SC.C#
SELECT Course.C# ,
Course.Cname ,
COUNT(*) [学生人数]
FROM Course ,
SC
WHERE Course.C# &#61; SC.C#
GROUP BY Course.C# ,
Course.Cname
ORDER BY Course.C# ,
Course.Cname
--40.1 当最高分只有一个时
SELECT TOP 1
Student.* ,
Course.Cname ,
SC.C# ,
SC.score
FROM Student ,
SC ,
Course ,
Teacher
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; Course.C#
AND Course.T# &#61; Teacher.T#
AND Teacher.Tname &#61; N&#39;张三&#39;
ORDER BY SC.score DESC
--40.2 当最高分出现多个时
SELECT Student.* ,
Course.Cname ,
SC.C# ,
SC.score
FROM Student ,
SC ,
Course ,
Teacher
WHERE Student.S# &#61; SC.S#
AND SC.C# &#61; Course.C#
AND Course.T# &#61; Teacher.T#
AND Teacher.Tname &#61; N&#39;张三&#39;
AND SC.score &#61; ( SELECT MAX(SC.score)
FROM SC ,
Course ,
Teacher
WHERE SC.C# &#61; Course.C#
AND Course.T# &#61; Teacher.T#
AND Teacher.Tname &#61; N&#39;张三&#39;
)
--方法1
SELECT m.*
FROM SC m ,
( SELECT C# ,
score
FROM SC
GROUP BY C# ,
score
HAVING COUNT(1) > 1
) n
WHERE m.C# &#61; n.C#
AND m.score &#61; n.score
ORDER BY m.C# ,
m.score ,
m.S#
--方法2
SELECT m.*
FROM SC m
WHERE EXISTS ( SELECT 1
FROM ( SELECT C# ,
score
FROM SC
GROUP BY C# ,
score
HAVING COUNT(1) > 1
) n
WHERE m.C# &#61; n.C#
AND m.score &#61; n.score )
ORDER BY m.C# ,
m.score ,
m.S#
SELECT t.*
FROM sc t
WHERE score IN ( SELECT TOP 2
score
FROM sc
WHERE C# &#61; T.C#
ORDER BY score DESC )
ORDER BY t.C# ,
t.score DESC
SELECT Course.C# ,
Course.Cname ,
COUNT(*) [学生人数]
FROM Course ,
SC
WHERE Course.C# &#61; SC.C#
GROUP BY Course.C# ,
Course.Cname
HAVING COUNT(*) >&#61; 5
ORDER BY [学生人数] DESC ,
Course.C#
SELECT student.S# ,
student.Sname
FROM student ,
SC
WHERE student.S# &#61; SC.S#
GROUP BY student.S# ,
student.Sname
HAVING COUNT(1) >&#61; 2
ORDER BY student.S#
--方法1 根据数量来完成
SELECT student.*
FROM student
WHERE S# IN ( SELECT S#
FROM sc
GROUP BY S#
HAVING COUNT(1) &#61; ( SELECT COUNT(1)
FROM course
) )
--方法2 使用双重否定来完成
SELECT t.*
FROM student t
WHERE t.S# NOT IN ( SELECT DISTINCT
m.S#
FROM ( SELECT S# ,
C#
FROM student ,
course
) m
WHERE NOT EXISTS ( SELECT 1
FROM sc n
WHERE n.S# &#61; m.S#
AND n.C# &#61; m.C# ) )
--方法3 使用双重否定来完成
SELECT t.*
FROM student t
WHERE NOT EXISTS ( SELECT 1
FROM ( SELECT DISTINCT
m.S#
FROM ( SELECT S# ,
C#
FROM student ,
course
) m
WHERE NOT EXISTS ( SELECT 1
FROM sc n
WHERE n.S# &#61; m.S#
AND n.C# &#61; m.C# )
) k
WHERE k.S# &#61; t.S# )
--46.1 只按照年份来算
SELECT * ,
DATEDIFF(yy, sage, GETDATE()) [年龄]
FROM student
--46.2 按照出生日期来算&#xff0c;当前月日 <出生年月的月日则&#xff0c;年龄减一
SELECT * ,
CASE WHEN RIGHT(CONVERT(VARCHAR(10), GETDATE(), 120), 5)
THEN DATEDIFF(yy, sage, GETDATE()) - 1
ELSE DATEDIFF(yy, sage, GETDATE())
END [年龄]
FROM student
SELECT *
FROM student
WHERE DATEDIFF(week,
DATENAME(yy, GETDATE()) &#43; RIGHT(CONVERT(VARCHAR(10), sage, 120),
6), GETDATE()) &#61; 0
SELECT *
FROM student
WHERE DATEDIFF(week,
DATENAME(yy, GETDATE()) &#43; RIGHT(CONVERT(VARCHAR(10), sage, 120),
6), GETDATE()) &#61; -1
SELECT *
FROM student
WHERE DATEDIFF(mm,
DATENAME(yy, GETDATE()) &#43; RIGHT(CONVERT(VARCHAR(10), sage, 120),
6), GETDATE()) &#61; 0
SELECT *
FROM student
WHERE DATEDIFF(mm,
DATENAME(yy, GETDATE()) &#43; RIGHT(CONVERT(VARCHAR(10), sage, 120),
6), GETDATE()) &#61; -1