第6章 数据查询

第6章
#【例6.1】查询student表中所有学生的学号、姓名和专业。

use stusys;
desc student;
select sno,sname,speciality from student;
#【例6.2】查询student表中所有列。

 

use stusys;
select * from student;
#【例6.3】查询student表中所有学生的学生的sno、sname、speciality,并将结果中各列的标题分别修改为学号, 姓名, 专业。

 

use stusys;
select sno as 学号,sname as 姓名, speciality as 专业 from student;
#【例6.4】设student1表的表结构和样本数据与student表相同,且已创建和插入数据;在student1表中,列出学号、学分和增加4分后的学分。

 

use stusys;
create table student1 like student;
desc student1;
select * from student1;
insert into student1
    select * from student;
select * from student1;

use stusys;
select sno as 学号 , tc as 学分, tc+4 as 增加4分后的学分 from student1;
select * from student1;
use stusys;
set sql_safe_updates=0;
update student1
    set tc=tc+4;
    select * from student1;

#【例6.5】查询student表中speciality列,消除结果中的重复行。

select distinct speciality as 专业 from student;
#【例6.6】查询student表中专业为计算机或性别为女的学生。

 

use stusys;
select * from student
    where speciality='计算机' or ssex='女';
#【例6.7】查询score表成绩为92、95的记录。

 

use stusys;
select * from score
    #where grade=92 or grade=95;
    where grade in(92,95);


#【例6.8】查询student表中不在1998年出生的学生情况 

 

 

 

use stusys;
select * from student
    where sbirthday not between '1998-01-01' and '1998-12-31';
#【例6.9】查询已选课但未参加考试的学生情况。

use stusys;
select * from score
    where grade is null;
#【例6.10】查询student表中姓董的学生情况。

 

 

use stusys;
select* from student
    where sname like '董%';
#【例6.11】查询含有“系统”或“数字”的所有课程名称。

use stusys;
select* from course
    where cname regexp '系统|数字';
#【例6.12】求学生的总人数。

 

 

use stusys;
select count(*)as 总人数
    from student;
#【例6.13】查询通信专业学生的总人数。

use stusys;
select count(*)as 总人数
    from student where speciality='通信';


#【例6.14】查询1201课程总分。

 

 

use stusys;
select sum(grade) as 课程1201总分
    from score where cno='1201';
#【例6.15】查询8001课程的最高分、最低分、平均成绩。

use stusys;
select max(grade) as 课程8001最高分,min(grade) as 课程8001最低分,avg(grade) as课程8001平均成绩
    from score where cno='8001';
#【例6.16】查询各门课程的最高分、最低分、平均成绩。

 

 

use stusys;
select cno as 课程号,max(grade) as 最高分,min(grade) as 最低分,avg(grade) as 平均成绩
    from score where not grade is null group by cno;
#【例6.17】查询平均成绩在90分以上的学生的学号和平均成绩。

use stusys;
select sno as 学号,avg(grade) as 平均成绩
    from score group by sno having avg(grade)>90;
#【例6.18】查询至少有5名学生选修且以8开头的课程号和平均分数。

 

 

use stusys;
select cno as 课程号,avg(grade) as 平均分数
    from score where cno like'8%' group by cno having count(*)>5;
#【例6.19】将计算机专业的学生按出生时间降序排序。

use stusys;
select * from student 
    where speciality='计算机' 
    order by sbirthday DESC;
#【例6.20】查询成绩表中成绩前3位学生的学号、课程号和成绩。

 

 

use stusys;
SELECT SNO,CNO,GRADE from score
    order by grade desc limit 0,3;

#【例6.21】采用交叉连接查询教师和和讲课地点所有可能组合。

use stusys;
select tname,location
    from teacher cross join lecture;
#【例6.22】查询每个学生选修课程的情况。

 

 

use stusys;
select student. *,score.*
    from student,score
    where student.sno=score.sno;
#【例6.23】查询选修了数据库系统课程且成绩在80分以上的学生情况。

use stusys;
select a.sno,sname,cname,grade
    from student a,score b,course c
    where a.sno=b.sno and b.cno=c.cno and cname='数据库系统'and grade>=80;
#【例6.24】对例6.22进行自然连接查询。

 

 

use stusys;
select * from student natural join score;
#【例6.25】查询选修了“1201”课程的成绩高于学号为“191002”的成绩的学生姓名。

use stusys;
select a.cno,a.sno,a.grade
    from score a,score b
    where a.grade>b.grade and a.cno='1201'and b.cno='1201'and b.sno='191002'
    order by a.grade desc;
#【例6.26】采用左外连接查询教师任课情况。

 

 

use stusys;
select tname,cno
    from teacher left join lecture on (teacher.tno=lecture.tno);
#【例6.27】采用右外连接查询教师任课情况。

use stusys;
select tno,cname
    from lecture right join course on(course.cno=lecture.cno);
#【例6.28】查询选修了课程号为8001的课程的学生情况。

 

 

use stusys;
select * from student
    where sno in (select sno from score
        where cno='8001'
        );
#【例6.29】查询选修某课程的学生人数多于4人的教师姓名。

use stusys;
select tname as 教师姓名
    from teacher
    where tno in (select tno from lecture
        where cno in(select a.cno
            from course a,score b
            where a.cno=b.cno
            group by a.cno
            having count(A.cno)>4
            )
        );


#【例6.30】查询比所有通信专业学生年龄都小的学生。

 

use stusys;
select * from student
    where sbirthday>all
        (select sbirthday from student
        where speciality='通信'
        );
#【例6.31】查询选修1004课程的学生姓名。

use stusys;
select sname as 姓名 from student
    where exists(select * from score
        where score.sno=student.sno and cno='1004'
        );


#【例6.32】查询性别为女及选修了课程号为4002的学生。 

 

 

use stusys;
select sno,sname,ssex from student
    where ssex='女'
    union
    select a.sno,a.sname,a.ssex from student a,score b
        where a.sno=b.sno and b.cno='4002';

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值