--------增--------------------
1、往学生表中插入一条信息
insert into stu(sid,cid,sc)
values(1,1,100)
2、往成绩表中批量插入多条信息
insert into score(sid,cid,sc)
select 1,1,100 union
select 1,2,90 union
select 1,3,88 union
select 2,1,80 union
select 2,2,75 union
select 2,3,73 union
select 3,1,70 union
select 3,2,50 union
select 3,3,40
3、把20岁以下的学生姓名、性别、年龄,提取后存放到另外一个已经存在的表中
insert into boy(name,sex,age) select name,sex,age from stu where age<20
4、先创建表xs,然后往xs表中插入100条记录(用循环)
Create table xs(
学号 int,
姓名 char(20)
)
Declare @i int
Set @i=1
While(@i<=20)
begin
insert into xs values (@i,'张'+convert(char(2),@i))
Set @i=@i+1
END
--------删--------------------
把sc小于60的学生删除
delete from scorewhere sc<90
--------改--------------------
1、把张三的学龄改成20
update stu set stuage=20 where name='张三'
2、把所有姓张的改成姓王的
update stu set name='王'+right(name,len(name)-1) where name like '张%'
--------查--------------------
/*
1.where条件中不能出现聚合函数,如果条件中出现聚合函数不能用where,只能用having
2.如果用having,必须先分组
3.在分组情况下,被查询字段只能是分组字段,聚合函数
*/
1、求平均分大于70分的学生信息
select sid,AVG(sc) from score group by sid having avg(sc)>70
2、求60分以上每个人的平均分
select AVG(sc) from score where sc>60 group by sid
先where后group by
3、找出及格(60以上)的学生中平均分大于70的学生信息
select sid,AVG(sc) from score where sc>60 group by sid
having AVG(sc)>70
4、统计女生人数(统计符合条件的记录数量)
select COUNT(*) from stu where sex='女'
5、统计学生总年龄
select SUM(age) from stu
6、求最高分
select MAX(sc) from score
7、求最低分
select MIN(sc) from score
8、求平均分
select AVG(sc) from score
--------表连接、子查询--------------------
9、查询60分以上的学生姓名
select name from stu
inner join score on stu.sid=score.sid
where sc>60
9、查询60分以上的学生姓名
select distinct name from stu
inner join score on stu.sid=score.sid
where sc>60
10、查询60分以上的学生姓名,成绩
select name,sc from stu
inner join score on stu.sid=score.sid
where sc>60
11、查询平均分高于70分的学生姓名
select name from stu
inner join score on stu.sid=score.sid
group by score.sid,name having AVG(sc)>70
12、查询平均分高于70分的学生姓名,平均分
select name,AVG(sc) from stu
inner join score on stu.sid=score.sid
group by score.sid,name having AVG(sc)>70
13、全外连接
select * from t1 full join t2 on t1.id=t2.id
14、交叉连接
select * from t1 cross join t2
alter database SS set compatibility_level=80
15、规则左外连接
select * from t1 left join t2 on t1.id=t2.id
16、不规则左外连接
select * from t1 ,t2 where t1.id*=t2.id
17、规则右外连接
select * from t1 right join t2 on t1.id=t2.id
18、不规则右外连接
select * from t1 ,t2 where t1.id =* t2.id
19、比李市民年龄大的学生信息
select * from stu where age>(张三的年龄)
方法1:
declare @ag int
select @ag=age from stu where name='李市民'
select * from stu where age>@ag
方法2
select * from stu
where age>(select age from stu where name='李市民')
20、至少一门课程及格(60以上)的学生姓名(用表连接实现)
select name from stu
inner join score on stu.sid=score.sid
where sc>60
21、至少一门课程及格(60以上)的学生姓名(用子查询实现)
select name from stu where sid in
(select sid from score where sc>60 )
22、没有参加考试的学生姓名(用表连接实现)
select name from stu
left join score on stu.sid=score.sid
where sc is null
23、没有参加考试的学生姓名(用子查询实现)
select name from stu where sid not in
(select sid from score )
24、没有参加1号课程考试的学生
select name from stu where sid not in
(select sid from score where cid=1)
25、查询平均分大于70的学生姓名(表连接实现)
select name from stu
inner join score on stu.sid=score.sid
group by score.sid,name having AVG(sc)>70
26、查询平均分大于70的学生姓名(子查询实现)
select name from stu where sid in
(select sid from score group by sid having AVG(sc)>70)
27、内连接
select * from t1
inner join t2 on t1.id=t2.id
28、左边外连接
select * from t1
left join t2 on t1.id=t2.id
29、右边外连接
select * from t1 right join t2
on t1.id=t2.id