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
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
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
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
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
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
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
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
(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
(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
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
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
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) --相关子查询