--单表查询之选择查询(2020.3.20)
--查询选修了课程的学生人数
--select count(distinct sno)
--from sc
--where 语句中不能出现 聚集函数
--查询选修了2号课程的学生的平均成绩
--select avg(grade)/*sum 总成绩 max 最大成绩*/
--from sc
--where cno = '2'
--select avg(grade),getdate()/*显示当前时间*/
--from sc
--where cno = '2'
--对查询结果分组 group by
--查询各个课程号及其相应的选课人数
--select cno,count(sno)
--from sc
--group by cno /*cno为分组属性;select语句中只能出现 分组属性 和 聚集函数*/
--查询各个系有多少个学生
--select sdept,count(*)
--from student
--group by sdept
--查询选修了3门以上的学生的学号
--select sno,count(*)
--from sc
--group by sno having count(*)>=3/*先按学生学号分组,然后进行筛选*/
/*having在分组后使用,*/
/*where作用在整个组*/
--多表连接查询
--查询每个同学及其选修课程的情况
--select student.*,sc.*/*这样会导致出现两列Sno*/
--from student,sc
--where student.sno=sc.sno --连接条件
--
--select student.*,cno,grade/*但是sno必须指定属于谁,因为它在两个表中都有*/
--from student,sc
--where student.sno=sc.sno --连接条件
--求每一门课程的间接选修课
--select first.cno 课程号,second.cpno 间接先修课
--from Course first,course second/*给表加别名*/
--where first.cpno = second.cno
--查询每名同学的选课情况,包括没有选修课程的同学
--select student.*,cno,grade
--from student left outer join sc
--on student.sno = sc.Sno
--查询每名同学的选课情况,包括没有选修课程的同学
--select student.*,cno,grade
--from student inner join sc/**/
--on student.sno = sc.sno
--上下两种查询等价
--select student.*,cno,grade
--from student,sc
--where student.sno=sc.sno
--查询选修2号课程且成绩在90分以上的学生的学号和姓名
--select sc.sno,sname
--from student,sc
--where student.sno = sc.sno and sc.cno = '2' and grade >60
--查询每个学生的学号姓名以及选修课程的课程名及其成绩
--select student.sno,sname,cname,grade
--from student,sc,course
--where student.sno = sc.sno and course.cno = sc.cno/*连接三个表*/
2020.3.20 单表查询之选择查询
猜你喜欢
转载自blog.csdn.net/u013140841/article/details/105380363
今日推荐
周排行