数据库:

class:

 course:

student:

teacher:

score:

/*
Navicat Premium Data Transfer Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50624
Source Host : localhost
Source Database : sqlexam Target Server Type : MySQL
Target Server Version : 50624
File Encoding : utf-8 Date: 10/21/2016 06:46:46 AM
*/ SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`caption` varchar(32) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `class`
-- ----------------------------
BEGIN;
INSERT INTO `class` VALUES ('', '三年二班'), ('', '三年三班'), ('', '一年二班'), ('', '二年九班');
COMMIT; -- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(32) NOT NULL,
`teacher_id` int(11) NOT NULL,
PRIMARY KEY (`cid`),
KEY `fk_course_teacher` (`teacher_id`),
CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `course`
-- ----------------------------
BEGIN;
INSERT INTO `course` VALUES ('', '生物', ''), ('', '物理', ''), ('', '体育', ''), ('', '美术', '');
COMMIT; -- ----------------------------
-- Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`num` int(11) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_score_student` (`student_id`),
KEY `fk_score_course` (`course_id`),
CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `score`
-- ----------------------------
BEGIN;
INSERT INTO `score` VALUES ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', '');
COMMIT; -- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`gender` char(1) NOT NULL,
`class_id` int(11) NOT NULL,
`sname` varchar(32) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_class` (`class_id`),
CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `student`
-- ----------------------------
BEGIN;
INSERT INTO `student` VALUES ('', '男', '', '理解'), ('', '女', '', '钢蛋'), ('', '男', '', '张三'), ('', '男', '', '张一'), ('', '女', '', '张二'), ('', '男', '', '张四'), ('', '女', '', '铁锤'), ('', '男', '', '李三'), ('', '男', '', '李一'), ('', '女', '', '李二'), ('', '男', '', '李四'), ('', '女', '', '如花'), ('', '男', '', '刘三'), ('', '男', '', '刘一'), ('', '女', '', '刘二'), ('', '男', '', '刘四');
COMMIT; -- ----------------------------
-- Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`tid` int(11) NOT NULL AUTO_INCREMENT,
`tname` varchar(32) NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `teacher`
-- ----------------------------
BEGIN;
INSERT INTO `teacher` VALUES ('', '张磊老师'), ('', '李平老师'), ('', '刘海燕老师'), ('', '朱云海老师'), ('', '李杰老师');
COMMIT; SET FOREIGN_KEY_CHECKS = 1;

表结构和数据


1. 成绩表中所有大于60分的数据

SELECT * from score where num>60;

2. 查询每个老师教多少门课

SELECT teacher_id,COUNT(cid) FROM course GROUP BY teacher_id;


3. 列出课程与对应老师

SELECT * FROM course LEFT JOIN teacher on course.teacher_id=teacher.tid;


4. 列出学生与对应班级

SELECT * FROM student LEFT JOIN class on student.class_id=class.cid;


5. 列出男生的个数和女生的个数

SELECT gender,COUNT(gender) from student GROUP BY gender;


6. 查询“生物”课程比“物理”课程成绩高的所有学生的学号

先拿到所有的生物成绩
SELECT score.sid,score.student_id,course.cname,score.num from score LEFT JOIN course ON score.course_id=course.cid WHERE course.cname='生物';
在拿到所有的物理成绩
SELECT score.sid,score.student_id,course.cname,score.num from score LEFT JOIN course ON score.course_id=course.cid WHERE course.cname='物理';
接着输出生物成绩大于物理成绩的数据
SELECT
*
FROM
(
SELECT
score.sid,
score.student_id,
course.cname,
score.num
FROM
score
LEFT JOIN course ON score.course_id = course.cid
WHERE
course.cname = '生物'
) AS A
INNER JOIN (
SELECT
score.sid,
score.student_id,
course.cname,
score.num
FROM
score
LEFT JOIN course ON score.course_id = course.cid
WHERE
course.cname = '物理'
) AS B ON A.student_id = B.student_id
WHERE
A.num > B.num;

最后,把* 改为A.student_id 即可
SELECT
A.student_id
FROM
(
SELECT
score.sid,
score.student_id,
course.cname,
score.num
FROM
score
LEFT JOIN course ON score.course_id = course.cid
WHERE
course.cname = '生物'
) AS A
INNER JOIN (
SELECT
score.sid,
score.student_id,
course.cname,
score.num
FROM
score
LEFT JOIN course ON score.course_id = course.cid
WHERE
course.cname = '物理'
) AS B ON A.student_id = B.student_id
WHERE
A.num > B.num;


7. 查询平均成绩大于60分的同学的学号、姓名和平均成绩

select student_id,student.sname,avg(num) from score LEFT JOIN student ON score.student_id=student.sid GROUP BY student_id HAVING AVG(num)>60;

用临时表做:

SELECT B.student_id,student.sname,B.avg_num from (select student_id,avg(num) as avg_num from score GROUP BY student_id HAVING AVG(num)>60) as B LEFT join student on B.student_id=student.sid;


8. 查询所有同学的学号、姓名、选课数、总成绩

select student.sid,student.sname,COUNT(score.course_id),sum(score.num) from student LEFT JOIN score ON student.sid=score.student_id GROUP BY student.sid;


9. 查询姓“李”的老师的个数

select count(tid) from teacher where tname like '李%';


10. 查询没学过“李平”老师课的同学的学号、姓名

