上机练习:
use CJGL
--1.查询student表中年出生的女同学全部信息。
Select * from student where year (birthday) = 1986 and sex = '女'
--2.查询student表中总学分(credithour)在至的同学全部信息。
Select * from student where credithour between 50 and 51
--3. 查询student表中姓“刘”同学的全部信息
Select * from student where studentName like '刘%'
--4. 查询student表中姓‘刘’同学且名为单字的全部信息。
Select * from student where studentname like '刘_'
--6. 查询student表中年龄最大的三位同学信息。
Select top 3 * from student order by YEAR (GETDATE()) - YEAR (birthday) desc
--7. 统计student表中的总人数。
Select count ( * ) as 总人数 from student
--8.统计student表中男女生人数。
Select sex , count ( * ) as 人数 from student where sex is not null group by sex
--9.统计student表中各专业学生人数。select speciality ,COUNT(*)as 人数from student group by speciality
--10.查询年龄为岁的学生的信息。
select * from student where YEAR (GETDATE()) - YEAR (birthday) = 20
--11.统计student表中专业的个数。
select count ( distinct (speciality)) as 专业个数 from student
--12.统计grade表中参考考试的总人数。
select distinct ( COUNT (studentID)) as 人数 from grade
--13. 统计grade表中参考考试的总人次。
select COUNT (studentID) as 人次 from grade
--14.计算grade表中“”课程的平均成绩。
select courseID , AVG (grade) as 平均成绩 from grade where courseID = '101' group by courseID
--15.计算grade表中“”同学的平均成绩。
select AVG (grade) from grade where studentID = '20040101'
--16.计算grade表中各门课程的平均成绩。
select courseID as 课程号, AVG (grade) from grade group by courseID
--17.计算grade表中每个同学的平均成绩。
select studentID , AVG (grade) from grade group by studentID
--18. .查找grade表中平均成绩高于的同学的学号及平均成绩。
select studentID , AVG (grade) from grade group by studentID having AVG (grade) > = 90
--19. 每个班的平均成绩
select SUBSTRING (studentID, 1 , 6 ) as 班级, avg (grade) as 每个班的平均成绩 from grade group by substring (studentID, 1 , 6 )
---------------------------------------------------------------------------------多表查询---------------------------------------------------------------------------------
use cjgl
--select * from sysobjects where type='U'
--1. 列出所有学生可能的选课情况笛卡尔积连接。
--select * from student cross join course
--2. 查询每个学生选修课程成绩高于90分的情况
----select * from student inner join grade on student.studentID=grade.studentID where grade>90
--3. 分别使用连接谓词和join关键字查询学生的学号、姓名、课程号、课程名及成绩。
--select student.studentID,studentname,course.courseID,coursename,grade from student join grade
--on student.studentID =grade.studentID join course on course.courseID=grade.courseID and grade>90
--4. 查询选修了课程的学生信息。
--select * from student where studentID in(select studentID from grade where courseID in(select courseID from course where courseID='101'))
--5. 查询选修了“英语”课程的学生信息。
--select * from student where studentID in(select studentID from grade where courseID in(select courseID from course where coursename='英语'))
--6.查找学生表中年龄最大的学生信息。
--select top 1 * from student where birthday is not null order by birthday
--7.使用存在子查询查找选修了课程的学生信息。
--select * from student where studentID in(select studentID from grade where courseID in(select courseID from course where courseID='102'))
--8.查询选修了两门以上课程的学生信息。
--select * from student where studentID in (select studentID from grade group by studentID having count(studentID)>=2)
--9.查找student表中年龄最大的学生信息。
--select top 1 * from student where birthday is not null order by birthday
--10.查找student表中比计算机专业所有同学都大的同学的信息
--select * from student where YEAR(GETDATE())-YEAR(birthday)>all (select YEAR(GETDATE())-YEAR(birthday) from student where speciality='计算机')
use CJGL
--1.查询student表中年出生的女同学全部信息。
Select * from student where year (birthday) = 1986 and sex = '女'
--2.查询student表中总学分(credithour)在至的同学全部信息。
Select * from student where credithour between 50 and 51
--3. 查询student表中姓“刘”同学的全部信息
Select * from student where studentName like '刘%'
--4. 查询student表中姓‘刘’同学且名为单字的全部信息。
Select * from student where studentname like '刘_'
--6. 查询student表中年龄最大的三位同学信息。
Select top 3 * from student order by YEAR (GETDATE()) - YEAR (birthday) desc
--7. 统计student表中的总人数。
Select count ( * ) as 总人数 from student
--8.统计student表中男女生人数。
Select sex , count ( * ) as 人数 from student where sex is not null group by sex
--9.统计student表中各专业学生人数。select speciality ,COUNT(*)as 人数from student group by speciality
--10.查询年龄为岁的学生的信息。
select * from student where YEAR (GETDATE()) - YEAR (birthday) = 20
--11.统计student表中专业的个数。
select count ( distinct (speciality)) as 专业个数 from student
--12.统计grade表中参考考试的总人数。
select distinct ( COUNT (studentID)) as 人数 from grade
--13. 统计grade表中参考考试的总人次。
select COUNT (studentID) as 人次 from grade
--14.计算grade表中“”课程的平均成绩。
select courseID , AVG (grade) as 平均成绩 from grade where courseID = '101' group by courseID
--15.计算grade表中“”同学的平均成绩。
select AVG (grade) from grade where studentID = '20040101'
--16.计算grade表中各门课程的平均成绩。
select courseID as 课程号, AVG (grade) from grade group by courseID
--17.计算grade表中每个同学的平均成绩。
select studentID , AVG (grade) from grade group by studentID
--18. .查找grade表中平均成绩高于的同学的学号及平均成绩。
select studentID , AVG (grade) from grade group by studentID having AVG (grade) > = 90
--19. 每个班的平均成绩
select SUBSTRING (studentID, 1 , 6 ) as 班级, avg (grade) as 每个班的平均成绩 from grade group by substring (studentID, 1 , 6 )
---------------------------------------------------------------------------------多表查询---------------------------------------------------------------------------------
use cjgl
--select * from sysobjects where type='U'
--1. 列出所有学生可能的选课情况笛卡尔积连接。
--select * from student cross join course
--2. 查询每个学生选修课程成绩高于90分的情况
----select * from student inner join grade on student.studentID=grade.studentID where grade>90
--3. 分别使用连接谓词和join关键字查询学生的学号、姓名、课程号、课程名及成绩。
--select student.studentID,studentname,course.courseID,coursename,grade from student join grade
--on student.studentID =grade.studentID join course on course.courseID=grade.courseID and grade>90
--4. 查询选修了课程的学生信息。
--select * from student where studentID in(select studentID from grade where courseID in(select courseID from course where courseID='101'))
--5. 查询选修了“英语”课程的学生信息。
--select * from student where studentID in(select studentID from grade where courseID in(select courseID from course where coursename='英语'))
--6.查找学生表中年龄最大的学生信息。
--select top 1 * from student where birthday is not null order by birthday
--7.使用存在子查询查找选修了课程的学生信息。
--select * from student where studentID in(select studentID from grade where courseID in(select courseID from course where courseID='102'))
--8.查询选修了两门以上课程的学生信息。
--select * from student where studentID in (select studentID from grade group by studentID having count(studentID)>=2)
--9.查找student表中年龄最大的学生信息。
--select top 1 * from student where birthday is not null order by birthday
--10.查找student表中比计算机专业所有同学都大的同学的信息
--select * from student where YEAR(GETDATE())-YEAR(birthday)>all (select YEAR(GETDATE())-YEAR(birthday) from student where speciality='计算机')