MYSQL经典练习题

(本练习题可让你熟悉DQL,快速的上手DQL)

首先,先在数据库中建立基本数据库以及表项:

DROP DATABASE IF EXISTS `test`;

CREATE DATABASE `test`;

USE `test`

-- 表一:student学生use)
CREATE TABLE student(
sno VARCHAR(20) PRIMARY KEY NOT NULL COMMENT'学号(主码)',
sname VARCHAR(20) NOT NULL COMMENT'学生姓名',
ssex VARCHAR(20) NOT NULL COMMENT'学生性别',
sbirthday DATETIME COMMENT'学生出生年月',
class VARCHAR(20) COMMENT'学生所在班级'
);
-- 表(二)Course(课程表)
CREATE TABLE course(
cno VARCHAR(20) PRIMARY KEY NOT NULL COMMENT'课程号(主码)',
cname VARCHAR(20) NOT NULL COMMENT'课程名称',
tno VARCHAR(20) NOT NULL COMMENT'教工编号'
);
-- 表(三)Score(成绩表)
CREATE TABLE score(
id INT PRIMARY KEY AUTO_INCREMENT COMMENT'主键自增',
sno VARCHAR(20) NOT NULL COMMENT'学号',
cno VARCHAR(20) NOT NULL COMMENT'课程号',
degree DECIMAL(4,1) COMMENT'成绩'
);
-- 表四 teacher(教师表)
CREATE TABLE teacher(
tno VARCHAR(20) PRIMARY KEY NOT NULL COMMENT'教工编号(主码)',
tname VARCHAR(20) NOT NULL COMMENT'教工姓名',
tsex VARCHAR(20) NOT NULL COMMENT'教工性别',
tbirthday DATETIME COMMENT'教工出生年月',
prof VARCHAR(20) COMMENT'职称',
depart VARCHAR(20) NOT NULL COMMENT'教工所在部门'
);

然后在表中插入会用到的数据

-- 学生表数据的插入:
INSERT INTO student VALUES
('108','曾华','男','1977-09-01','95033'),
('105','匡明','男','1977-09-01','95031'),
('107','王丽','女','1977-09-01','95033'),
('101','李军','男','1977-09-01','95033'),
('109','王芳','女','1977-09-01','95031'),
('103','陆君','男','1977-09-01','95031');
-- 课程表数据的插入:
INSERT INTO course VALUES
('3-105','计算机导论','825'),
('3-245','操作系统','804'),
('6-166','数字电路','856'),
('9-888','高等数学','831');
-- 成绩表数据的插入:
INSERT INTO score(sno,cno,degree) VALUES
('103','3-245','86'),
('105','3-245','75'),
('109','3-245','68'),
('103','3-105','92'),
('105','3-105','88'),
('109','3-105','76'),
('101','3-105','64'),
('107','3-105','91'),
('108','3-105','78'),
('101','6-166','85'),
('107','6-166','79'),
('108','6-166','81');
-- 教师表数据的插入:
INSERT INTO teacher VALUES
('804','李诚','男','1958-12-02','副教授','计算机系'),
('856','张旭','男','1969-03-12','讲师','电子工程系'),
('825','王萍','女','1972-05-05','助教','计算机系'),
('831','刘冰','女','1977-08-14','助教','电子工程系');

下面是30道经典习题