先查询选过李平老师课的学生ID
SELECT
student_id
FROM
score
WHERE
course_id IN ( SELECT course.cid FROM teacher LEFT JOIN course ON teacher.tid = course.teacher_id WHERE teacher.tname = '李平老师' )
GROUP BY
student_id

最后,从学生表里面选出没学过李平老师的课的数据
SELECT
student.sid,
student.sname
FROM
student
WHERE
sid NOT IN (
SELECT
student_id
FROM
score
WHERE
course_id IN ( SELECT course.cid FROM teacher LEFT JOIN course ON teacher.tid = course.teacher_id WHERE teacher.tname = '李平老师' )
GROUP BY
student_id
)


python学习之老男孩python全栈第九期_数据库day003 -- 作业的更多相关文章

  1. python学习之老男孩python全栈第九期_数据库day002 -- 作业 (数据库为day001创建的数据库)

    1.自行创建测试数据 对score表进行优化: 添加数据: 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 为了方便做题,额外增加几条数据 查询: 3.查询平均成绩大于60分的同学的学号和 ...

  2. python学习之老男孩python全栈第九期_数据库day004 -- 作业

    https://www.cnblogs.com/YD2018/p/9451809.html 11. 查询学过“001”并且也学过编号“002”课程的同学的学号.姓名 select student.si ...

  3. python学习之老男孩python全栈第九期_数据库day003知识点总结 —— MySQL数据库day3

    复习: 1. 增 insert into xx(name) values('root'),('xxx'); insert into xx(name) select id from tb1; 2. 自增 ...

  4. python学习之老男孩python全栈第九期_数据库day001 -- 作业

    创建如图所示数据库: 创建过程:  查看数据库,创建数据库 db1,再查看一下数据库  进入数据库,查看一下表  接着再创建一个class表 发现增加了重复数据,因此要把第二个修改一下  修改完数据之 ...

  5. python学习之老男孩python全栈第九期_数据库day005知识点总结 —— MySQL数据库day5

    三. MySQL视图(不常用) 给某个查询语句设置个别名(视图名),日后方便使用 - 创建: create view 视图名 as SQL; PS:视图是虚拟的 - 修改: alter view 视图 ...

  6. python学习之老男孩python全栈第九期_数据库day004知识点总结 —— MySQL数据库day4

    复习: 1. MySQL:文件管理的软件 2. 三部分: - 服务端 - SQL语句 - 客户端 3. 客户端: - MySQL - navicat 4. 授权操作: - 用户操作 - 授权操作 5. ...

  7. python学习之老男孩python全栈第九期_数据库day002知识点总结 —— MySQL数据库day2(全部)

    一. 复习1. MySQL: - 服务端 - 客户端2. 通信交流 - 授权 - SQL语句 - 数据库 创建数据库: create database db1 default charset utf8 ...

  8. python学习之老男孩python全栈第九期_数据库day001知识点总结 —— MySQL操作数据库以及数据表、基本数据类型、基本增删改查、外键定义以及创建

    一. 学习SQL语句规则以及外键 1. 操作文件夹 create database db2; 创建文件夹 create database db2 default charset utf8; 创建文件夹 ...

  9. python学习之老男孩python全栈第九期_第一次周末考试题(over)第三次添加完毕

    day 6python基础数据类型考试题 考试时间:两个半小时 满分100分(80分以上包含80分及格) 一,基础题. 1. 简述变量命名规范(3分) 答:(1) 变量为数字,字母以及下划线的任意组合 ...

随机推荐

  1. sql语句_统计总成绩最高的前2名

    有一个数据表,id user_id score 三个字段,计算总成绩最高的前两名 SELECT * FROM (SELECT user_name,SUM(score) AS score FROM us ...

  2. DCL,即Double Check Lock,中卫双重检查锁定。

    DCL,即Double Check Lock,中卫双重检查锁定. [Java并发编程]之十六:深入Java内存模型——happen-before规则及其对DCL的分析(含代码) 关于单例.关于DCL: ...

  3. Git进行fork后如何与原仓库同步

    在进行Git协同开发的时候,往往会去fork一个仓库到自己的Git中,过一段时间以后,原仓库可能会有各种提交以及修改,很可惜,Git本身并没有自动进行同步的机制,这个需要手动去执行.name如何进行自 ...

  4. sublime text 内调试Javascript代码

    转自:sublime内调试Javascript代码 之前用webstorm, 可以直接调试js, 在浏览器中也可以调试js,最近换了sublime text, 在想它是否支持调试js代码,于是找到了这 ...

  5. iOS多线程---NSOperation的常用操作

    1.最大并发数: - (NSInteger)maxConcurrentOperationCount;- (void)setMaxConcurrentOperationCount:(NSInteger) ...

  6. chrome inspect 远程调试H5

    chrome://inspect/#devices 一个内置于chrome的远程调试指令,满足远程调试的几个必须条件 1,能够访问https://chrome-devtools-frontend.ap ...

  7. 【GDOI2018模拟8】 数学竞赛 三角函数性质+记忆化搜索

    数据范围:p,q≤20. 只能说我整个人傻逼了..... 我们考虑三角函数的部分性质: $sin(x)=\sqrt{ 1-cos^2(x)}$ $cos(x)=\sqrt{1-sin^2(x)}$ $ ...

  8. POJ 1018

    #include<iostream> #define MAXN 105 #define inf 10000000 #include<vector> #include<io ...

  9. manifest.xml

    main action 和 laucher的categoty If either the MAIN action or LAUNCHER category are not declared for o ...

  10. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...