目录
练习题30道
1.查询“1”号学生的姓名和各科成绩:
2.查询各个学科的平均成绩和最高成绩:
3.查询所有姓张的同学的各科成绩:
4.查询每个同学的最高成绩和科目名称
5.查询每个课程的最高分的学生信息
6.查询名字中含有'张'或'李'字的学生的信息和各科成绩。
7.查询平均成绩大于70的同学的信息。(子查询)
8.将学生按照总分数进行排名。(从高到低)
9.查询数学成绩的最高分、最低分、平均分。
10.将各科目按照平均分排序。
11.查询老师的信息和他所带的科目的平均分
12.查询被"Tom"和"Jerry"教的课程的最高分和最低分
13.查询每个学生的最好成绩的科目名称(子查询)
14.查询所有学生的课程及分数
15.查询课程编号为1且课程成绩在60分以上的学生的学号和姓名(子查询)
16. 查询平均成绩大于等于70的所有学生学号、姓名和平均成绩
17.查询有不及格课程的学生信息
18.查询每门课程有成绩的学生人数
19.查询每门课程的平均成绩,结果按照平均成绩降序排列,如果平均成绩相同,再按照课程编号升序排列
20.查询平均成绩大于60分的同学的学生编号和学生姓名和平均成绩
21.查询有且仅有一门课程成绩在80分以上的学生信息
22.查询出只有三门课程的学生的学号和姓名
23.查询有不及格课程的课程信息
24.查询至少选择4门课程的学生信息
25.查询没有选全所有课程的同学的信息
26.查询选全所有课程的同学的信息
27.查询各学生都选了多少门课
28.查询课程名称为"java",且分数低于60分的学生姓名和分数
29.查询学过"Tony"老师授课的同学的信息
30.查询没学过"Tony"老师授课的学生信息
作业
首先建立表:
建card表
建student表
建立book表:
建立borrowrecored表:
插入数据:
1.根据学生学号 查询 2020年-1-1之后借过哪些书籍
2.根据书籍编号 查询2020-1-1之前都有那哪些学生借阅过
3.根据学生编号,查阅该生借阅过哪些书籍
4.根据学生编号查询该生一共借阅过多少本书
5.根据书籍编号查询书籍一共被借阅过多少次
6.根据学院名称查询该学院的学生一共借过哪些书籍
7.查询哪个学生最爱看书
8.每本书被借阅了多少次
9.查询那本书最受欢迎
总结:
进行student表和scores表的id相连接,course表和scores表的id相连接
SELECT
s.id sid,
s.`name` sname,
c.`name` cname,
sc.score
FROM
student s
LEFT JOIN scores sc ON s.id = sc.s_id
LEFT JOIN course c ON c.id = sc.c_id
WHERE
s.id = 1;
SELECT
c.id,
c.`name`,
AVG( sc.score ),
max( sc.score )
FROM
course c
LEFT JOIN scores sc ON c.id = sc.c_id
GROUP BY
c.id,
c.`name`;
SELECT
s.id,
s.`name`,
c.`name` cname,
sc.score
FROM
student s
LEFT JOIN scores sc ON sc.s_id = s.id
LEFT JOIN course c ON c.id = sc.c_id
WHERE
s.`name` LIKE '张%';
SELECT
t.id,
t.NAME,
c.id,
c.NAME,
r.score
FROM
(
SELECT
s.id,
s.NAME,(
SELECT
max( score )
FROM
scores r
WHERE
r.s_id = s.id
) score
FROM
student s
) t
LEFT JOIN scores r ON r.s_id = t.id
AND r.score = t.score
LEFT JOIN course c ON r.c_id = c.id;
SELECT
*
FROM
student s
WHERE
id IN (
SELECT DISTINCT
r.s_id
FROM
(
SELECT
c.id,
c.NAME,
max( score ) score
FROM
student s
LEFT JOIN scores r ON r.s_id = s.id
LEFT JOIN course c ON c.id = r.c_id
GROUP BY
c.id,
c.NAME
) t
LEFT JOIN scores r ON r.c_id = t.id
AND t.score = r.score
);
SELECT
s.id,
s.NAME sname,
sc.score,
c.NAME
FROM
student s
LEFT JOIN scores sc ON s.id = sc.s_id
LEFT JOIN course c ON sc.c_id = c.id
WHERE
s.NAME LIKE '%张%'
OR s.NAME LIKE '%李%';
SELECT
*
FROM
student
WHERE
id IN (
SELECT
sc.s_id
FROM
scores sc
GROUP BY
sc.s_id
HAVING
avg( sc.score ) >= 70
);
SELECT
s.id,
s.NAME,
sum( sc.score ) score
FROM
student s
LEFT JOIN scores sc ON s.id = sc.s_id
GROUP BY
s.id,
s.NAME
ORDER BY
score DESC,
s.id ASC;
SELECT
c.NAME,
max( sc.score ),
min( sc.score ),
avg( sc.score )
FROM
course c
LEFT JOIN scores sc ON c.id = sc.c_id
WHERE
c.NAME = '数学';
SELECT
c.id,
c.NAME,
avg( sc.score ) score
FROM
course c
LEFT JOIN scores sc ON c.id = sc.c_id
GROUP BY
c.id,
c.NAME
ORDER BY
score DESC;
SELECT
t.id,
t.NAME,
c.id cid,
c.NAME cname,
avg( r.score )
FROM
teacher t
LEFT JOIN course c ON t.id = c.t_id
LEFT JOIN scores r ON r.c_id = c.id
GROUP BY
t.id,
t.NAME,
c.id,
c.NAME;
SELECT
t.id,
t.NAME,
c.id cid,
c.NAME cname,
max( r.score ),
min( r.score )
FROM
teacher t
LEFT JOIN course c ON t.id = c.t_id
LEFT JOIN scores r ON r.c_id = c.id
GROUP BY
t.id,
t.NAME,
c.id,
c.NAME
HAVING
t.NAME IN ( 'Tom', 'Jerry' );
SELECT
t.id,
t.sname,
r.c_id,
c.NAME,
t.score
FROM
(
SELECT
s.id,
s.NAME sname,
max( r.score ) score
FROM
student s
LEFT JOIN scores r ON r.s_id = s.id
GROUP BY
s.id,
s.NAME
) t
LEFT JOIN scores r ON r.s_id = t.id
AND r.score = t.score
LEFT JOIN course c ON r.c_id = c.id;
SELECT
s.id,
s.NAME,
c.id,
c.NAME,
r.score
FROM
student s
LEFT JOIN scores r ON s.id = r.s_id
LEFT JOIN course c ON c.id = r.c_id;
SELECT
s.*,
r.*
FROM
student s
LEFT JOIN scores r ON s.id = r.s_id
WHERE
r.c_id = 1
AND r.score > 60
SELECT
s.id,
s.NAME,
t.score
FROM
student s
LEFT JOIN ( SELECT r.s_id, avg( r.score ) score FROM scores r GROUP BY r.s_id ) t ON s.id = t.s_id
WHERE
t.score >= 70;
SELECT
*
FROM
student s
WHERE
id IN ( SELECT r.s_id FROM scores r GROUP BY r.s_id HAVING min( r.score ) < 60 );
SELECT
c.id,
c.NAME,
count(*)
FROM
course c
LEFT JOIN scores r ON c.id = r.c_id
GROUP BY
c.id,
c.NAME;
SELECT
c.id,
c.NAME,
avg( score ) score
FROM
course c
LEFT JOIN scores r ON c.id = r.c_id
GROUP BY
c.id,
c.NAME
ORDER BY
score DESC,
c.id ASC;
SELECT
s.id,
s.NAME sname,
avg( r.score ) score
FROM
student s
LEFT JOIN scores r ON r.s_id = s.id
LEFT JOIN course c ON c.id = r.c_id
GROUP BY
s.id,
s.NAME
HAVING
score > 65;
SELECT
s.id,
s.NAME,
s.gender
FROM
student s
LEFT JOIN scores r ON s.id = r.s_id
WHERE
r.score > 80
GROUP BY
s.id,
s.NAME,
s.gender
HAVING
count(*) = 1;
SELECT
s.id,
s.NAME,
s.gender
FROM
student s
LEFT JOIN scores r ON s.id = r.s_id
GROUP BY
s.id,
s.NAME,
s.gender
HAVING
count(*) = 3;
SELECT
*
FROM
course c
WHERE
id IN (
SELECT
r.c_id
FROM
scores r
GROUP BY
r.c_id
HAVING
min( r.score ) < 60
);
SELECT
s.id,
s.NAME
FROM
student s
LEFT JOIN scores r ON s.id = r.s_id
GROUP BY
s.id,
s.NAME
HAVING
count(*) >= 4;
SELECT
*
FROM
student
WHERE
id IN (
SELECT
r.s_id
FROM
scores r
GROUP BY
r.s_id
HAVING
count(*) != 5
);
SELECT
s.id,
s.NAME,
count(*) number
FROM
student s
LEFT JOIN scores r ON s.id = r.s_id
GROUP BY
s.id,
s.NAME
HAVING
number = ( SELECT count(*) FROM course );
SELECT
s.id,
s.NAME,
count(*) number
FROM
student s
LEFT JOIN scores r ON s.id = r.s_id
GROUP BY
s.id,
s.NAME;
SELECT
s.id,
s.NAME,
r.score
FROM
student s
LEFT JOIN scores r ON s.id = r.s_id
LEFT JOIN course c ON r.c_id = c.id
WHERE
c.NAME = 'java'
AND r.score < 60;
SELECT
s.id,
s.NAME
FROM
student s
LEFT JOIN scores r ON r.s_id = s.id
LEFT JOIN course c ON c.id = r.c_id
LEFT JOIN teacher t ON t.id = c.t_id
WHERE
t.NAME = 'Tom';
SELECT
*
FROM
student
WHERE
id NOT IN (
SELECT DISTINCT
s.id
FROM
student s
LEFT JOIN scores r ON r.s_id = s.id
LEFT JOIN course c ON c.id = r.c_id
LEFT JOIN teacher t ON t.id = c.t_id
WHERE
t.NAME = 'Tom'
)
DROP TABLE IF EXISTS `card`;
CREATE TABLE `card` (
`cid` varchar(6) NOT NULL,
`copendate` date NULL DEFAULT NULL,
`cprivilige` char(1) NULL DEFAULT NULL,
PRIMARY KEY (`cid`) USING BTREE
) ENGINE = InnoDB;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`sname` varchar(6) DEFAULT NULL,
`sage` int(2) NULL DEFAULT NULL,
`sdepartment` varchar(20) NULL DEFAULT NULL,
`senterdate` date NULL DEFAULT NULL,
`cid` varchar(6) NULL DEFAULT NULL,
PRIMARY KEY (`sid`) USING BTREE,
INDEX `fk_student_cid`(`cid`) USING BTREE,
CONSTRAINT `fk_student_cid` FOREIGN KEY (`cid`) REFERENCES `card` (`cid`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE = InnoDB;
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`bid` varchar(6) NOT NULL,
`bname` varchar(30) NULL DEFAULT NULL,
`bauthor` varchar(50)NULL DEFAULT NULL,
`bpublishdate` date NULL DEFAULT NULL,
PRIMARY KEY (`bid`) USING BTREE
) ENGINE = InnoDB;
DROP TABLE IF EXISTS `borrowrecored`;
CREATE TABLE `borrowrecored` (
`cid` varchar(6) NOT NULL,
`bid` varchar(6) NOT NULL,
`bdate` date NULL DEFAULT NULL,
PRIMARY KEY (`cid`, `bid`) USING BTREE,
INDEX `fk_borrowrecored_bid`(`bid`) USING BTREE,
FOREIGN KEY (`bid`) REFERENCES `book` (`bid`)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`cid`) REFERENCES `card` (`cid`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;
INSERT INTO `student` VALUES (1, '张三', 20, '机械设计', '2019-06-06', 'c1');
INSERT INTO `student` VALUES (2, '李四', 19, '机械设计', '2019-09-08', 'c2');
INSERT INTO `student` VALUES (3, '王五', 21, '软件学院', '2019-10-10', 'c3');
INSERT INTO `book` VALUES ('b1', '盗墓笔记', '唐家三少', '2010-02-02');
INSERT INTO `book` VALUES ('b2', '西游记', '吴承恩', '2016-06-06');
INSERT INTO `book` VALUES ('b3', '红楼梦', '曹雪芹', '2015-05-05');
INSERT INTO `book` VALUES ('b4', '水浒传', '施耐庵', '2014-04-04');
INSERT INTO `book` VALUES ('b5', '三国演义', '罗贯中', '2012-02-02');
INSERT INTO `card` VALUES ('c1', '2020-01-01', 'A');
INSERT INTO `card` VALUES ('c2', '2020-02-02', 'B');
INSERT INTO `card` VALUES ('c3', '2020-03-03', 'C');
INSERT INTO `borrowrecored` VALUES ('c1', 'b1', '2019-11-03');
INSERT INTO `borrowrecored` VALUES ('c1', 'b3', '2019-12-15');
INSERT INTO `borrowrecored` VALUES ('c2', 'b1', '2019-12-16');
INSERT INTO `borrowrecored` VALUES ('c2', 'b2', '2020-02-02');
INSERT INTO `borrowrecored` VALUES ('c2', 'b3', '2020-03-06');
INSERT INTO `borrowrecored` VALUES ('c2', 'b4', '2020-01-05');
INSERT INTO `borrowrecored` VALUES ('c3', 'b1', '2019-12-18');
INSERT INTO `borrowrecored` VALUES ('c3', 'b2', '2020-03-08');
SELECT
b.bdate,
s.sname
FROM
student s
LEFT JOIN card c ON c.cid=s.cid
LEFT JOIN borrowrecored b ON b.cid=s.cid
LEFT JOIN book bo ON bo.bid=b.bid
WHERE
b.bdate>'2020-1-1';
SELECT
b.bdate,
s.sname
FROM
student s
LEFT JOIN card c ON c.cid=s.cid
LEFT JOIN borrowrecored b ON b.cid=s.cid
LEFT JOIN book bo ON bo.bid=b.bid
WHERE
b.bdate<'2020-1-1';
SELECT
s.sname,
bo.bname
FROM
student s
LEFT JOIN card c ON c.cid=s.cid
LEFT JOIN borrowrecored b ON b.cid=s.cid
LEFT JOIN book bo ON bo.bid=b.bid
WHERE
s.sid=1;
SELECT
s.sname,
COUNT(bo.bname)
FROM
student s
LEFT JOIN card c ON c.cid=s.cid
LEFT JOIN borrowrecored b ON b.cid=s.cid
LEFT JOIN book bo ON bo.bid=b.bid
WHERE
s.sid=1
GROUP BY
s.sname;
SELECT
bo.bname,
bo.bid,
COUNT(s.sname)
FROM
student s
LEFT JOIN card c ON c.cid=s.cid
LEFT JOIN borrowrecored b ON b.cid=s.cid
LEFT JOIN book bo ON bo.bid=b.bid
WHERE
bo.bid='b1'
GROUP BY
bo.bname,
bo.bid;
SELECT
bo.bname,
bo.bid,
COUNT(s.sname)
FROM
student s
LEFT JOIN card c ON c.cid=s.cid
LEFT JOIN borrowrecored b ON b.cid=s.cid
LEFT JOIN book bo ON bo.bid=b.bid
WHERE
s.sdepartment='机械设计'
GROUP BY
bo.bname,
bo.bid;
SELECT
s.sname name,
s.sid id,
COUNT(bo.bid) ci
FROM
student s
LEFT JOIN card c ON c.cid=s.cid
LEFT JOIN borrowrecored b ON b.cid=s.cid
LEFT JOIN book bo ON bo.bid=b.bid
GROUP BY
s.sname,
s.sid
ORDER BY
COUNT(bo.bid) DESC
LIMIT 1;
SELECT
bo.bname name,
bo.bid id,
COUNT(s.sname) ci
FROM
student s
LEFT JOIN card c ON c.cid=s.cid
LEFT JOIN borrowrecored b ON b.cid=s.cid
LEFT JOIN book bo ON bo.bid=b.bid
GROUP BY
bo.bname,
bo.bid;
SELECT
bo.bname name,
bo.bid id,
COUNT(s.sname) ci
FROM
student s
LEFT JOIN card c ON c.cid=s.cid
LEFT JOIN borrowrecored b ON b.cid=s.cid
LEFT JOIN book bo ON bo.bid=b.bid
GROUP BY
bo.bname,
bo.bid
ORDER BY
COUNT(s.sname) DESC
LIMIT 1;
将这两天学习的Mysql知识进行实际操作,目标是在以后开发中Mysql不会成为我们的绊脚石