单元测试|sql练习题

单元测试|sql练习题
文章图片

CREATE DATABASE day09;
CREATE TABLE celebrity(
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(255),
sage VARCHAR(255),
ssex VARCHAR(255)
)
INSERT INTO celebrity VALUES
(NULL,‘李白’,‘34’,‘男’),
(NULL,‘杜甫’,‘24’,‘男’),
(NULL,‘白居易’,‘31’,‘男’),
(NULL,‘李商隐’,‘40’,‘女’),
(NULL,‘苏轼’,‘26’,‘男’),
(NULL,‘辛弃疾’,‘22’,‘男’);
CREATE TABLE works(
wid INT PRIMARY KEY AUTO_INCREMENT,
wwork VARCHAR(255),
wdynasty VARCHAR(255)
);
DELETE FROM celebrity
INSERT INTO works VALUES
(NULL,‘将进酒’,‘唐代’),
(NULL,‘蜀道难’,‘唐代’),
(NULL,‘夜雨寄北’,‘唐代’),
(NULL,‘静夜思’,‘唐代’),
(NULL,‘望岳’,‘唐代’),
(NULL,‘钱塘湖春行’,‘南宋’),
(NULL,‘念奴娇赤壁怀古’,‘北宋’),
(NULL,‘水调歌头’,‘唐代’);
【单元测试|sql练习题】CREATE TABLE summary(
sid INT,
wid INT,
sassess VARCHAR(255)
)
INSERT INTO summary VALUES
(1,1,‘天生我材必有用’),
(1,2,‘危乎高哉’),
(6,8,‘明月几时有’),
(3,6,‘乱花渐欲迷人眼’),
(4,3,‘却话巴山夜雨时’),
(5,7,‘大江东去’),
(2,5,‘一览众山小’),
(1,4,‘举头望明月’);
– 查询名人表中男的作者
SELECT * FROM celebrity WHERE ssex=‘男’;
– 查询静夜思的作者
SELECT celebrity.sname FROM celebrity INNER JOIN works ON celebrity.sid=works.wid WHERE works.wwork=‘静夜思’;
– 查询李白和杜甫的年纪
SELECT sname,sage FROM celebrity WHERE sname IN(‘李白’,‘杜甫’);
– 查询名人表中的男女人数
SELECT ssex,COUNT(*) FROM celebrity GROUP BY ssex;
– 查询白居易写的作品名称和对应的名句
SELECT 表1.字段,表2.字段,表3.字段 FROM 表1 INNER JOIN 表2 ON 表1.字段=表2.字段 INNER JOIN 表3 ON 表1.字段=表3.字段 WHERE 条件
SELECT c.sname,w.wwork,s.sassess FROM celebrity c INNER JOIN summary s ON c.sid=s.sid INNER JOIN works w ON w.wid=s.sid WHERE c.sname=‘白居易’;
– 查询年纪在25到30岁之间的个数
SELECT COUNT(*) FROM celebrity WHERE sage BETWEEN 25 AND 30;
– 查询名人表中年纪最小的两条数据信息
SELECT * FROM celebrity ORDER BY sage LIMIT 0,2;
– 查询李白的作品名称,名句和年纪
SELECT c.sname,w.wwork,s.sassess,c.sage FROM celebrity c INNER JOIN summary s ON c.sid=s.sid INNER JOIN works w ON w.wid=s.wid WHERE c.sname=‘李白’;
– 将作品为望岳的朝代修改为北宋
UPDATE works SET wdynasty=‘北宋’ WHERE wwork=‘望岳’;
– 新增名人表中的一个作者王维,年纪25,性别男
INSERT INTO celebrity VALUES(NULL,‘王维’,25,‘男’)
– 创建学生信息表
CREATE TABLE student(
sno INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(255),
age INT,
sex VARCHAR(255)
);
– 创建课程信息表
CREATE TABLE course(
cno INT PRIMARY KEY AUTO_INCREMENT,
cname VARCHAR(255),
cteacher VARCHAR(255)
);
– 创建选课信息表
CREATE TABLE studentcourse(
sno INT,
cno INT,
scgrade INT
);
– 向学生表中添加数据
INSERT INTO student VALUES(NULL,‘张三’,18,‘男’),
(NULL,‘李四’,20,‘女’),
(NULL,‘王五’,23,‘男’),
(NULL,‘赵六’,22,‘女’);
INSERT INTO course VALUES(NULL,‘java’,‘何昊’),
(NULL,‘php’,‘李美军’),
(NULL,‘android’,‘王超’);
INSERT INTO studentcourse VALUES(1,1,50),
(2,1,66),
(3,1,90),
(1,2,55),
(2,2,68),
(3,2,61),
(4,2,44),
(1,3,90),
(2,3,78),
(3,3,22),
(4,3,55);
– 查询对应何昊老师课程的女学生的信息
SELECT s.* FROM student s INNER JOIN studentcourse sc ON s.sno=sc.sno INNER JOIN course c ON c.cno=sc.cno WHERE c.cteacher=‘何昊’ AND s.sex=‘女’;
– 找出没有选修过何老师课程的所有学生的姓名
SELECT DISTINCT s.sname FROM student s INNER JOIN studentcourse sc ON s.sno=sc.sno INNER JOIN course c ON c.cno=sc.cno WHERE sc.sno NOT IN(SELECT sno FROM studentcourse WHERE cno=1);
– 列出所有成绩小于60的学生的姓名
SELECT DISTINCT s.sname FROM student s INNER JOIN studentcourse sc ON s.sno=sc.sno WHERE sc.scgrade<60;
– 创建学生表和老师表
CREATE TABLE student3(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(255),
score INT
);
CREATE TABLE teacher3(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(255),
class VARCHAR(255),
classroom VARCHAR(255),
student_id INT
);
– 插入表数据
INSERT INTO student3 VALUES(NULL,‘张三’,80),
(NULL,‘李四’,56),
(NULL,‘王五’,72),
(NULL,‘赵六’,30),
(NULL,‘刘七’,66);
INSERT INTO teacher3 VALUES(NULL,‘刘良誉’,‘测试’,‘2102A’,1),
(NULL,‘刘良誉’,‘测试’,‘2011A’,2),
(NULL,‘樊庆晓’,‘android’,‘2012A’,3),
(NULL,‘孙文龙’,‘PHP’,‘2101A’,4),
(NULL,‘许腾升’,‘JAVA’,‘2013B’,5);
– 查询刘良誉老师带的学生的姓名和对应学科
SELECT s.name,t.class FROM student3 s INNER JOIN teacher3 t ON s.id=t.student_id WHERE t.name=‘刘良誉’;
– 查询刘良誉老师带的学生,成绩及格的姓名
SELECT s.name,s.score FROM student3 s INNER JOIN teacher3 t ON s.id=t.student_id WHERE t.name=‘刘良誉’ AND s.score>=60;
– 查询赵六学生的老师姓名和对应学科的名称
SELECT s.name 学生,t.name 老师,t.class 学科 FROM student3 s INNER JOIN teacher3 t ON s.id=t.student_id WHERE s.name=‘赵六’;
![请添加图片描述](https://img-blog.csdnimg.cn/2f9fe5819ce44802afc5002f597cb868.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Yav5pmo6Iq4,size_20,color_FFFFFF,t_70,g_se,x_16单元测试|sql练习题
文章图片

单元测试|sql练习题
文章图片

单元测试|sql练习题
文章图片

单元测试|sql练习题
文章图片

单元测试|sql练习题
文章图片

    推荐阅读