当前位置:Gxlcms > 数据库问题 > sql语句练习

sql语句练习

时间:2021-07-01 10:21:17 帮助过:24人阅读

SELECT student.SNAME from student 
where not EXISTS 
(
      SELECT * from course 
      where NOT EXISTS 
      (
            SELECT * from sc 
            where sc.SNO = student.SNO and sc.CNO = course.CNO
       )
);

理解:exists是每层向下遍历,每层结果放到当层结果集里,not exists 若内层结果集为空则上一层的值放到结果集合。

student { 对于某个人

        course { 不存在一门可能

                sc{ 他没有分数记录

                         sc.SNO = student.SNO &&

                         sc.CNO = course.CNO

                 }

        }

}

https://blog.csdn.net/songxueyu/article/details/9140061

 

1.1、检索至少选修"程军"老师所授全部课程的学生姓名

SELECT student.SNAME from student 
where not EXISTS 
(
      SELECT * from course 
      where course.TEACHER = ‘程军‘ AND NOT EXISTS 
      (
            SELECT * from sc 
            where sc.SNO = student.SNO and sc.CNO = course.CNO
       )
);

 

 

执行顺序:from > join > on > where > group by > avg.sum.count.... .. > having > select > distinct > order by

 

distinct 优先级高于order by 连用时order by 的字段必须在 select 中,否则distinct生成的虚拟表无order by字段。

 

sql语句练习

标签:sum   生成   检索   net   having   exist   遍历   select   style   

人气教程排行