13. 查询Studentinfo表中前5行学生信息。
select top 5 * from StudentInfo
14. 查询课程表Courseinfo表中的前40%的数据。
Select top 40 percent * From courseinfo
15. 查询课程信息表”CourseInfo中课程类型为必修,并且学分在2和5之间(包含2和5)
的课程编码、课程名称、学分。
select Course_Code,Course_Name,Course_Credit from CourseInfo
where course_type=1 and Course_Credit<=5 and Course_Credit>=2 或 Course_Credit between 2 and 5
16. 查询“学生信息表”StudentInfo中入学时间在2013年9月1日到2014年1月1日(包
含两个端点)的学生的学号、姓名、性别。
select Student_Code,Student_Name,Student_sex from StudentInfo
where Student_indate between '20130901' and '20140101'
17. 查询学生表StudentInfo中出生日期在1994年出生的女学生的姓名和手机号码 。
select Student_Name,Student_Mobile from StudentInfo
where Student_Sex='0' and Student_BirthDay between '19940101' and '19941231'
(或where Student_Sex='0' and year(Student_BirthDay)=1994)
18. 查询教师表teacherinfo中在2005年入职的教师的基本信息 。
select *
from teacherInfo
where Teacher_Hiredate between '20050101' and '20051231'
19. 查询“学生信息表”StudentInfo中姓“王”同学的学号和姓名。
select Student_Code,Student_Name from StudentInfo
where Student_Name like '王%'
20. 查询“学生信息表”StudentInfo中姓“李”并且名字中带有“明”字在2013年入学的
同学的学号和姓名、手机号码。
select Student_Code,Student_Name, Student_Mobile from StudentInfo
where Student_Name like '李%明%' and Student_indate between '20130101 'and '20131201'
21. 查询“课程信息表”CourseInfo以“中”或者“国”开头的课程编码、课程名称。
select Course_Code,Course_Name from CourseInfo
where Course_Name like '[中国]%'
22. 查找“学生信息表”StudentInfo中学号为单数的学生信息 。
select *
from StudentInfo
where Student_Code like '%[13579]'
23. 查找“学生信息表”StudentInfo中学号的最后一位不是0到7的学生信息 。
select *
from StudentInfo
where Student_Code like '%[^0-7]'
24. 查询“学生信息表”StudentInfo中学号最后两位是21的学生信息。
select *
from StudentInfo
where Student_Code like '!'
25. 查询学生表StudentInfo中未留手机号码的学生情况,并以学号进行排序。
select *
from StudentInfo
where Student_Mobile is null order by student_code
26. 查询”课程信息表”courseinfo中没有人数限制的课程信息,以课程名称进行升序排列。
select *
from CourseInfo
where Course_limit is null order by Course_Name asc
27. 查询“学生信息表”StudentInfo中学号中有7且最后一位是3或4的学生,并按照学号
进行降序排序。 select *
from StudentInfo
where Student_Code like '%7%[34]' order by Student_Code desc
28. 查询“学生信息表”StudentInfo中学生的信息,根据入学时间排升序,学号排降序,显
示前100个学生的基本信息。 Select top 100 * from StudentInfo
order by student_indate asc ,student_name desc
29. 查询学生信息表StudentInfo中学号在130016201到130016220之间(包括第一个和最
后一个)学生所有基本信息,并按照姓名升序排序。 select *
from StudentInfo
where Student_Code between '130016201' and '130016220' order by Student_Name asc
30. 把课程表CourseInfo中课程类型排升序,学分排降序,但是要排除学分大于5的课程。
select *
from CourseInfo
where Course_Credit<=5
order by Course_Type asc,Course_Credit desc
子查询
1. 查询“课程信息表”CourseInfo中课程编号为Z10001、Z10007、Z10010、Z10025的课
程名称、学分。(易)
select Course_Name,Course_Credit From courseInfo
where Course_Code in('Z10001','Z10007','Z10010','Z10025')
2. 查找与陈欣老师在同一个系的所有教师的基本信息。(易)
Select * from teacherinfo
Where dept_id in (select dept_id from teacherinfo where Teacher_Name='陈欣')
3. 将学生信息表studentinfo中1400111班学生的学号和姓名数据复制到新表newStudent
中. (易)
select Student_Code,Student_Name into newStudent from StudentInfo
where class_id in(select class_id from classinfo where class_code='1400111')
4. 将所有学生的“计算机基础”成绩置为70 。(易)
Update student_course Set course_grade=70
Where course_id in(select course_id from courseinfo where course_name='计算机基础')
5. 删除李波的 “程序设计基础”课程成绩记录。(中)
Delete from student_course
Where course_id in(select course_id from courseinfo where
course_name='程序设计基础')
and student_id in (select student_id from studentinfo where student_name='李波')
6. 查询比1300162班的所有学生年龄都小的学生的学号、姓名和出生日期。(中)
select Student_Code,Student_Name,Student_BirthDay from StudentInfo where Student_BirthDay>all
(select Student_BirthDay from StudentInfo
where Class_ID=(select Class_ID from ClassInfo where Class_Code='1300162') )
7. 查询比李波所在班级学生年龄都大的学生信息。(中)
select * from StudentInfo where Student_BirthDay
select Student_BirthDay from StudentInfo where Class_ID=( select Class_ID from StudentInfo where student_name='李波') )
8. 查询课程表中比“SQL Server数据库应用”的学分都大的课程信息。(易)
select * from CourseInfo where Course_Credit>all(
select Course_Credit from CourseInfo where Course_Name='SQL Server数据库应用')
9. 查找不属于“软件工程系”所有女同学的学号、姓名、入属于学时间。(较难)
Select Student_Code,Student_Name,Student_Indate From StudentInfo
Where Student_Sex=0 And Class_ID IN (
Select Class_ID From ClassInfo Where Dept_ID not IN
(
Select Dept_ID From DeptInfo Where Dept_Name ='软件工程系' ) )
10. 查找课程号Z10003的成绩不低于课程号Z10001的最低成绩的学生的学号和姓名。(难)
select student_name,student_code from StudentInfo where student_ID in (
select student_ID from Student_Course
where Course_ID in (select Course_ID from CourseInfo where Course_Code='Z10003') and course_grade >any (select course_grade from Student_Course where Course_ID= (select Course_ID from CourseInfo where Course_Code='Z10001')) ) 或者
select student_name,student_code from StudentInfo where student_ID in (
select student_ID from Student_Course
where Course_ID in (select Course_ID from CourseInfo where Course_Code='Z10003')
and course_grade >(select min(course_grade) from Student_Course where Course_ID=(select Course_ID from CourseInfo where Course_Code='Z10001')) )
多表查询
多表查询
1. 查询考试成绩在80分以上的学生的姓名. (易)
select student_name
from studentinfo,student_course where
studentinfo.Student_ID=Student_Course.Student_ID
and
Course_Grade>=80
2. 查询选修了“程序设计基础”的学生的ID号. (易)
select student_id
from Student_Course,CourseInfo
where CourseInfo.Course_ID=Student_Course.Course_ID and Course_Name='程序设计基础' and CourseInfo.course_type=0
3. 查找“软件工程系”所开课程的课程编码、课程名称、学分。(易)
select course_code,course_name,course_credit from CourseInfo,DeptInfo
where DeptInfo.Dept_ID=CourseInfo.Dept_ID and Dept_Name='软件工程系'
4. 查找班级编号为1400201的这学生的学号、姓名、性别。(易)
select student_code,student_name,student_sex from StudentInfo,ClassInfo
where StudentInfo.Class_ID=ClassInfo.Class_ID and class_Code='1400201'
5. 查找开设了课程号Z10003的系部信息。(易)
select deptInfo.*
from CourseInfo,deptInfo
where CourseInfo.Dept_ID=DeptInfo.Dept_ID and Course_Code='Z10003'
6. 查询每个教师的信息,包括系部编码、系部名称、教师工号、教师姓名。(易)
select dept_code,dept_name,teacher_code,teacher_name from DeptInfo,teacherInfo
where DeptInfo.Dept_ID=teacherInfo.Dept_ID
7. 查询单科学分大于3课程的课程信息,包括系部名称、课程编码、课程名称。(易)
select dept_name,course_code,course_name from courseinfo,deptinfo
where courseinfo.dept_id=deptinfo.dept_id and course_credit>3
8. 查询“软件工程系”手机号码采用133号段的教师信息,信息包括系部名称、教师工号、
教师名称、手机号码。(易)
select dept_name,teacher_code,teacher_name,teacher_mobile from DeptInfo,teacherInfo
where teacherInfo.dept_id=deptinfo.dept_id and Dept_Name='软件工程系' and left(teacher_mobile,3) = '133'
9. 查询所有的课程情况及其被选修信息,如果有课程未被选修,也需要包含该课程的信息。
(易)
select * from CourseInfo left join Student_Course on CourseInfo.Course_ID=Student_Course.Course_ID
10. 查询所有教师情况及教师课表情况,如果有教师未上课,也需要包含该教师的信息。(易)
select * from teacherInfo left join Teacher_Class_Course on teacherInfo.Teacher_ID=Teacher_Class_Course.Teacher_Id