sql语句课程练习

Posted willem_chen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql语句课程练习相关的知识,希望对你有一定的参考价值。

sql语句课程练习

表结构

CREATE TABLE `course` (
  `Cid` varchar(10) NOT NULL DEFAULT '' COMMENT '课程ID',
  `Cname` varchar(255) NOT NULL DEFAULT '' COMMENT '课程名称',
  `Tid` varchar(10) NOT NULL DEFAULT '' COMMENT '教师ID',
  PRIMARY KEY (`Cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='课程表';

INSERT INTO `mysqldemo`.`course` (`Cid`, `Cname`, `Tid`) VALUES ('01', '语文', '02');
INSERT INTO `mysqldemo`.`course` (`Cid`, `Cname`, `Tid`) VALUES ('02', '数学', '01');
INSERT INTO `mysqldemo`.`course` (`Cid`, `Cname`, `Tid`) VALUES ('03', '英语', '03');

CREATE TABLE `sc` (
  `SCid` varchar(11) NOT NULL DEFAULT '' COMMENT '成绩ID',
  `Cid` varchar(11) NOT NULL DEFAULT '' COMMENT '课程ID',
  `score` varchar(255) NOT NULL DEFAULT '' COMMENT '成绩'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='成绩表';

INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('01', '01', '80');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('01', '02', '90');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('01', '03', '99');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('02', '01', '70');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('02', '02', '60');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('02', '03', '80');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('03', '01', '80');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('03', '02', '80');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('03', '03', '80');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('04', '01', '50');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('04', '02', '30');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('04', '03', '20');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('05', '01', '76');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('05', '02', '87');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('06', '01', '31');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('06', '03', '34');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('07', '02', '89');
INSERT INTO `mysqldemo`.`sc` (`SCid`, `Cid`, `score`) VALUES ('07', '03', '98');

CREATE TABLE `stu` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL DEFAULT '' COMMENT '名称',
  `kecheng` varchar(255) NOT NULL DEFAULT '' COMMENT '课程',
  `score` varchar(255) NOT NULL DEFAULT '' COMMENT '成绩'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生历史成绩表';

INSERT INTO `mysqldemo`.`stu` (`id`, `name`, `kecheng`, `score`) VALUES ('1', '张三', '语文', '81');
INSERT INTO `mysqldemo`.`stu` (`id`, `name`, `kecheng`, `score`) VALUES ('2', '张三', '数学', '75');
INSERT INTO `mysqldemo`.`stu` (`id`, `name`, `kecheng`, `score`) VALUES ('3', '李四', '语文', '76');
INSERT INTO `mysqldemo`.`stu` (`id`, `name`, `kecheng`, `score`) VALUES ('4', '李四', '数学', '90');
INSERT INTO `mysqldemo`.`stu` (`id`, `name`, `kecheng`, `score`) VALUES ('5', '王五', '语文', '81');
INSERT INTO `mysqldemo`.`stu` (`id`, `name`, `kecheng`, `score`) VALUES ('7', '王五', '数学', '100');
INSERT INTO `mysqldemo`.`stu` (`id`, `name`, `kecheng`, `score`) VALUES ('8', '王五', '数学', '81');

CREATE TABLE `student` (
  `Sid` varchar(11) NOT NULL DEFAULT '' COMMENT '学生ID',
  `Sname` varchar(255) NOT NULL DEFAULT '' COMMENT '学生名称',
  `Sage` varchar(255) NOT NULL DEFAULT '' COMMENT '学生年龄',
  `Ssex` varchar(255) NOT NULL DEFAULT '' COMMENT '学生性别'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生表';

INSERT INTO `mysqldemo`.`student` (`Sid`, `Sname`, `Sage`, `Ssex`) VALUES ('01', '赵雷', '1990-01-01', '男');
INSERT INTO `mysqldemo`.`student` (`Sid`, `Sname`, `Sage`, `Ssex`) VALUES ('02', '钱电', '1990-12-21', '男');
INSERT INTO `mysqldemo`.`student` (`Sid`, `Sname`, `Sage`, `Ssex`) VALUES ('03', '孙风', '1990-05-20', '男');
INSERT INTO `mysqldemo`.`student` (`Sid`, `Sname`, `Sage`, `Ssex`) VALUES ('04', '李云', '1990-08-06', '男');
INSERT INTO `mysqldemo`.`student` (`Sid`, `Sname`, `Sage`, `Ssex`) VALUES ('05', '周梅', '1991-12-01', '女');
INSERT INTO `mysqldemo`.`student` (`Sid`, `Sname`, `Sage`, `Ssex`) VALUES ('06', '吴兰', '1992-03-01', '女');
INSERT INTO `mysqldemo`.`student` (`Sid`, `Sname`, `Sage`, `Ssex`) VALUES ('07', '郑竹', '1989-07-01', '女');
INSERT INTO `mysqldemo`.`student` (`Sid`, `Sname`, `Sage`, `Ssex`) VALUES ('08', '王菊', '1990-01-20', '女');

CREATE TABLE `teacher` (
  `Tid` varchar(11) NOT NULL DEFAULT '' COMMENT '教师ID',
  `Tname` varchar(255) NOT NULL DEFAULT '' COMMENT '教师名称'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='教师表';

INSERT INTO `mysqldemo`.`teacher` (`Tid`, `Tname`) VALUES ('01', '张三');
INSERT INTO `mysqldemo`.`teacher` (`Tid`, `Tname`) VALUES ('02', '李四');
INSERT INTO `mysqldemo`.`teacher` (`Tid`, `Tname`) VALUES ('03', '王五');

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

SELECT stu.*,c1.score '01课程',c2.score '02课程',c3.score '03课程' from (SELECT score,Scid from sc

WHERE cid=01) c1,(SELECT score,Scid from sc

WHERE Cid=02) c2,student stu,(SELECT score,Scid from sc

WHERE Cid=02) c3

WHERE c1.Scid=c2.scid and stu.Sid=c1.scid and c3.scid=c2.scid and c1.score>c2.score

在这里插入图片描述

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

SELECT stu.*,c1.score '01课程',c2.score '02课程',c3.score '03课程' from (SELECT score,Scid from sc

WHERE cid=01) c1,(SELECT score,Scid from sc

WHERE Cid=02) c2,student stu,(SELECT score,Scid from sc

WHERE Cid=02) c3

WH

以上是关于sql语句课程练习的主要内容,如果未能解决你的问题,请参考以下文章

SQL语句练习

sql语句练习

练习一:对学生—课程数据库进行以下查询,按要求写出关系代数和SQL语句. 1. 查询全体学生的学号和姓名. .

sql语句经典练习50题

sql语句经典练习50题

SQL语句练习