sql server 多表连接

sql server数据库多表连接查询语句,用到的数据库表如下:

1.Find the ID, names of all the instructors from departments whose

name contain character '门'

select ID,name
from instructor left join department on instructor.dept_name=department.dept_name
where department.dept_name like '%门%'   --使用like关键字进行模糊匹配
2. Find the id, names and credits of courses which have 3 credits opened by '内功学院' department or by '拳脚学院' department
(select course_id,title,credits
from course
where credits=3 and dept_name='内功学院')
union  --使用union关键字进行集合的并运算,其中可以包含重复元素
(select course_id,title,credits
from course
where credits=3 and dept_name='拳脚学院')
3. For the student with ID 12345 (or any other value), show all
course_id and title of all courses registered for by the student.
select distinct takes.ID,course.course_id,course.title   --使用distinct关键字消除重复
from takes left join course on takes.course_id=course.course_id
where takes.ID=12345
4.As above, but show the total number of credits for such courses
(taken by that student). Don't display the tot_creds value from the
student table, you should use SQL aggregation on courses taken by the
student.
select sum(credits) as cre_sum    
from course join takes 
on course.course_id=takes.course_id    
where takes.id =12345 
5. As above, but display the total credits for each of the students, along
with the ID of the student; don't bother about the name of the student.
(Don't bother about students who have not registered for any course,
they can be omitted)
select student.ID,sum(course.credits) as cre_sum
from student join takes 
on student.ID=takes.ID join course 
on course.course_id=takes.course_id 
group by student.ID
6. Find the names of all students who have taken any 内功学院 course
ever (there should be no duplicate names)
select distinct name
from  takes join  student 
on student.ID = takes.ID
join course 
on takes.course_id = course.course_id
where course.dept_name='内功学院'
7. Display the IDs and names of all instructors who have never taught a
course (Note: interpret "taught" as "taught or is scheduled to teach")
select ID,name
from instructor
where ID not in (select ID
		from teaches)
8.Find the id and names of the students who have registered for some
courses without evaluated grades.
select student.ID,name
from student join takes
on student.ID=takes.ID
where takes.grade is null
9.Find the courses which are the Prerequisites of other courses. The
result should involve the ids and titles of the prerequisites and the ids
and titles of the Subsequence courses (note: the names of columns in
result should show the roles of the courses clearly)
select subCourse.course_id,subCourse.title,preCourse.course_id,preCourse.title
from prereq join course as subCourse
on prereq.course_id=subCourse.course_id
join course as preCourse
on prereq.prereq_id=preCourse.course_id
10. Find the sections which have the minimum enrollment among sections registered by
students. For each section as such, information displayed should involve:
(1) Identifier of section(i.e. the primary key for section)
(2) Course name corresponding to the section
(3) Enrollment of the section('enrollment' is the alias for the number of students who
registered for the section)

(4) TOP keyword in SQL Server is denied.

with secStu(sCount,title,cour_id,sec_id,semester,year) as
(select COUNT(takes.ID) sCount,course.title,section.course_id,section.sec_id,section.semester,section.year
from takes 
full join section
on takes.course_id=section.course_id
and takes.sec_id=section.sec_id
and takes.semester=section.semester
and takes.year=section.year
left join course
on section.course_id=course.course_id
group by section.course_id,section.sec_id,section.semester,section.year,course.title)--虚表,将takes与section连接,并计算选课总人数
select *
from secStu
where sCount <= all (select sCount 
		from secStu)--找出选课人数最少的课程
11. USE aggregation on outer join to construct the following query
For all students, list the registration information of the students. The students who have
never registered for any courses should also be considered. In the case, the aggregative
information of such students should be set to 0. For each student, information displayed
should involve:
(1) Identifier of student(i.e. the primary key for student)
(2) Name of the student
(3) Number of course registrations (Caution: Not the number of section registrations. E.g. ,
student A registered the course B twice in 2 sections, the number of course
registrations is 1 and the number of section registrations is 2).
(4) Number of section registrations

(5) TOP keyword in SQL Server is denied.

with stuSec (ID,name,course_id,sec_id,semester,year) as 
(select student.ID,name,takes.course_id,takes.sec_id,takes.semester,takes.year
from student 
left join takes
on student.ID=takes.ID)  --with子句形成的虚表,将student与takes表连接
select ID,name,count(distinct course_id) courseCount,count(course_id) sectionCount --课程数去重,section数不去重,课程数与section数不同主要在于部分同学重修了该门课程
from stuSec
group by ID,name

12. USE scalar subquery to construct the following query
Find the information for the instructors who have taught more than 1 course (that is,
he/she should have taught 2 distinct courses as least). For each instructor as such,
information displayed should involve:
(1) Identifier of instructor(i.e. the primary key for instructor)
(2) Name of the instructor
(3) Average salary of the department for which the instructor works
(4) Sum of credit points taught by the instructor(for example, if instructor A has taught
course A (2 credit points) twice, course B(3 credit points) once, then the sum of credit

points taught by instructor A is 7)

with depSal(dept_name,sal) as 
(select dept_name,avg(salary)
 from instructor
 group by dept_name)   --with子句构成的虚表计算了所有部门的平均工资
select instructor.ID,instructor.name,sal,sum(course.credits) sumCredits
from instructor
left join teaches
on instructor.ID=teaches.ID
left join depSal
on instructor.dept_name=depSal.dept_name
left join course
on teaches.course_id=course.course_id
group by instructor.ID,instructor.name,depSal.sal  --将表按照老师姓名分组
having count(teaches.course_id) >= 2      --老师所教课程大于等于2门
13. Find students who have registered for some but not all courses taught by instructors of
department '拳脚学院'. Do this using the "not exists ... except ..." structure. For each  student as such, information displayed should involve:
(1) Identifier of student(i.e. the primary key for student)
(2) Name of the student
(3) Number of courses, taught by instructors of department '拳脚学院', registered by the student
with T(student_name,total_course)as--修过拳脚学院课的学生
	(select student.name,COUNT(course.course_id)
	from student join takes on student.ID=takes.ID
		join course on takes.course_id=course.course_id 
	where course.dept_name='拳脚学院'
	group by student.name)
select ID,name,total_course
from student join T on student.name=T.student_name 
where not exists(
			(select student_name --修过全部拳脚学院课的学生
			from T 
			where T.total_course=( select COUNT(course_id)
						from course 
						where course.dept_name='拳脚学院')  )
			except              --修过拳脚学院课的学生
			(select student_name 
			from T)   )         --用到了not exists ...except...语句,来进行非空检验
14. As query requirement in Q4, Use matching of counts to fulfill the requirement. (don't
forget the distinct clause!).
with T(student_name,total_course)as --修过拳脚学院课的学生
	(select student.name,COUNT(course.course_id)
	from student join takes on student.ID=takes.ID
		join course on takes.course_id=course.course_id 
	where course.dept_name='拳脚学院'
	group by student.name)
select ID,name,total_course
from student join T on student.name=T.student_name 
where ( select COUNT(course_id)       --拳脚学院开设的总的课程数,其返回值只有一个
	from course 
	where course.dept_name='拳脚学院')
	>
	(select total_course          --学生修拳脚学院课程的数目
	 from T t
	where t.student_name=T.student_name)  --相关子查询







猜你喜欢

转载自blog.csdn.net/weixin_40304882/article/details/80508372