mysql学⽣表经典案例50题
⼀、建库、建表
create database alibaba;
use alibaba;
– 建表
create table student(s_id varchar(10),s_name varchar(10),s_age date,s_sex varchar(10)) engine=InnoDB default charset utf8;
insert into student(s_id,s_name,s_age,s_sex)
values('01' , '赵雷', '1990-01-01' , '男'),
('02' , '钱电' , '1990-12-21' , '男')
,('03' , '孙风' , '1990-05-20' , '男')
,('04' , '李云' , '1990-08-06' , '男')
,('05' , '周梅' , '1991-12-01' , '⼥')
,('06' , '吴兰' , '1992-03-01' , '⼥')
,('07' , '郑⽵' , '1989-07-01' , '⼥')
,('08' , '王菊' , '1990-01-20' , '⼥');
select * from student;
create table course(c_id varchar(10),c_name varchar(10),t_id varchar(10)) engine=InnoDB default charset utf8;
insert into course values('01' , '语⽂' , '02'),
('02' , '数学' , '01'),
('03' , '英语' , '03');
select * from course;
create table teacher(t_id varchar(10),t_name varchar(10)) engine=InnoDB default charset utf8;
insert into teacher values('01' , '张三'),('02' , '李四'),('03' , '王五');
select * from teacher;
create table stu_sco(s_id varchar(10),c_id varchar(10),score decimal(18,1)) engine=InnoDB default charset utf8;
insert into stu_sco values ('01' , '01' , 80), ('01' , '02' , 90), ('01' , '03' , 99), ('02' , '01' , 70), ('02' , '02' , 60), ('02' , '03' , 80), ('03' , '01' , 80), ('03' , '02' , 80), ('03' , '03' , 80), ('04' , '01' , 50), ('04' , '02' , 30), ('04' , '03' , 20), ('05' , '01' , 76), ('05' , '02' , 87), ('06' , '01' , 31), ('06' , '03' , 34), ('07' , '02' , 89), ('07' , '03' , 98);
select * from stu_sco;
⼆、完成如下查询语句
1、查询"01"课程⽐"02"课程成绩⾼的学⽣信息及课程分数
select a.*,b.score as sc1,c.score as sc2 from student a join stu_sco b on a.s_id =b.s_id and b.c_id =01 join stu_sco c on a.s_id=c.s_id and c.c_id =02 where b.score>c.score;
2、查询"01"课程⽐"02"课程成绩低的学⽣的信息及课程分数
select a.*,b.score as sc1,c.score as sc2 from student a join stu_sco b on a.s_id =b.s_id and b.c_id =01 join stu_sco c on a.s_id=c.s_id and c.c_id =02 where b.score<c.score;
3、查询平均成绩⼤于等于60分的同学的学⽣编号和学⽣姓名和平均成绩
SELECT
student.s_id 学⽣编号,
student.s_name 学⽣姓名,
avg(score) 平均成绩
FROM
student
JOIN stu_sco ON student.s_id = stu_sco.s_id
GROUP BY
student.s_id,
student.s_name
HAVING
平均成绩 >= 60;
student.s_id,
student.s_name,
avg_score
FROM
student
INNER JOIN (
SELECT
s_id,
avg(score) avg_score
FROM
stu_sco
GROUP BY
vbs爱心特效s_id
) b ON student.s_id = b.s_id
AND b.avg_score >= 60;
4、查询平均成绩⼩于60分的同学的学⽣编号和学⽣姓名和平均成绩
SELECT
student.s_id,
student.s_name,
avg_score
FROM
student
INNER JOIN (
SELECT
s_id,
avg(score) avg_score
FROM
stu_sco
mysql面试题库GROUP BY
s_id
) b ON student.s_id = b.s_id
AND b.avg_score <60;
SELECT
student.s_id 学⽣编号,
student.s_name 学⽣姓名,
avg(score) 平均成绩
sql语句常用面试FROM
student
JOIN stu_sco ON student.s_id = stu_sco.s_id
GROUP BY
student.s_id,
student.s_name
HAVING
平均成绩 <60;
5、查询所有同学的学⽣编号、学⽣姓名、选课总数、所有课程的总成绩
SELECT
student.s_id 学⽣编号,
student.s_name 学⽣姓名,
count(c_id) 选课总数,
sum(score) 总成绩
FROM
studenttcpip协议的含义是局域网传输协议
JOIN stu_sco ON student.s_id = stu_sco.s_id
GROUP BY student.s_id;
6、查询"李"姓⽼师的数量
count(*) 李姓⽼师的数量
FROM
teacher
WHERE t_name LIKE '李%';
7、查询学过"张三"⽼师授课的同学的信息
SELECT *
FROM
student
WHERE
s_id IN (
SELECT
s_id
FROM
stu_sco
WHERE
c_id IN (
SELECT
c_id
FROM
course
WHERE
c_id IN (
SELECT
t_id
FROM
teacher
WHERE
t_name = '张三' ) ) );
8、查询没学过"张三"⽼师授课的同学的信息
SELECT *
FROM
student
WHERE
s_id NOT IN (
SELECT
s_id
FROM
stu_sco
WHERE
c_id IN (
SELECT
c_id
FROM
course
WHERE
c_id IN (
SELECT
t_id
FROM
teacher
WHERE
t_name = '张三'
)
)
);
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
*
FROM
student
WHERE
s_id IN (
vba编辑教程
SELECT
a.s_id
FROM
(
SELECT
*
FROM
stu_sco
WHEREselect下拉菜单
c_id = '01'
) a
JOIN (
SELECT
*
FROM
stu_sco
WHERE
c_id = '02'
) b ON a.s_id = b.s_id
);
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT
student.*
FROM
student,
stu_sco sc1
WHERE
student.s_id = sc1.s_id
AND sc1.c_id = '01'
AND student.s_id NOT IN (
SELECT
s_id
FROM
stu_sco sc2
WHERE
sc2.c_id = '02'
);
11、查询没有学全所有课程的学⽣信息
SELECT
student.*
FROM
student
JOIN stu_sco ON student.s_id = stu_sco.s_id
GROUP BY
student.s_id
HAVING
count(stu_sco.c_id) < (
SELECT
count(DISTINCT c_id)
FROM
course
);
12、查询⾄少有⼀门课与学号为"01"的同学所学相同的同学的信息
student.*
FROM
student
WHERE
s_id IN (
SELECT DISTINCT
(s_id)
FROM
stu_sco
WHERE
c_id IN (
SELECT
c_id
FROM
stu_sco
WHERE
s_id = '01'
)
AND s_id <> '01'
);
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT
student.*
FROM
student
WHERE
s_id IN (
SELECT
s_id
FROM
stu_sco
WHERE
s_id <> '01'
GROUP BY
s_id
HAVING
count(1) = (
SELECT
count(c_id)
FROM
stu_sco
WHERE
s_id = '01'
)
);
14、查询没学过"张三"⽼师讲授的任⼀门课程的学⽣姓名

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。