多表查询sql练习题
1、用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name course grade 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 语文 81 王五 数学 100 王五 英语 90
/*创建表*/
CREATE TABLE stu(
stu_name VARCHAR(255),
Course CHAR(2),
grade INT);
/*插入数据*/
INSERT INTO stu VALUES('张三','语文',81);
INSERT INTO stu VALUES('张三','数学',75);
INSERT INTO stu VALUES('李四','语文',76);
INSERT INTO stu VALUES('李四','数学',90);
INSERT INTO stu VALUES('王五','语文',81);
INSERT INTO stu VALUES('王五','数学',100);
INSERT INTO stu VALUES('王五','英语',90);
/*查询*/
SELECT DISTINCT stu_name FROM stu
WHERE stu_name NOT IN(SELECT stu_name FROM stu WHERE grade<=80);
/*
分析:
先查出成绩小于80的学生姓名,再查出不在成绩小于80的学生姓名中的学生(去除重复记录),即可
*/
2、现有学生表如下: 自动编号 学号 姓名 课程编号 课程名称 分数 1 2005001 张三 0001 数学 69 2 2005002 李四 0001 数学 89 3 2005001 张三 0001 数学 69 删除除了自动编号不同, 其他都相同的学生冗余信息
/*创建表*/
CREATE TABLE stu(
id INT(7) PRIMARY KEY AUTO_INCREMENT,
number INT(7),
stu_name VARCHAR(255),
course_id INT(7),
course_name VARCHAR(255),
score INT(7));
/*插入数据*/
INSERT INTO stu VALUES(NULL,2005001,'张三',0001,'数学',69);
INSERT INTO stu VALUES(NULL,2005002,'李四',0002,'数学',89);
INSERT INTO stu VALUES(NULL,2005001,'张三',0001,'数学',69);
/*删除重复数据*/
DELETE FROM stu
WHERE stu.`id` NOT IN(SELECT `mid` FROM
(SELECT MIN(id) `mid` FROM stu
GROUP BY stu.`stu_name`,stu.`number`,stu.`course_id`,stu.`course_name`,stu.`score`) t);
/*
分析:
先按学号 姓名 课程编号 课程名称 分数分组,
然后查出分组中的最小id(如果满足删除条件则id重复,取最小即可)
得到最小id的集合,然后删除id不在集合中的数据(id不在集合中说明满足删除条件,即数据重复)
*/
3、一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录, 分别是a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合
SELECT t1.`t_name`, t2.`t_name`
FROM team t1,team t2
WHERE t1.`t_name`<t2.`t_name`;
/*
参考资料:https://blog.csdn.net/qq_37958608/article/details/87531029
*/
4、怎么把这样一个数据表 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1991 4 1.4 1992 1 2.1 1992 2 2.2 1992 3 2.3 1992 4 2.4 查成这样一个结果? year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4
/*创建date表*/
CREATE TABLE DATE(
YEAR VARCHAR(20),
MONTH INT(10),
amount VARCHAR(20)
);
/*插入数据*/
INSERT INTO DATE VALUES('1991', 1, '1.1');
INSERT INTO DATE VALUES('1991', 2, '1.2');
INSERT INTO DATE VALUES('1991', 3, '1.3');
INSERT INTO DATE VALUES('1991', 4, '1.4');
INSERT INTO DATE VALUES('1992', 1, '2.1');
INSERT INTO DATE VALUES('1992', 2, '2.2');
INSERT INTO DATE VALUES('1992', 3, '2.3');
INSERT INTO DATE VALUES('1992', 4, '2.4');
/*查询*/
SELECT YEAR,
(SELECT amount FROM `date` d WHERE MONTH=1 AND d.year=date.year) AS m1,
(SELECT amount FROM `date` d WHERE MONTH=2 AND d.year=date.year) AS m2,
(SELECT amount FROM `date` d WHERE MONTH=3 AND d.year=date.year) AS m3,
(SELECT amount FROM `date` d WHERE MONTH=4 AND d.year=date.year) AS m4
FROM DATE GROUP BY YEAR
/*参考资料:https://blog.csdn.net/vivian_python/article/details/102793872*/
5、创建数据表(学生表 Student、教师表 Teacher、成绩表 SC)
/*学生表 Student*/
CREATE TABLE Student(Sid VARCHAR(6), Sname VARCHAR(10), Sage DATETIME, Ssex VARCHAR(10));
INSERT INTO Student VALUES('01' , '赵雷' , '1990-01-01' , '男');
INSERT INTO Student VALUES('02' , '钱电' , '1990-12-21' , '男');
INSERT INTO Student VALUES('03' , '孙风' , '1990-05-20' , '男');
INSERT INTO Student VALUES('04' , '李云' , '1990-08-06' , '男');
INSERT INTO Student VALUES('05' , '周梅' , '1991-12-01' , '女');
INSERT INTO Student VALUES('06' , '吴兰' , '1992-03-01' , '女');
INSERT INTO Student VALUES('07' , '郑竹' , '1989-07-01' , '女');
INSERT INTO Student VALUES('08' , '王菊' , '1990-01-20' , '女')
/*成绩表 SC*/
CREATE TABLE SC(Sid VARCHAR(10), Cid VARCHAR(10), score DECIMAL(18,1));
INSERT INTO SC VALUES('01' , '01' , 80);
INSERT INTO SC VALUES('01' , '02' , 90);
INSERT INTO SC VALUES('01' , '03' , 99);
INSERT INTO SC VALUES('02' , '01' , 70);
INSERT INTO SC VALUES('02' , '02' , 60);
INSERT INTO SC VALUES('02' , '03' , 80);
INSERT INTO SC VALUES('03' , '01' , 80);
INSERT INTO SC VALUES('03' , '02' , 80);
INSERT INTO SC VALUES('03' , '03' , 80);
INSERT INTO SC VALUES('04' , '01' , 50);
INSERT INTO SC VALUES('04' , '02' , 30);
INSERT INTO SC VALUES('04' , '03' , 20);
INSERT INTO SC VALUES('05' , '01' , 76);
INSERT INTO SC VALUES('05' , '02' , 87);
INSERT INTO SC VALUES('06' , '01' , 31);
INSERT INTO SC VALUES('06' , '03' , 34);
INSERT INTO SC VALUES('07' , '02' , 89);
INSERT INTO SC VALUES('07' , '03' , 98)
/*教师表 Teacher*/
CREATE TABLE Teacher(Tid VARCHAR(10),Tname VARCHAR(10));
INSERT INTO Teacher VALUES('01' , '张三');
INSERT INTO Teacher VALUES('02' , '李四');
INSERT INTO Teacher VALUES('03' , '王五')
需求1:查询” 01 “课程比” 02 “课程成绩高的学生的信息及课程分数
SELECT s.*,sc.`score` FROM student s
NATURAL JOIN sc
WHERE s.sid IN
(SELECT t1.sid FROM
(SELECT * FROM sc WHERE cid=01) t1,
(SELECT * FROM sc WHERE cid=02) t2
WHERE t1.sid=t2.sid AND t2.score>t1.score);
/*参考资料:https://www.cnblogs.com/ql70me/p/10329630.html*/
需求2:查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
SELECT student.sid,student.sname,a.avg_score FROM student
INNER JOIN(SELECT Sid,AVG(score) avg_score FROM sc
GROUP BY sid
HAVING AVG(score )>=60) a
ON student.sid=a.sid
/*
分析:
先按sid分组查询出sid和平均成绩avg_score得到新表a(注意:必须取别名),
然后将学生表和a表内连接查询出sid相等的学生即可
*/
需求3:查询在 SC 表存在成绩的学生信息
SELECT * FROM student
WHERE sid IN(SELECT sid FROM sc GROUP BY sid) t;
需求4:查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
SELECT s.sid,s.sname,a.cid_count FROM student s LEFT OUTER JOIN (SELECT sid ,COUNT(cid) cid_count FROM sc GROUP BY sid) a ON s.`Sid`=a.sid;
需求5:查有成绩的学生信息
SELECT * FROM student WHERE sid IN (SELECT sid FROM sc GROUP BY sid);