SQL_Q

 

学生信息xs(xh,xm,xb,nl) 

成绩信息cj(xh,kch,cj) 

课程信息(kch,kcm)

1统计年龄在20到30的男女生的人数,显示性别,人数

2 为下列查询select xb,nl,count(xh)    from  xs  where  nl>=20  or  nl<=30

group  by  xb,nl;选择一个合适的题目

A查询男生人数,女生人数

B 统计各年龄段的男女生人数

C 统计所有年龄的男女生人数

D 统计每个年龄的男女生人数

 

3 查询需要补考的人,显示学号,姓名,课程号

4假定有表CJ(学号xh,姓名xm,课程号kch,成绩cj),解释下列查询

1)select xh,xm,kch  from  cj  group by   xh  having  max(cj)<60

2)select xh,xm,kch  from  cj  group by  xh,xm,kch  having min(cj)<60

3)select xh,xm,kch  from  cj  group by  xh,xm,kch  having max(cj)<60

4)  23是否有区别?

 

 

5 统计平均成绩在80以上的课程,显示课程号,课程名

 

6 找出同名的人,显示学号,姓名,性别,年龄

 

7 此语句select xh,xm,xb,nl from xs group by xh,xm,xb,nl having count(xm)>1

查询什么?

 

8  语句select xh,xm,xb,nl  from xs a,xs b where  a.xm=b.xm错在什么地方?

 

9  统计所有人(没有成绩的也要统计)的总分,显示学号,姓名,总分

select  * from       xs  left  join  cj  on  xs.xh=cj.xh;

select xs.xh,xm,cj  from xs  left  join  cj  on  xs.xh=cj.xh;

select  xs.xh,xm, sum(cj) from xs  left  join  cj  on  xs.xh=cj.xh

group by  xs.xh,xm;

 

 

1   -   n 怎么创建表?

m  -   n 怎么创建表

 

老师   学生两个实体没有任何关系?

假定是老师1 对学生n   ,要在学生的表中定义老师的编号做为外键,有什么意思?

假定是多对多,根据数据库设计范式,要将两个实体的主键提取出来,至少再产生一张表,请问将老师编号和学生编号放在一张表有什么用处?

 

 

 

1统计每个人的总分,显示学号,总分

select sid,sum(score) from sc  group by sid;

2 统计总分最高的人,显示学号

数据库第一范式:字段名称必须规范且不能相同

将查询做为数据源要符合数据库第一范式

select  sid,max(sum(score)) from sc group by sid;

 select  sid,max(sum(score)) from

 (

select sid,sum(score) from sc  group by sid

 );

select sid,sum(score) sid from sc group by sid;

select * from  (  select sid, sum(score) sid  from sc group by sid  );

方法一:

先查出最大总分

select  max(zf)  from  (select  sum(score)  zf  from sc  group by sid );

将每个人的总分和最大总分比较

select sid,sum(score) 总分  from sc  group by sid

having  sum(score)>=(  最大总分  );

方法二:

 将每个人的总分和所有人的总分比较(利用all关键字)

 

select sid,sum(score) 总分  from sc  group by sid

having  sum(score)>=all(select  sid,sum(score)   from sc  group by sid);执行结果( 

select sid,sum(score) 总分  from sc  group by sid

having  sum(score)>=all(select  sum(score)   from sc  group by sid);

 

 

3找出同名的人,显示学号,姓名,性别

select  distinct  a.* from  student  a, student b  

where  a.sname=b.sname  and  a.sid<>b.sid;

为什么要加distinct

学号

姓名

 

学号

姓名

1

张三

1

张三

2

张三

2

张三

3

张三

3

张三

用子查询演示找同名的学生

select   *  from student  where sname in (

select sname,count(*) from student group by sname

having  count(*)>1

);

select  a.* from student a,

(select sname from student group by sname having count(*)>1) b

where a.sname=b.sname;

4 找出选修3门课程以上,且平均分最高的人,显示学号

select sid from sc group by sid having  count(*)>=3

and avg(score)>=all(   select  avg(score) from sc group  by  sid        );

 

5 统计所有人(没有成绩的也要统计)的总分,显示学号,姓名,总分

左或右连接

 

不准使用左或右连接

select a.sid 学号,sname 姓名 ,sum(score) 总分 from student a,sc where a.sid=sc.sid group by a.sid,sname

union

select sid,sname ,0    from  student  where  sid  not  in ( 

 select   distinct  sid  from  sc

);

 

6 统计学过sc曹杨老师teacher课程course的人,显示学号,姓名student

select a.sid,sname from student a,sc,course b ,teacher c

 where a.sid=sc.sid  and sc.cid=b.cid and b.tid=c.tid and c.tname=’aaa’;

 

select a.sid,sname from student a ,sc  where a.sid=sc.sid 

and  cid  in  (

    select  b.cid  from  course b ,teacher  c  where  b.tid=c.tid  and  c.tname=’sss’     ) ; 

 

7  统计学过1001老师course.tid两门课程sc.cid  course.cid以上的人,显示学号sc.sid

select  sid,tid,sc.cid  from  sc, course   where  sc.cid=course.cid  and  tid=1001 ;

select   sid  from  sc  ,course  where sc.cid=course.cid  and  tid=1001

group by  sid   having count(*)>=2;

 

8 统计学过1001号老师所有课程的人,显示学号

提示:学生选1001老师的课的数量=1001老师带课的数量

select   sid  from  sc  ,course  where sc.cid=course.cid  and  tid=1001

group by  sid   having  count(*)=(  

   select   count(*)  from sc,course where sc.cid=course.cid and tid=1001  );

上述语句问题(                                     )

 

学号

课程号

成绩

 

课程号

课程名

老师号

1

1

 

1

 

1001

1

2

 

2

 

1001

2

1

 

3

 

1002

2

2

 

 

4

 

1002

 

 

 

 

5

 

1001

select   sid  from  sc  ,course  where sc.cid=course.cid  and  tid=1001

group by  sid   having  count(*)=(

   select   count(*)  from  course  where  tid=1001  );

 

9 统计学过1001号老师所有课程的人,显示学号,姓名

 

 

 

 

10  统计学过曹杨老师所有课程的人,显示学号,姓名

 

先统计每个学生选的每个曹杨老师的选课数量

再统计每个曹杨老师带课数量

然后这个两个数据源进行比对连接

 

只要符合某个曹杨,比如

学1    曹1   曹2 

学2    曹1

学3    曹2

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值