sql-经典例子练习

 

/************************************************************************************************************

* 数据见50条常用sql.sql脚本

* 1.学生表

* student(studentID,stu_name,stu_age,stu_sex) --studentID 学生编号,stu_name 学生姓名,stu_age 出生年月,stu_sex 学生性别

* 2.课程表 

* course(courseID,crs_name,teacherID) --courseID --课程编号,crs_name 课程名称,teacherID 教师编号

* 3.教师表 

* teacher(teacherID,tea_name) --teacherID 教师编号,tea_name 教师姓名

* 4.成绩表 

* stu_crs(studentID,courseID,score) --studentID 学生编号,courseID 课程编号,score 分数

        "01"课程、"02"课程是变量(记录)。不是常量(字段)。

        如果把"01"、"02"设计成字段,就会使难度降低,但是不够灵活。比如以后会再加一科目04物理。

 

* ########方法:先确定哪些表的字段,再一一满足条件,即可。

************************************************************************************************************/

表达式:where isnull(b.score,0)是简单的动态,还有动态sql。

连接顺序:选择那个表作为源数据表,那个表优先被连接

3、group by子句将数据划分为多个分组; 

5、使用having子句筛选分组; 

7、使用order by对结果集进行排序。

 

group by 、左外连接和where并用。

--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数        

        #####同一个表同一个字段值的比较,把一个表变为两个表,才能比较。

        #####正常的是,一个字段值和一个常量比较。

        有两种情况:1.1、1.2

--1.1、同时存在"01"课程和"02"课程的情况(01必选课,02必选课)

        SELECT  a.*,b.score,c.score 

        FROM student a,stu_crs b,stu_crs c

        WHERE b.courseID='01' AND c.courseID='02' AND b.score>c.score AND a.studentID=b.studentID AND  c.studentID=a.studentID

        (a.studentID=b.studentID AND  c.studentID=a.studentID这两个都需要,因为c.studentID和b.studentID与a表关联的结果集不一样)

--1.2、存在"01"课程但可能不存在"02"课程的情况、(01必选课,02选修课)、(不存在时显示为null)(以下存在相同内容时不再解释)

        select * from student a 

        left join stu_crs b on a.studentID=b.studentID and b.courseID='01'

        left join stu_crs c on a.studentID=c.studentID and c.courseID='04'

        and b.score>c.score (and 有8条,where b.score>c.score只有一条)

 

 

--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

        select a.*,avg(b.score) from student a,stu_crs b

        where a.studentID = b.studentID

        group by a.studentID

        having avg(b.score)>70

        order by avg(b.score) desc

 

--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

        --5.1、查询所有有成绩的SQL。

        select a.*,count(b.score),sum(b.score) from student a,stu_crs b

        where a.studentID = b.studentID                                        #######因为“表达式需要b.score”,且b.score可能有空的。resultSet7条。

        group by a.studentID

        --5.2、查询所有(包括有成绩和无成绩)的SQL。

        select a.*,count(b.score),sum(b.score) from student a

        left join stu_crs b on a.studentID = b.studentID                        #######left join,因为b.score可能有空的。resultSet8条。

        group by a.studentID                                                                #######对结果集还要进行计算的,一般需要group by、having

 

--11、查询没有学全所有课程的同学的信息 

select * from student a,stu_crs b,course c

where a.studentID=b.studentID

group by a.studentID

having count(distinct b.courseID)         ######不能是count(distinct b.courseID)==1,因为group by a.studentID过了,

< count(distinct c.courseID)

 

--12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 

select distinct                                         ########distinct过滤重复结果

a.* from student a, stu_crs b where a.studentID = b.studentID and b.courseID 

in (select courseID from stu_crs where studentID = '01') and a.studentID <> '01'

 

--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 

select student.* from student where studentID in

(select distinct stu_crs.studentID from stu_crs where studentID <> '01' 

and stu_crs.courseID in (select distinct courseID from stu_crs where studentID = '01')         ######限定范围和限定数量一块就可以确定完全一样了。

group by stu_crs.studentID having count(1) = (select count(1) from stu_crs where studentID='01')) ###限定数量,count(1) 与count(*)效果一样。

 

--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 

        select a.*,avg(b.score) from student a,stu_crs b 

        where a.studentID=b.studentID and a.studentID 

        in(select studentID from stu_crs where score <60 group by studentID having count(1)>1)

        group by a.studentID

 

 

 

 

 

 

 

--------------------------------未完成----------------------

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值