5月8号
-- 1、查询所有的课程的名称以及对应的任课老师姓名
SELECT cname, tname from course join teacher on course.teacher_id = teacher.tid;
-- 2、查询学生表中男女生各有多少人
SELECT gender, COUNT(sid) from student GROUP BY gender;
-- 3、查询物理成绩等于100的学生的姓名
SELECT
sname
FROM
student
WHERE
sid IN (
SELECT
student_id
FROM
score
WHERE
course_id IN ( SELECT cid FROM course WHERE cname = '物理' )
AND num = 100);
-- 4、查询平均成绩大于八十分的同学的姓名和平均成绩
select student.sname, avg(num) from student join score on student.sid = score.student_id
GROUP BY score.student_id
HAVING avg(num) > 80;
-- 5、查询所有学生的学号,姓名,选课数,总成绩
SELECT
student.sid,
student.sname,
t1.course_num,
t1.total_num
FROM
student
LEFT JOIN (
SELECT
student_id,
COUNT(course_id) course_num,
sum(num) total_num
FROM
score
GROUP BY
student_id
) AS t1 ON student.sid = t1.student_id;
-- 6、 查询姓李老师的个数
SELECT count(tid) FROM teacher WHERE tname LIKE '李%';
-- 7、 查询没有报李平老师课的学生姓名
SELECT
sname
FROM
student
WHERE
sid NOT IN (
SELECT
student_id
FROM
score
WHERE
course_id IN (
SELECT
cid
FROM
course
WHERE
teacher_id IN ( SELECT tid FROM teacher WHERE tname = '李平老师' )));
-- 8、 查询物理课程比生物课程高的学生的学号
SELECT
t1.student_id
FROM
(
SELECT
student_id,
num
FROM
score
WHERE
course_id = ( SELECT cid FROM course WHERE cname = '物理' )) AS t1
JOIN (
SELECT
student_id,
num
FROM
score
WHERE
course_id = ( SELECT cid FROM course WHERE cname = '生物' )) AS t2 ON t1.student_id = t2.student_id
WHERE
t1.num > t2.num;
-- 9、 查询没有同时选修物理课程和体育课程的学生姓名
SELECT
student.sname
FROM
student
WHERE
sid IN (
SELECT
student_id
FROM
score
WHERE
course_id IN ( SELECT cid FROM course WHERE cname = '物理' OR cname = '体育' )
GROUP BY
student_id
HAVING
COUNT( course_id ) = 1
);
-- 10、查询挂科超过两门(包括两门)的学生姓名和班级
SELECT
sname,
caption
FROM
student
JOIN class ON student.class_id = class.cid
WHERE
sid IN (
SELECT
student_id
FROM
score
WHERE
num < 60 GROUP BY student_id HAVING COUNT( student_id ) >= 2);
-- 11、查询选修了所有课程的学生姓名
SELECT
student.sname
FROM
student
WHERE
sid IN (
SELECT
student_id
FROM
score
GROUP BY
student_id
HAVING
COUNT(course_id) = (SELECT count(cid) FROM course)
);
-- 12、查询李平老师教的课程的所有成绩记录
SELECT
*
FROM
score
WHERE
course_id IN (
SELECT
cid
FROM
teacher
JOIN course ON course.teacher_id = teacher.tid
WHERE
tname = '李平老师');
-- 13、查询全部学生都选修了的课程号和课程名
SELECT
cid,
cname
FROM
course
WHERE
cid IN (
SELECT
course_id
FROM
score
GROUP BY
course_id
HAVING
count( student_id ) = ( SELECT count( sid ) FROM student ));
-- 14、查询每门课程被选修的次数
SELECT
course_id,
count( student_id )
FROM
score
GROUP BY
course_id;
-- 15、查询之选修了一门课程的学生姓名和学号
SELECT
sid,
sname
FROM
student
WHERE
sid IN (
SELECT
student_id
FROM
score
GROUP BY
student_id
HAVING
COUNT(course_id) = 1
);
-- 16、查询所有学生考出的成绩并按从高到低排序(成绩去重)
select DISTINCT num from score ORDER BY num DESC;
-- 17、查询平均成绩大于85的学生姓名和平均成绩
SELECT
sname,
avg( num )
FROM
student
JOIN score ON student.sid = score.student_id
GROUP BY
student_id
HAVING
avg( num ) > 85;
-- 18、查询生物成绩不及格的学生姓名和对应生物分数
SELECT
sname as '姓名',
num as '生物成绩'
FROM
score
JOIN course ON score.course_id = course.cid
JOIN student ON score.student_id = student.sid
WHERE course.cname = '生物' AND score.num < 60;
-- 19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名
SELECT
sname
FROM
student
WHERE
sid = (
SELECT
student_id
FROM
score
WHERE
course_id IN ( SELECT cid FROM course JOIN teacher ON teacher.tid = course.teacher_id WHERE tname = '李平老师' )
GROUP BY
student_id
ORDER BY
avg( num ) DESC
LIMIT 1);
-- 20、查询每门课程成绩最好的前两名学生姓名
select student.sid, student.sname, t4.course_id, t4.first_num, t4.second_num from student join
(SELECT
score.student_id as id,
t3.course_id,
t3.first_num,
t3.second_num
FROM
score
INNER JOIN (
SELECT
t1.course_id,
t1.first_num,
t2.second_num
FROM
(
SELECT
course_id,
max(num) first_num
FROM
score
GROUP BY
course_id
) AS t1
INNER JOIN (
SELECT
score.course_id,
max(num) second_num
FROM
score
INNER JOIN (
SELECT
course_id,
max(num) first_num
FROM
score
GROUP BY
course_id
) AS t ON score.course_id = t.course_id
WHERE
score.num < t.first_num
GROUP BY
course_id
) AS t2 ON t1.course_id = t2.course_id
) AS t3 ON score.course_id = t3.course_id
WHERE
score.num >= t3.second_num
AND score.num <= t3.first_num) as t4 on student.sid = t4.id;
-- 21、查询不同课程但成绩相同的学生的学号,课程号,成绩
select score.student_id, score.course_id, score.num, t.course_id, t.num from score join (select student_id, course_id, num from score) as t on t.student_id = score.student_id
WHERE t.num = score.num and t.course_id != score.course_id;
-- 22、查询没学过李平老师课程的学生姓名以及选修的课程名称;
select t.sname, course.cname from
(SELECT
sid, sname
FROM
student
WHERE
sid NOT IN (
SELECT
student_id
FROM
score
WHERE
course_id IN (
SELECT
cid
FROM
course
WHERE
teacher_id IN ( SELECT tid FROM teacher WHERE tname = '李平老师' )))) AS t
LEFT JOIN score ON t.sid = score.student_id
LEFT JOIN course ON course.cid = score.course_id
-- 23、查询所有选修了学号为1的同学选修过的一门或者多门课程的同学学号和姓名;
SELECT
student_id,
sname
FROM
score
JOIN student ON score.student_id = student.sid
WHERE
student_id != 1
AND course_id IN ( SELECT course_id FROM score WHERE student_id = 1 )
GROUP BY
student_id;
-- 24、任课最多的老师中学生单科成绩最高的学生姓名
SELECT
sname
FROM
student
WHERE
sid IN (
SELECT
student_id
FROM
score
JOIN (
SELECT
course_id,
max( num ) AS n
FROM
(
SELECT
student_id,
course_id,
num
FROM
score
WHERE
course_id IN (
SELECT
cid
FROM
course
WHERE
teacher_id = ( SELECT max( s ) AS id FROM ( SELECT count( teacher_id ) AS s, teacher_id FROM course GROUP BY teacher_id ) AS t ))) AS t
GROUP BY
t.course_id
) AS t1 ON t1.course_id = score.course_id
WHERE
score.num = t1.n)
5月6号
完成下列分组查询练习题
1. 查询岗位名以及岗位包含的所有员工名字
select post, group_concat(name) from empp group by post;
2. 查询岗位名以及各岗位内包含的员工个数
select post, count(name) from empp group by post;
3. 查询公司内男员工和女员工的个数
select sex, count(name) as '人数' from empp group by sex;
4. 查询岗位名以及各岗位的平均薪资
select post, avg(salary) from empp GROUP BY post;
5. 查询岗位名以及各岗位的最高薪资
select post, max(salary) from empp GROUP BY post;
6. 查询岗位名以及各岗位的最低薪资
select post, min(salary) from empp GROUP BY post;
7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
select sex, avg(salary) from empp GROUP BY sex;
5月5号
练习:账号信息表,用户组,主机表,主机组
#用户表
create table user(
id int not null unique auto_increment,
username varchar(20) not null,
password varchar(50) not null,
primary key(username,password)
);
#用户组表
create table usergroup(
id int primary key auto_increment,
groupname varchar(20) not null unique
);
#主机表
create table host(
id int primary key auto_increment,
ip char(15) not null unique default '127.0.0.1'
);
#业务线表
create table business(
id int primary key auto_increment,
business varchar(20) not null unique
);
#建关系:user与usergroup
create table user2usergroup(
id int not null unique auto_increment,
user_id int not null,
group_id int not null,
primary key(user_id,group_id),
foreign key(user_id) references user(id),
foreign key(group_id) references usergroup(id)
);
#建关系:host与business
create table host2business(
id int not null unique auto_increment,
host_id int not null,
business_id int not null,
primary key(host_id,business_id),
foreign key(host_id) references host(id),
foreign key(business_id) references business(id)
);
#建关系:user与host
create table user2host(
id int not null unique auto_increment,
user_id int not null,
host_id int not null,
primary key(user_id,host_id),
foreign key(user_id) references user(id),
foreign key(host_id) references host(id)
);
练习:
# 班级表
cid caption
create table class(
cid int PRIMARY KEY auto_increment,
caption VARCHAR(50) not null
);
# 学生表
sid sname gender class_id
create table student(
sid int PRIMARY key auto_increment,
sname varchar(16) not null,
gender enum('male','female') default 'male',
class_id int,
foreign key(class_id) references class(cid) on update cascade on delete cascade
);
# 老师表
tid tname
create table teacher(
tid int primary key,
tname varchar(16) not null
);
# 课程表
cid cname teacher_id
create table course(
cid int primary key auto_increment,
cname varchar(16) not null,
teacher_id int,
foreign key(teacher_id) references teacher(tid) on update cascade on delete cascade
);
# 成绩表
sid student_id course_id number
create table score(
sid int not null unique auto_increment,
student_id int,
course_id int,
number int not null,
primary key(student_id, course_id),
foreign key(student_id) references student(sid),
foreign key(course_id) references course(cid)
);