-- ===============题目==================
-- 1. 查询student表中的所有记录sname,ssex,class
SELECT `sname`,`ssex`,`class` FROM student; -- 2. 查询所有教师单位不重复的depart列 -- 第一种方式
SELECT DISTINCT `depart` FROM teacher; -- 第二种方式
SELECT `depart` FROM teacher GROUP BY `depart`; -- 3.查询student中所有记录
SELECT * FROM student; -- 4. 查询score中成绩在60(包含)~80(不包含)的列 -- 第一种方式
SELECT * FROM score WHERE `degree` BETWEEN 60 AND 80; -- 第二种方式
SELECT * FROM score WHERE `degree`>=60 AND `degree`<80; -- 5.查询成绩为85 86 或者88 记录 -- 第一种方式
SELECT * FROM score WHERE `degree`=85 OR `degree`=86 OR `degree`=88; -- 第二种方式 SELECT * FROM score WHERE `degree` IN(85,86,88); -- 6.查询student表中“95031”班或性别为“女”的同学记录。
SELECT * FROM student WHERE class='95031' OR ssex='女'; -- 7. 以class降序查询student表的所有记录。
SELECT * FROM student ORDER BY `class` DESC; -- 8.以cno升序、degree降序查询Score表的所有记录。
SELECT * FROM score ORDER BY `cno` ASC , `degree` DESC; -- 9.查询“95031”班的学生人数。
SELECT COUNT(sno) FROM student WHERE class="95031"; -- 10. 查询Score表中的最高分的sno和cno。(子查询或者排序) -- 子查询方式
SELECT `sno`,`cno` FROM score
WHERE degree =(
SELECT MAX(`degree`) FROM score
); -- 排序
SELECT `sno`,`cno` FROM score
ORDER BY degree DESC
LIMIT 0,1; -- 11.查询每门课的cno、总人数、平均成绩。
SELECT `cno`,COUNT(1) AS '总人数',AVG(`degree`) AS '平均分' FROM score GROUP BY `cno`; -- 12.查询Score表中至少有5名学生选修的并以3开头的课程号的cno、总人数、平均分。
SELECT s.`cno`,COUNT(*) AS '总人数',AVG(`degree`) AS '平均分' FROM score AS s
INNER JOIN course AS c
ON c.`cno` = s.`cno`
WHERE c.`cno` LIKE '3%'
GROUP BY c.`cno`
HAVING COUNT(*)>5; -- 13.查询分数大于70,小于90的sno、degree。 SELECT `sno`,`degree` FROM score WHERE `degree`>70 AND `degree`<90; -- 14.查询所有学生的sname、cno和degree列。(多表查询) -- 第一种方式
SELECT `sname`,`cno`,`degree` FROM student,score
WHERE student.`sno`= score.`sno`; -- 第二种方式:联合查询
SELECT `sname`,`cno`,`degree` FROM student AS s
INNER JOIN score sc
ON s.`sno`=sc.`sno`; -- 15.查询所有学生的sno、cname和degree列(如果学生没有cname 和 degree 也需要查询出来)。 SELECT sc.`sno`,`cname`,`degree` FROM score AS sc
LEFT JOIN course AS c
ON sc.`cno`=c.`cno`; -- 16.查询“95033”班学生的cno,平均分(子查询和多表查询)。 -- 多表查询方式一
SELECT `cno`,AVG(`degree`) AS '平均分' FROM student s
INNER JOIN score sc
ON s.`sno`=sc.`sno`
WHERE `class`='95033'; -- 多表查询方式二
SELECT cno,AVG(`degree`) AS '平均分' FROM score,student
WHERE score.`sno`=student.`sno` AND student.`class`='95033'; -- 子查询
SELECT cno,AVG(`degree`) AS '平均分' FROM score
WHERE sno IN(
SELECT `sno` FROM student WHERE `class`='95033'
); -- 17.查询课程号为“3-105”课程且学号为“109”号同学成绩的记录(子查询和多边查询)。 -- 子查询 SELECT * FROM student
WHERE sno IN(
SELECT `sno` FROM score WHERE `cno`='3-105' AND `sno`=109
); -- 多表查询 -- 方式一
SELECT s.* FROM student AS s
INNER JOIN score AS sc
ON s.`sno`=sc.`sno`
WHERE `cno`='3-105' AND sc.`sno`=109; -- 方式二
SELECT s.* FROM student AS s,score AS sc WHERE s.`sno`=sc.`sno` AND `cno`='3-105' AND sc.`sno`=109; -- 18.查询score中选学2门课程的同学的sno、总成绩(且按照总成绩降序排序)。
SELECT `sno`,SUM(`degree`) AS '总成绩' FROM score
GROUP BY `sno`
HAVING COUNT(`sno`)=2
ORDER BY SUM(`degree`) DESC -- 19.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 -- 子查询
SELECT * FROM score WHERE `degree`>(
SELECT `degree` FROM score WHERE `sno`='109' AND `cno`='3-105'
); -- 20.查询和学号为108的同学同年出生的所有学生的sno、sname和sbirthday列。
SELECT * FROM student WHERE `sbirthday` IN(
SELECT `sbirthday` FROM student WHERE sno = '108'
); -- 21. 获取“张旭"教师所交课程课程号以及课程名 SELECT `cno`,`cname` FROM course AS c
INNER JOIN teacher AS t
ON t.`tno`=c.`tno`
WHERE t.`tname`="张旭"; -- 21.查询“张旭“教师任课的学生成绩(姓名)。 -- 方式一
SELECT `sname`,`degree` FROM student
LEFT JOIN score
ON student.`sno`=score.`sno`
WHERE score.sno IN(
SELECT s.`sno` FROM course AS c
INNER JOIN teacher AS t
ON c.`tno`=t.`tno`
INNER JOIN score AS sc
ON sc.`cno`=c.`cno`
LEFT JOIN student AS s
ON s.`sno`=sc.`sno`
WHERE t.`tname`="张旭"
); -- 方式二
SELECT `sname`,`degree` FROM student
LEFT JOIN score
ON student.`sno`=score.`sno`
WHERE score.sno IN(
SELECT s.sno FROM student AS s,score AS sc
WHERE sc.`sno`=s.`sno` AND cno IN(SELECT cno FROM course AS c,teacher AS t WHERE c.`tno`=t.`tno` AND t.`tname`="张旭")
); -- 22 查询考计算机导论的学生成绩(子查询)
SELECT `degree` FROM score WHERE `cno` IN(
SELECT `cno` FROM course WHERE cname="计算机导论"
); -- 23.教高等数学的老师是叫什么,哪个系的(子查询)
SELECT `tname`,`depart` FROM teacher WHERE `tno` IN(
SELECT `tno` FROM course WHERE `cname`="高等数学"
); -- 24.查询所有教师和同学的name、sex和birthday.(联合查询)
SELECT t.`tname` AS 'name',t.`tsex` AS 'sex',t.`tbirthday` AS 'birthday' FROM teacher AS t
UNION
SELECT s.`sname`,s.`ssex`,s.`sbirthday` FROM student AS s; -- 25.查询选修某课程的同学人数多于5人的教师姓名(子查询)。
SELECT `tname` FROM teacher WHERE tno IN(
SELECT `tno` FROM course WHERE `cno` IN(
SELECT `cno` FROM score GROUP BY `cno` HAVING COUNT(`sno`)>=5
)
); -- 26查询最高分同学的Sno、Cno和Degree列(子查询)。 SELECT `sno`,`cno`,`degree` FROM score WHERE `degree`=(
SELECT MAX(`degree`) FROM score
); -- 27.查询Student表中不姓“王”的同学记录。
SELECT * FROM student WHERE sno NOT IN(
SELECT sno FROM student WHERE `sname` LIKE '王%'
); -- 28.查询Student表中每个学生的姓名和年龄。
SELECT `sname`, FLOOR(DATEDIFF(CURDATE(),`sbirthday`)/365.2422) AS '年龄' FROM student; -- 29.查询所有任课教师的Tname和Depart.
SELECT `tname`,`depart` FROM teacher; -- 30.查询所有成绩比3-105课程平均成绩低的同学的成绩表。
SELECT * FROM score WHERE `degree`<(
SELECT AVG(`degree`) FROM score WHERE cno="3-105"
);
05-11 19:55