根据上图创建 数据库 & 表结构 并 录入数据(可以自行创造数据)
create database userdb default charset utf8 collate utf8_general_ci;
create table class(
cid int not null auto_increment primary key,
caption varchar(16) not null
)default charset=utf8;
create table student(
sid int not null auto_increment primary key,
sname varchar(16) not null,
gender char(1) check(gender='男' or gender='女'),
class_id int,
constraint fk_student_class foreign key (class_id) references class(cid)
)default charset=utf8;
create table teacher(
tid int not null auto_increment primary key,
tname varchar(16) not null
)default charset=utf8;
create table course(
cid int not null auto_increment primary key,
cname varchar(16) not null,
teacher_id int,
constraint fk_course_teacher foreign key (teacher_id) references teacher(tid)
)default charset=utf8;
create table score(
sid int not null auto_increment primary key,
student_id int,
course_id int,
num int,
constraint fk_score_student foreign key score(student_id) references student(sid),
constraint fk_score_course foreign key score(course_id) references course(cid)
)default charset=utf8;
insert into class(caption) values("三年二班"),("一年三班"),("三年一班");
insert into student(sname,gender,class_id) values("钢蛋","女",1),("铁锤","女",1),("山炮","男",2);
insert into teacher(tname) values("波多"),("苍空"),("饭岛");
insert into course(cname,teacher_id) values("生物",1),("体育",1),("物理",2);
insert into score(student_id,course_id,num) values(1,1,60),(1,2,59),(2,2,100);
1.创建用户 luffy 并赋予此数据库的所有权限
create user 'luffy'@'%' identified by 'root123';
grant all privileges on userdb.* TO 'luffy'@'%';
flush privileges;
终端登录
mysql -u luffy -p
# 进入数据库
use userdb;
2.查询姓“李”的老师的个数。
select count(tid) from teacher where tname like "李%";
3.查询姓“张”的学生名单。
select sname from student where sname like "张%";
4.查询男生、女生的人数。
select gender AS 性别, COUNT(*) as 总人数 from student GROUP BY gender;
5.查询同名同姓学生名单,并统计同名人数。
select sname,COUNT(*) as 人数 from student GROUP BY sname having count(*)>1;
6.查询 “三年二班” 的所有学生。
select sname
from student
left outer join class
on student.class_id = class.cid
where class.caption=“三年二班”;
7.查询 每个 班级的 班级名称、班级人数。
select class.caption,count(student.sid) as 人数 from class,student where student.class_id = class.cid GROUP BY student.class_id ;
Select
c.caption,count(s.sid) as 人数
from class c
LEFT JOIN student s
on c.cid = s.class_id
group by c.caption;
8.查询成绩小于60分的同学的学号、姓名、成绩、课程名称。
select
student.sid,
student.sname,
score.num,
course.cname
from
score
left join student on score.student_id=student.sid
left join course on score.course_id =course.cid
where num <60;
SELECT b.sid as 学号, b.sname as 姓名,f.cname as 课程名称,f.num as 成绩 FROM (
SELECT s.num ,c.cname ,s.student_id
FROM score s LEFT JOIN course c ON c.cid = s.course_id where s.num<60 )
f,student b WHERE b.sid = f.student_id ;
9.查询选修了 “生物课” 的所有学生ID、学生姓名、成绩。
SELECT b.sid as 学生ID, b.sname as 姓名,f.cname as 课程名称,f.num as 成绩 FROM (
SELECT s.num ,c.cname ,s.student_id
FROM score s LEFT JOIN course c ON c.cid = s.course_id where c.cname="生物" )
f,student b WHERE b.sid = f.student_id ;
select
student.sid,
student.sname,
score.num
from
score
left join course on score.course_id =course.cid
left join student on score.student_id=student.sid
where course.cname="生物";
10.查询选修了 “生物课” 且分数低于60的的所有学生ID、学生姓名、成绩。
SELECT b.sid as 学生ID, b.sname as 姓名,f.cname as 课程名称,f.num as 成绩
FROM (
SELECT s.num ,c.cname ,s.student_id
FROM score s LEFT JOIN course c
ON c.cid = s.course_id
where c.cname="生物" and s.num<60
)f,student b
WHERE
b.sid = f.student_id ;
select
student.sid,student.sname,score.num
from
score
left join course on score.course_id =course.cid
left join student on score.student_id=student.sid
where course.cname="生物" and score.num < 60;
11.查询所有同学的学号、姓名、选课数、总成绩。
SELECT
stu.sid AS '学号',
stu.sname AS '姓名',
count( sc.course_id ) AS '选课数',
sum( sc.num ) AS '总成绩'
FROM
student AS stu
INNER JOIN score AS sc
ON ( sc.student_id = stu.sid )
GROUP BY stu.sid
12.查询各科被选修的学生数。
SELECT c.cname ,count(c.cname) as 上课人数
FROM course c
LEFT JOIN score s
ON c.cid = s.course_id
group by c.cname;
select student_id,student.sname,count(1),sum(num) from score left join student on score.student_id=student.sid group by student_id;
13.查询各科成绩的总分、最高分、最低分,显示:课程ID、课程名称、总分、最高分、最低分。
SELECT
c.cid as 课程ID,
c.cname as 课程名称,
sum(s.num) as 总分,
max(s.num) as 最高分,
min(s.num) as 最低分
FROM score s
LEFT JOIN course c
ON c.cid = s.course_id
group by s.course_id;
14.查询各科成绩的平均分,显示:课程ID、课程名称、平均分。
SELECT
c.cid as 课程ID,
c.cname as 课程名称,
avg(s.num) as 平均分
FROM score s
LEFT JOIN course c
ON c.cid = s.course_id
group by s.course_id;
15.查询各科成绩的平均分,显示:课程ID、课程名称、平均分(按平均分从大到小排序)。
SELECT
c.cid as 课程ID,
c.cname as 课程名称,
avg(s.num) as 平均分
FROM score s
LEFT JOIN course c
ON c.cid = s.course_id
group by s.course_id
order by avg(s.num) desc;
也可以取别名
select course_id,course.cname,avg(num) as A from score left join course on score.course_id =course.cid group by course_id order by A desc;
16.查询各科成绩的平均分和及格率,显示:课程ID、课程名称、平均分、及格率。
select
course_id,
course.cname,
avg(num),
sum(case when score.num >= 60 then 1 else 0 end)/count(1) *100 as percent
from
score
left join course on score.course_id =course.cid
group by
course_id;
SELECT
c.cid as 课程ID,
c.cname as 课程名称,
avg(s.num) as 平均分,
round(100 * SUM( CASE WHEN ifnull( s.num, 0 ) >= 60 THEN 1 ELSE 0 END ) / COUNT( * ),2) as 及格率
FROM score s
LEFT JOIN course c
ON c.cid = s.course_id
group by s.course_id;
17.查询平均成绩大于60的所有学生的学号、平均成绩
SELECT
stu.sid AS '学号',
stu.sname AS '姓名',
count( sc.course_id ) AS '选课数',
avg( sc.num ) AS '平均成绩'
FROM
student AS stu
INNER JOIN score AS sc
ON ( sc.student_id = stu.sid )
GROUP BY stu.sid
having avg( sc.num )>60;
select student_id,avg(num) from score group by student_id having avg(num) > 60;
18.查询平均成绩大于85的所有学生的学号、平均成绩、姓名。
select student_id,avg(num),student.sname from score left join student on score.student_id=student.sid group by student_id having avg(num) > 85;
SELECT
stu.sid AS '学号',
stu.sname AS '姓名',
avg( sc.num ) AS '平均成绩'
FROM
student AS stu
INNER JOIN score AS sc
ON ( sc.student_id = stu.sid )
GROUP BY stu.sid
having avg( sc.num )>85;
19.查询 “三年二班” 每个学生的 学号、姓名、总成绩、平均成绩。
SELECT
student_id,
sname,
sum( num ),
avg( num )
FROM
score
LEFT JOIN student ON score.student_id = student.sid
LEFT JOIN class ON class.cid = student.class_id
WHERE
class.caption = "三年二班"
GROUP BY
student_id;
SELECT
f.学号, f.姓名,b.caption as '班级名称', f.总成绩, f.平均成绩
FROM (
SELECT
stu.sid AS '学号',
stu.sname AS '姓名',
sum( sc.num ) AS '总成绩',
avg( sc.num ) AS '平均成绩',
stu.class_id
FROM
student AS stu
INNER JOIN score AS sc
ON ( sc.student_id = stu.sid )
GROUP BY stu.sid
)f,class AS b
WHERE
b.caption="三年二班" ;