开始复习下sql,常用SQL语句示例(一)
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 分数
--创建测试数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
--创建测试数据 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)
--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
两种情况:
--1.1、查询同时存在"01"课程和"02"课程的情况
select s.*,a.score as ['01'课程分数],b.score as ['02'课程分数] from Student as s,SC as a,SC as b where s.S#=a.S# and s.S#=b.S# and a.C#=01 and b.C#=02 and a.score>b.score
--1.1 查询结果
--1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)
![](https://pic002.cnblogs.com/images/2012/441289/2012102812311134.jpg)
select s.*,a.score as ['01'课程分数],b.score as ['02'课程分数] from Student as s join SC as a on s.S#=a.S# and a.C#=01 left join SC as b on s.S#=b.S# and b.C#=02 where s.S#=a.S# and a.score> isnull(b.score,0)
![](https://pic002.cnblogs.com/images/2012/441289/2012102812311134.jpg)
--2、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select s.S# as 学号,s.Sname as 姓名,avg(score) as 平均成绩 from Student as s,SC where s.S#=SC.S# group by s.S#,s.Sname having avg(score)>=60
--3、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
select s.S# as 学号,s.Sname as 姓名,isnull(avg(score),0) as 平均成绩 from Student as s left join SC on s.S#=SC.S# group by s.S#,s.Sname having isnull( avg(score),0)<60 order by s.S#
--4、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select s.S#,s.Sname,count(C#) as 选课数,sum(score) as 总分 from Student s left join SC on s.S#=SC.S# group by s.S#,s.Sname order by s.S#
--5、查询"李"姓老师的数量
select count(Tname) as 姓李老师的数量 from Teacher where Tname like '李%'
--6、查询学过"张三"老师授课的同学的信息
select s.* from Student s,SC,Teacher t,Course c where t.T#=c.T# and c.C#=SC.C# and SC.S#=s.S# and Tname='张三'
--7、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
--取a表中的课程id为01,b表中的课程id为02.当两个条件同时满足时,才能选择出正确结果 select s.* from Student s,SC a,SC b where s.S#=a.S# and s.S#=b.S# and a.C#=01 and b.C#=02
--8、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
--查出包含01的id,不包含02的学生学号。取交集 select * from Student s,SC a where s.S#=a.S# and a.C#=01 and s.S# not in (select S# from SC where C#=02 )
--9、查询没有学全所有课程的同学的信息
select s.* from Student s,SC where s.S#=SC.S# group by s.S#,Sname,Sage,Ssex having count(C#)<(select count(C#) from Course)
--10、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select distinct s.* from Student s,SC where s.S#=SC.S# and s.S#!=01 and SC.C# in (select C# from SC where S#=01)
小结:
感觉sql要想比较必须在一行里面。我认为吧,要想查询出来,必须把数据放到一行里面。然后对其中的条件进行查询。这样就可以得到所要的数据。