Sql Server 学习1

create database tempCZBK
go
use tempCZBK
go
create table student_info
(
    学号 varchar(30) primary key,
    姓名 varchar(30) null,
    性别 varchar(30) null,
    出生年月 date,
    家庭住址 varchar(300),
    备注 varchar(500)
)
go
create table curriculum
(
    课程编号 varchar(30) primary key,
    课程名称 varchar(30)null,
    学分 int
)
go
create table grade
(
    学号 varchar(30),
    课程编号 varchar(30),
    分数 int
)

insert into student_info values('0001','张三','',GETDATE(),'北京','')
insert into student_info values('0002','李斯','',GETDATE(),'上海','')
insert into student_info values('0003','陈晨','',GETDATE(),'郑州','')
insert into curriculum values('01','C语言',2)
insert into curriculum values('02','C#语言',3)
insert into curriculum values('03','Java',1)
insert into grade values('0001','01',90)
insert into grade values('0001','02',70)
insert into grade values('0001','03',50)
insert into grade values('0002','01',95)
insert into grade values('0002','02',71)
insert into grade values('0002','03',52)
insert into grade values('0003','01',94)
insert into grade values('0003','02',70)
insert into grade values('0003','03',54)
--1.    在GRADE表中查找80-90份的学生学号和分数
select 学号,分数 from grade where 分数 between 80 and 90
--2.    在GRADE 表中查找课程编号为03学生的平均分
select AVG(分数) from grade where 课程编号='03'
--3.    在GRADE 表中查询学习各门课程的人数
select 课程编号,count(学号) from grade group by 课程编号
--4.    查询所有姓张的学生的学号和姓名
select 学号,姓名 from student_info where 姓名 like '张%'
--1、    查询和学号’0001’的这位同学性别相同的所有同学的姓名和出生年月
select 姓名,出生年月 from student_info where 性别=(select 性别  sex from student_info where 学号='0001') 
 select 姓名,出生年月 from student_info where 性别 in(select 性别 from student_info where 学号='0001')
--2、    查询所有选修课程编号为0002 和0003的学生的学号、姓名和性别

select 学号,姓名,性别 from student_info where 学号 in(select 学号 xuehao from grade where 课程编号='03' or 课程编号='02')
select 学号,姓名,性别 from student_info where 学号 in(select 学号 from grade where 课程编号='02' and 学号 in(select 学号 from grade where 课程编号='01'))
--3、    查询出学号为0001的学生的分数比0002号学生最低分高的课程编号的课程编号和分数
select  课程编号,分数 from grade where 学号='0001' and 分数 >(select min(分数) t from grade where 学号='0002')
--1、    查询分数在80-90分的学生的学号、姓名、分数
select  student_info.学号,student_info.姓名,grade.分数 from student_info,grade where grade.分数 between 80 and 90;
--2、    查询学习了’C语言’课程的学生学号、姓名和分数
select  student_info.学号,student_info.姓名,grade.分数
 from student_info,grade 
 where student_info.学号=grade.学号 and grade.课程编号=(select curriculum.课程编号 t from curriculum where curriculum.课程名称='C语言')
--3、    查询所有学生的总成绩,要求列出学号、姓名、总成绩,没有选课的学生总成绩为空。
--select  student_info.学号,student_info.姓名,sum(grade.分数) from student_info,grade group by grade.学号
select w.学号,w.姓名,c.t as 总分 from (select student_info.学号,student_info.姓名 from student_info where student_info.学号 in( select grade.学号  from grade group by grade.学号))w,
( select grade.学号,sum(grade.分数) t from grade group by grade.学号)c where w.学号=c.学号

select grade.学号,student_info.姓名,sum(grade.分数) as 总分 from student_info,grade 
where grade.学号=student_info.学号 group by grade.学号,student_info.姓名
原文地址:https://www.cnblogs.com/mxxblog/p/2851746.html