create table Student(SId varchar(10),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-12-20','男');
insert into Student values('04','李云','1990-12-06','男');
insert into Student values('05','周梅','1991-12-01','女');
insert into Student values('06','吴兰','1992-01-01','女');
insert into Student values('07','郑竹','1989-01-01','女');
insert into Student values('09','张三','2017-12-20','女');
insert into Student values('10','李四','2017-12-25','女');
insert into Student values('11','李四','2012-06-06','女');
insert into Student values('12','赵六','2013-06-13','女');
insert into Student values('13','孙七','2014-06-01','女');
course table
create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01','语文','02');
insert into Course values('02','数学','01');
insert into Course values('03','英语','03');
teacher table
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01','张三');
insert into Teacher values('02','李四');
insert into Teacher values('03','王五');
scores table
create table Scores(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into Scores values('01','01',80);
insert into Scores values('01','02',90);
insert into Scores values('01','03',99);
insert into Scores values('02','01',70);
insert into Scores values('02','02',60);
insert into Scores values('02','03',80);
insert into Scores values('03','01',80);
insert into Scores values('03','02',80);
insert into Scores values('03','03',80);
insert into Scores values('04','01',50);
insert into Scores values('04','02',30);
insert into Scores values('04','03',20);
insert into Scores values('05','01',76);
insert into Scores values('05','02',87);
insert into Scores values('06','01',31);
insert into Scores values('06','03',34);
insert into Scores values('07','02',89);
insert into Scores values('07','03',98);
Practice questions
1. Query the information and course scores of students with higher course scores in "01 "Curriculum than" 02"
Idea: First filter the information of the course 01, 02, as T1, T2.
SELECT S.SID AS ID , S.SNAME AS NAME ,TEM.CLASS1 AS CLASS1,TEM.CLASS2 AS CLASS2 FROM student AS S RIGHT JOIN
(SELECT T1.SID AS ID,T1.SCORE AS CLASS1,T2.SCORE AS CLASS2 FROM
(SELECT * FROM scores WHERE CID =01)T1,
(SELECT * FROM scores WHERE CID =02)T2
WHERE T1.SID=T2.SID AND T1.SCORE > T2.SCORE
)TEM ON TEM.ID = S.SID
2. Query the situation that "01" course and "02" course exist at the same time
Idea: First filter the information of the course 01, 02, as T1, T2.
将两表sid相同的数据提取出来
SELECT * FROM (
(SELECT * from scores WHERE scores.CId=01) T1,
(SELECT * from scores WHERE scores.CId=02) T2
) WHERE t1.sid =T2.sid
3. Query the situation that "01" course exists but may not exist "02" course (displayed as null if it does not exist)
Idea: null will appear when the join method is used
SELECT * FROM
(SELECT * from scores WHERE scores.CId=01 )T1
LEFT JOIN
(SELECT * from scores WHERE scores.CId=02) T2
ON T1.SID = T2.SID
4. Query the student number and student name and average score of students whose average score is greater than or equal to 60 points
Idea: Use the groupby method, AVG function to average, and then use the join method
SELECT S.SID, S.SNAME,TEM.avg_score FROM
(SELECT SID,AVG(SCORE) as avg_score from scores GROUP BY SId HAVING avg_score >60)TEM
LEFT JOIN student S ON S.SID =TEM.SID
5. Query the information of at least one class that is the same as the student whose student ID is "01"
Idea: Query the course ID of the 01 student report, query all the students who have the first step result of the course IN, query all the student information
select * from student
where student.sid in (
select sc.sid from sc
where sc.cid in(
select sc.cid from sc
where sc.sid = '01'
)
)
6. Query the information of the students who have studied the "Zhang San" teacher
SELECT * from student where student.SId in(
SELECT sid from scores WHERE scores.SId in (
select sid from scores WHERE scores.CId =02)).
7. Query the information of other students who are learning the same courses as the students with number "01"
-- 得到学生信息
SELECT * from student where sid in(
-- 课程和01完全相同的学生
select sid from scores where sid not in
-- 课程不包含在01的课程里面的学生号码
(select sid from scores where sid !=01 and cid not in
-- 01的所有课程号码
(select cid from scores where sid='01'))) a
-- 按照sid分组
group by sid
-- 课程总数和01学生相同的总数筛选
HAVING count(*)=(select count(*) from scores where sid='01')
-- 排除掉01自己
and sid !=01