sql语句课程练习

Posted willem_chen

tags:

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

sql语句课程练习(2)

表结构

表名和字段

–1.学生表
Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别

–2.课程表
Course(c_id,c_name,t_id) – --课程编号, 课程名称, 教师编号

–3.教师表
Teacher(t_id,t_name) --教师编号,教师姓名

–4.成绩表
Score(s_id,c_id,s_score) --学生编号,课程编号,分数

CREATE TABLE `Course` (
  `c_id` varchar(20) NOT NULL COMMENT '课程编号',
  `c_name` varchar(20) NOT NULL DEFAULT '' COMMENT '课程名称',
  `t_id` varchar(20) NOT NULL COMMENT '教师编号',
  PRIMARY KEY (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='课程表';

INSERT INTO `mysqldemo`.`Course` (`c_id`, `c_name`, `t_id`) VALUES ('01', '语文', '02');
INSERT INTO `mysqldemo`.`Course` (`c_id`, `c_name`, `t_id`) VALUES ('02', '数学', '01');
INSERT INTO `mysqldemo`.`Course` (`c_id`, `c_name`, `t_id`) VALUES ('03', '英语', '03');

CREATE TABLE `Student` (
  `s_id` varchar(20) NOT NULL COMMENT '学生编号',
  `s_name` varchar(20) NOT NULL DEFAULT '' COMMENT '学生姓名',
  `s_birth` varchar(20) NOT NULL DEFAULT '' COMMENT '出生年月',
  `s_sex` varchar(10) NOT NULL DEFAULT '' COMMENT '学生性别',
  PRIMARY KEY (`s_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生表';

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

CREATE TABLE `Teacher` (
  `t_id` varchar(20) NOT NULL COMMENT '教师编号',
  `t_name` varchar(20) NOT NULL DEFAULT '' COMMENT '教师姓名',
  PRIMARY KEY (`t_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='教师表';

INSERT INTO `mysqldemo`.`Teacher` (`t_id`, `t_name`) VALUES ('01', '张三');
INSERT INTO `mysqldemo`.`Teacher` (`t_id`, `t_name`) VALUES ('02', '李四');
INSERT INTO `mysqldemo`.`Teacher` (`t_id`, `t_name`) VALUES ('03', '王五');

CREATE TABLE `Score` (
  `s_id` varchar(20) NOT NULL COMMENT '学生编号',
  `c_id` varchar(20) NOT NULL COMMENT '课程编号',
  `s_score` int(3) DEFAULT NULL COMMENT '分数',
  PRIMARY KEY (`s_id`,`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='成绩表';

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

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

第一种方式

select a.* ,b.s_score as 01_score,c.s_score as 02_score from 
Student a 
	join Score b on a.s_id=b.s_id and b.c_id='01'
	left join Score c on a.s_id=c.s_id and c.c_id='02' or c.c_id = NULL where b.s_score>c.s_score

在这里插入图片描述
第二种方式

select a.*,b.s_score as 01_score,c.s_score as 02_score from Student a,Score b,Score c 
		where a.s_id=b.s_id 
		and a.s_id=c.s_id 
		and b.c_id='01' 
		and c.c_id='02' 
		and b.s_score>c.s_score

在这里插入图片描述

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

select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from 
	Student b 
	join Score a on b.s_id = a.s_id
	GROUP BY b.s_id,b.s_name HAVING avg_score >=60;

在这里插入图片描述

3、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩-- (包括有成绩的和无成绩的)

select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from 
	Student b 
	left join Score a on b.s_id = a.s_id
	GROUP BY b.s_id,b.s_name HAVING avg_score <60
	union
select a.s_id,a.s_name,0 as avg_score from 
	Student a 
	where a.s_id not in (
				select distinct s_id from Score);

在这里插入图片描述

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

select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score from 
	Student a 
	left join Score b on a.s_id=b.s_id
	GROUP BY a.s_id,a.s_name;

在这里插入图片描述

5、查询学过"张三"老师授课的同学的信息

select a.* from 
	Student a 
	join Score b on a.s_id=b.s_id where b.c_id in(
		select c_id from Course where t_id =(
			select t_id from Teacher where t_name = '张三'));

在这里插入图片描述

6、查询没学过"张三"老师授课的同学的信息

select * from 
    Student c 
    where c.s_id not in(
        select a.s_id from Student a join Score b on a.s_id=b.s_id where b.c_id in(
        select a.c_id from Course a join Teacher b on a.t_id = b.t_id where t_name ='张三'));

在这里插入图片描述

7、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select a.* from 
	Student a,Score b,Score c 
	where a.s_id = b.s_id  and a.s_id = c.s_id and b.c_id='01' and c.c_id='02';

在这里插入图片描述

8、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

select a.* from 
	Student a 
	where a.s_id in (select s_id from Score where c_id='01' ) and a.s_id not in(select s_id from Score where c_id='02')

在这里插入图片描述

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

select s.

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

SQL语句练习

sql语句练习

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

sql语句经典练习50题

sql语句经典练习50题

SQL语句练习