sql练习

2021-04-26  本文已影响0人  大晴天小阳光

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------

-- Table structure for student

-- ----------------------------

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student` (

  `Sid` varchar(6) NOT NULL DEFAULT '',

  `Sname` varchar(10) DEFAULT NULL,

  `Sage` datetime DEFAULT NULL,

  `Ssex` varchar(10) DEFAULT NULL,

  PRIMARY KEY (`Sid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------

-- Table structure for teacher

-- ----------------------------

DROP TABLE IF EXISTS `teacher`;

CREATE TABLE `teacher` (

  `Tid` varchar(10) NOT NULL DEFAULT '',

  `Tname` varchar(10) DEFAULT NULL,

  PRIMARY KEY (`Tid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------

-- Table structure for course

-- ----------------------------

DROP TABLE IF EXISTS `course`;

CREATE TABLE `course` (

  `Cid` varchar(10) NOT NULL DEFAULT '',

  `Tid` varchar(10) NOT NULL DEFAULT '',

  `Cname` varchar(10) DEFAULT NULL,

  PRIMARY KEY (`Cid`),

  CONSTRAINT `course_ibfk_1` FOREIGN KEY (`Tid`) REFERENCES `teacher` (`Tid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------

-- Table structure for sc

-- ----------------------------

DROP TABLE IF EXISTS `sc`;

CREATE TABLE `sc` (

  `Sid` varchar(10) NOT NULL DEFAULT '',

  `Cid` varchar(10) NOT NULL DEFAULT '',

  `score` decimal(18,1) DEFAULT NULL,

  CONSTRAINT `sc_ibfk_1` FOREIGN KEY (`Sid`) REFERENCES `student` (`Sid`),

  CONSTRAINT `sc_ibfk_2` FOREIGN KEY (`Cid`) REFERENCES `course` (`Cid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into Student values('01' , '赵雷' , '1990-01-01' , '男');

insert into Student values('02' , '钱电' , '1990-12-21' , '男');

insert into Student values('03' , '孙风' , '1990-05-20' , '男');

insert into Student values('04' , '李云' , '1990-08-06' , '男');

insert into Student values('05' , '周梅' , '1991-12-01' , '女');

insert into Student values('06' , '吴兰' , '1992-03-01' , '女');

insert into Student values('07' , '郑竹' , '1989-07-01' , '女');

insert into Student values('08' , '王菊' , '1990-01-20' , '女');

insert into SC values('01' , '01' , 80);

insert into SC values('01' , '02' , 90);

insert into SC values('01' , '03' , 99);

insert into SC values('02' , '01' , 70);

insert into SC values('02' , '02' , 60);

insert into SC values('02' , '03' , 80);

insert into SC values('03' , '01' , 80);

insert into SC values('03' , '02' , 80);

insert into SC values('03' , '03' , 80);

insert into SC values('04' , '01' , 50);

insert into SC values('04' , '02' , 30);

insert into SC values('04' , '03' , 20);

insert into SC values('05' , '01' , 76);

insert into SC values('05' , '02' , 87);

insert into SC values('06' , '01' , 31);

insert into SC values('06' , '03' , 34);

insert into SC values('07' , '02' , 89);

insert into SC values('07' , '03' , 98);

insert into Course values('01' , '语文' , '02');

insert into Course values('02' , '数学' , '01');

insert into Course values('03' , '英语' , '03');

insert into Teacher values('01' , '张三');

insert into Teacher values('02' , '李四');

insert into Teacher values('03' , '王五');

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

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

3. 查询在 SC 表存在成绩的学生信息

4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

4.1 查有成绩的学生信息

5. 查询「李」姓老师的数量

6. 查询学过「张三」老师授课的同学的信息

7. 查询没有学全所有课程的同学的信息

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

9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息

10. 查询没学过"张三"老师讲授的任一门课程的学生姓名

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

13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

14. 查询各科成绩最高分、最低分和平均分,以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90)。

15. 按平均成绩进行排序,显示总排名和各科排名,Score 重复时保留名次空缺

15.1 按平均成绩进行排序,显示总排名和各科排名,Score 重复时合并名次

17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

18. 查询各科成绩前三名的记录

20. 查询出只选修两门课程的学生学号和姓名

22. 查询名字中含有「风」字的学生信息

24. 查询 1990 年出生的学生名单

33. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

40. 查询各学生的年龄,只按年份来算

41. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

42. 查询本周过生日的学生

43. 查询下周过生日的学生

44. 查询本月过生日的学生

45. 查询下月过生日的学生

46.查询所有课程成绩都大于80分的学生名字

47.查询所有课程成绩都大于80分的学生名字,不显示重复名字。

查询学生的最高成绩。

SELECT sid,score from sc group by sid ;

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

select s.* ,a.score as score_01,b.score as score_02 FROM

student s,

(SELECT Sid,score from sc WHERE sc.Cid = "01") a,

(SELECT Sid,score from sc WHERE sc.Cid = "02") b

where a.Sid = b.Sid and a.score> b.score and s.Sid = a.Sid;

SELECT a.*,b.score score1 from

(SELECT Sid,score from sc WHERE sc.Cid = "01") a,

(SELECT Sid,score from sc WHERE sc.Cid = "02") b

where a.sid = b.sid and a.score > b.score;

#2. 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT sid,AVG(score) as avgg from sc GROUP BY sid  having AVG(score) >60;

SELECT Sname,sc.sid,AVG(score) as avgg from sc,student s WHERE sc.Sid = s.Sid GROUP BY sid  having AVG(score) >60;

#3. 查询在 SC 表存在成绩的学生信息

SELECT * from student where sid in(SELECT Sid from sc WHERE score is not null);

SELECT s.*,sc.score FROM student s,sc WHERE s.Sid = sc.sid and sc.score is not null group BY sc.sid;

#4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

这道题得用到left join或者right join,不能用where连接,因为题目说了要求有显示为null的,where是inner join,不会出现null,在这道题里会查不出第08号学生。

select  count(sid) as xuankezongshu, sum(score) as xuankezongfen from sc GROUP BY Sid;

select  s.*,count(sc.sid) as xuankezongshu, sum(score) as xuankezongshu from  student s, sc WHERE s.Sid = sc.sid GROUP BY sc.Sid;

select  s.*,count(sc.sid) as xuankezongshu, sum(score) as xuankezongshu from  student s LEFT JOIN sc on s.Sid = sc.sid GROUP BY sc.Sid;

4.1 查有成绩的学生信息  ***************************

select s.*,a.xuankezongshu,a.xuankezongfen,a.score from student s,

(SELECT sid,score,count(sid) as xuankezongshu, sum(score) as xuankezongfen from  sc where score is not NULL GROUP BY sid) a

WHERE s.sid = a.sid

select s.sid, s.sname, count(*) as 选课总数, sum(score) as 总成绩,

    sum(case when cid = 01 then score else null end) as score_01,

    sum(case when cid = 02 then score else null end) as score_02,

    sum(case when cid = 03 then score else null end) as score_03

from student as s, sc

where s.sid = sc.sid

group by s.sid

5. 查询「李」姓老师的数量

SELECT count(tid) from teacher t WHERE t.tname LIKE "李%";

6. 查询学过「张三」老师授课的同学的信息

SELECT * from student WHERE sid in (SELECT sid FROM sc where cid in (SELECT cid from course WHERE tid in ( select tid from teacher t WHERE t.Tname = "张三" )));

select * from student where sid in (

    select sid from sc, course, teacher

    where sc.cid = course.cid

    and course.tid = teacher.tid

    and tname = '张三'

)

7. 查询没有学全所有课程的同学的信息

select * from student where sid in (select sid from sc group by sid having count(cid) < 3)

SELECT s.*,b.xuankeshu from student s,

(

select sid,count(*) as xuankeshu from sc group by sid HAVING xuankeshu <

(select count(*) coursecount from (SELECT count(cid) from sc group by cid) a)

) b

where s.sid = b.sid;

9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息

参考地址:https://blog.csdn.net/u010452388/article/details/80150985

上一篇下一篇

猜你喜欢

热点阅读