sql 50道

#–1.学生表
#Student(s_id,s_name,s_brith,s_sex) –学生编号,学生姓名, 出生年月,学生性别
CREATE TABLE Student (
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL DEFAULT ‘’,
s_brith VARCHAR(20) NOT NULL DEFAULT ‘’,
s_sex VARCHAR(10) NOT NULL DEFAULT ‘’,
PRIMARY KEY(s_id)
);

#–2.课程表
#Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
create table Course(
c_id varchar(20),
c_name VARCHAR(20) not null DEFAULT ‘’,
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id)
);

/*
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
*/
CREATE TABLE Teacher(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL DEFAULT ‘’,
PRIMARY KEY(t_id)
);

/*
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数
*/
Create table Score(
s_id VARCHAR(20),
c_id VARCHAR(20) not null default ‘’,
s_score INT(3),
primary key(s_id,c_id)
);
#–插入学生表测试数据
#(‘01’ , ‘赵雷’ , ‘1990-01-01’ , ‘男’)
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’ , ‘女’);
#–课程表测试数据
insert into Course values(‘01’ , ‘语文’ , ‘02’);
insert into Course values(‘02’ , ‘数学’ , ‘01’);
insert into Course values(‘03’ , ‘英语’ , ‘03’);
#–教师表测试数据
insert into Teacher values(‘01’ , ‘张三’);
insert into Teacher values(‘02’ , ‘李四’);
insert into Teacher values(‘03’ , ‘王五’);
#–成绩表测试数据
insert into Score values(‘01’ , ‘01’ , 80);
insert into Score values(‘01’ , ‘02’ , 90);
insert into Score values(‘01’ , ‘03’ , 99);
insert into Score values(‘02’ , ‘01’ , 70);
insert into Score values(‘02’ , ‘02’ , 60);
insert into Score values(‘02’ , ‘03’ , 80);
insert into Score values(‘03’ , ‘01’ , 80);
insert into Score values(‘03’ , ‘02’ , 80);
insert into Score values(‘03’ , ‘03’ , 80);
insert into Score values(‘04’ , ‘01’ , 50);
insert into Score values(‘04’ , ‘02’ , 30);
insert into Score values(‘04’ , ‘03’ , 20);
insert into Score values(‘05’ , ‘01’ , 76);
insert into Score values(‘05’ , ‘02’ , 87);
insert into Score values(‘06’ , ‘01’ , 31);
insert into Score values(‘06’ , ‘03’ , 34);
insert into Score values(‘07’ , ‘02’ , 89);
insert into Score values(‘07’ , ‘03’ , 98);

1.查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(重点)

select
a.*,
b.s_score,
c.s_score
from
student a
left join score b
on a.s_id=b.s_id and b.c_id=‘01’
left join score c
on a.s_id=c.s_id and c.c_id=‘02’
where b.s_score >c.s_score

2.查询"01"课程比"02"课程成绩低的学生的信息及课程分数(重点)
select
a.*,
b.s_score,
c.s_score
from
student a
left join score b
on a.s_id=b.s_id and b.c_id=‘01’
left join score c
on a.s_id=c.s_id and c.c_id=‘02’
where b.s_score <c.s_score

3.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩(重点)

select
name,
round(score,2) as score
from(
select
a.s_name as name ,
avg(b.s_score) as score
from student a
left join score b
on a.s_id=b.s_id
group by a.s_name
) a
where score > 60

4.查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩:

select
name,
round(score,2) as score
from(
select
a.s_name as name ,
avg(b.s_score) as score
from student a
left join score b
on a.s_id=b.s_id
group by a.s_name
) a
where score < 60

5.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

select
a.s_id,
a.name,
count(a.c_id) as cnt,
sum(a.score) as sum
from(
select
a.s_id as s_id,
a.s_name as name,
b.c_id as c_id,
coalesce(b.s_score,0) as score
from
student a
left join score b
on a.s_id=b.s_id
) a
group by a.s_id,a.name

6.查询"李"姓老师的数量
select
t_name,
count(d.t_name)
from
student a
left join score b
on a.s_id = b.s_id
left join course c
on b.c_id=c.c_id
left join teacher d
on c.t_id=d.t_id
where d.t_name like “李%”
group by d.t_name

7.查询学过"张三"老师授课的同学的信息(重点)
select
a.*
from
student a
left join score b
on a.s_id = b.s_id
left join course c
on b.c_id=c.c_id
left join teacher d
on c.t_id=d.t_id
where d.t_name like “张三”

8.查询没学过"张三"老师授课的同学的信息(重点)
select
a.*
from student a
left join
(
select
*
from
student a
left join score b
on a.s_id = b.s_id
left join course c
on b.c_id=c.c_id
left join teacher d
on c.t_id=d.t_id
where d.t_name like “张三”
) b
on a.s_id=b.s_id
where b.s_id is NULL

9.查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息(重点):
select
a.*
from student a
left join score b
on a.s_id=b.s_id and b.c_id=“01”
left join score c
on a.s_id=c.s_id and c.c_id=“02”
where b.c_id is not null and c.c_id is not null

10.查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息(重点):
select
a.*
from student a
left join score b
on a.s_id=b.s_id and b.c_id=“01”
left join score c
on a.s_id=c.s_id and c.c_id=“02”
where b.c_id is not null and c.c_id is null

11、查询没有学全所有课程的同学的信息(重点):

select
a.*,
cnt2
from
student a
join (
select
count(b.c_id) as cnt1
from
course b
) d
left join (
select
c.s_id,
count(c.c_id) as cnt2
from score c
group by
s_id
) e
on a.s_id = e.s_id
where cnt1 > cnt2

12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息(重点):
select
d.*
from(
select
a.s_id
from
score a
join (
select
c_id
from score
where s_id=“01”
) b

where a.c_id=b.c_id and a.s_id !=“01”
group by a.s_id) c
left join student d
on c.s_id=d.s_id

13、查询和"01"号的同学学习的课程完全相同的其他同学的信息(重点):
select
f.*
from
(
select
*
from
(
select
a.s_id,
count(a.s_id) as cnt1
from
score a
join (
select
c_id
from score
where s_id=“01”
) b
where a.c_id=b.c_id and a.s_id !=“01”
group by a.s_id
) c
where c.cnt1 = (
select
count(1) as cnt2
from score d
where d.s_id=“01”
)
) e
left join student f
where e.s_id=f.s_id

14、查询没学过"张三"老师讲授的任一门课程的学生姓名(重点):
select
e.s_name
from student e
left join (
select
s_id
from score a
left join
course b
on a.c_id=b.c_id
left join
teacher c
on b.t_id=c.t_id where c.t_name=“张三”
) f
on e.s_id=f.s_id
where f.s_id is null

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩(重点):
select
e.s_id,e.s_name,
avg(f.s_score) as avg
from
(
select
s_id
from
(
select
*
from student a
left join score b
on a.s_id=b.s_id where b.s_score <60
) c
group by s_id
) d
left join student e
on d.s_id=e.s_id
left join score f
on d.s_id=f.s_id
group by e.s_id,e.s_name
16、检索"01"课程分数小于60,按分数降序排列的学生信息(和34类似):
select
a.*
from student a
left join score b
on a.s_id=b.s_id
where b.c_id =“01”
and b.s_score<60
order by b.s_score desc

17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(重点):
select
a.*,
b.avg
from score a
left join
(
select
s_id,
avg(s_score) as avg
from score
group by s_id
) b
on a.s_id =b.s_id
order by b.avg desc

18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(重点):
select
b.c_id,
b.c_name,
max(a.s_score) as max,
min(a.s_score)as min,
round(avg(a.s_score),2 )as avg,
round(sum(case when a.s_score>=60 then 1 else 0 end)/count(a.c_id),2) jige,
round(sum(case when a.s_score>=60 and a.s_score <70 then 1 else 0 end)/count(a.c_id),2) zhongdeng,
round(sum(case when a.s_score>=70 and a.s_score <90 then 1 else 0 end)/count(a.c_id),2) youliang,
round(sum(case when a.s_score>=90 then 1 else 0 end)/count(a.c_id),2) youxiu
from score a
left join course b
on a.c_id=b.c_id
group by b.c_id,b.c_name

19、按各科成绩进行排序,并显示排名(重点row_number):
select
c_id,
s_id,
s_score,
row_number()over(partition by c_id order by s_score desc)
from
score
where s_score is not null

20、查询学生的总成绩并进行排名(重点):
select
s_id,
sum,
row_number()over(order by sum desc)
from
(
select
s_id,
sum(s_score) as sum
from
score
group by s_id
) a

21、查询不同老师所教不同课程平均分从高到低显示(重点):
select
t_name,
avg,
row_number()over(order by avg desc)
from
(
select
t_name,
round(avg(a.s_score),2) as avg
from score a
left join course b
on a .c_id=b.c_id
left join teacher c
on b.t_id=c.t_id
group by t_name
) a
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩(重点):
select
*
from
(
select
c.,
e.
,
row_number()over(partition by e.c_id order by d.s_score) as number
from
student c
left join score d
on c.s_id=d.s_id
left join course e
on d.c_id =e.c_id
) f
where t_id is not null and number >=2 and number <=3
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比(重点)

select
a.c_id,
b.c_name,
round(sum(case when a.s_score >=0 and a.s_score<60 then 1 else 0 end)/count(a.c_id) ,2) bujige,
round(sum(case when a.s_score >=60 and a.s_score<70 then 1 else 0 end)/count(a.c_id),2) jige,
round(sum(case when a.s_score >=70 and a.s_score<85 then 1 else 0 end)/count(a.c_id),2) youxiu,
round(sum(case when a.s_score >=85 and a.s_score<=100 then 1 else 0 end)/count(a.c_id),2) hao
from score a
left join course b
on a.c_id=b.c_id
group by a.c_id,b.c_name
24、查询学生平均成绩及其名次(重点):
select
a.s_name,
avg(b.s_score) as avg,
row_number()over(order by (avg(b.s_score)) desc )
from student a
left join score b
on a.s_id=b.s_id
group by a.s_name

25、查询各科成绩前三名的记录(不考虑成绩并列情况)(重点)
select
*
from
(
select
a.,
b.
,
row_number()over(partition by b.c_id order by b.s_score desc) as number
from student a
left join score b
on a.s_id=b.s_id
where b.s_score is not null
) a
where number <=3
26、查询每门课程被选修的学生数:

27、查询出只有两门课程的全部学生的学号和姓名:
select
*
from
(
select
a.s_id,
a.s_name,
count(b.c_id) as cnt
from student a
left join score b
on
a.s_id=b.s_id
group by a.s_id,a.s_name
) a
where cnt =2
28、查询男生、女生人数:
select
count(b.s_sex) as womansex,
count(c.s_sex) as man
from student a
left join student b
on a.s_id=b.s_id and b.s_sex=“女”
left join student c
on a.s_id=c.s_id and c.s_sex=“男”
29、查询名字中含有"风"字的学生信息:
select
*
from student a
where a.s_name like “%风%”
30、查询同名同性学生名单,并统计同名人数:
select
a.s_id,a.s_name,a.s_sex,
count() as cnt
froms student a
left join student b
on a.s_name=b.s_name
where a.s_sex=b.s_sex
group by a.s_id,a.s_name,a.s_sex
31、查询1990年出生的学生名单(重点year):
select * from student where s_birth like ‘1990%’;
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列:
select
*
from
(
select
b.c_id,
avg(b.s_score) as avg_alias
from course a
left join score b
on a.c_id=b.c_id
group by b.c_id
) a
order by avg_alias desc,c_id
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩:
select
a.s_id,a.s_name,
avg(b.s_score) as avg
from student a
left join score b
on a.s_id=b.s_id
group by a.s_id,a.s_name
having avg>=85
34、查询课程名称为"数学",且分数低于60的学生姓名和分数:
select
a.s_name,
b.s_score
from student a
left join score b
on a.s_id=b.s_id
left join course c
on b.c_id=c.c_id
where c.c_name=“数学” and b.s_score< 60
35、查询所有学生的课程及分数情况(重点):
select
a.s_name,
b.c_id,
b.s_score,
c.
,
d.t_name
from student a
left join score b
on a.s_id=b.s_id
left join course c
on b.c_id=c.c_id
left join teacher d
on c.t_id =d.t_id

36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数(重点):
select
*
from student a
left join (
select
b.*
from score b
left join
(select
s_id
from score c
where c.s_score <=70
group by s_id
) d
on b.s_id=d.s_id where d.s_id is null
) e
on a.s_id=e.s_id
join course f
on e.c_id=f.c_id

37、查询课程不及格的学生:
select
*
from student a
left join score b
on a.s_id=b.s_id
where b.s_score <60
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名:
select
a.s_id,
a.s_name
from student a
left join score b
on a.s_id=b.s_id
where b.s_score >60 and b.c_id=“01”
39、求每门课程的学生人数:
select
b.c_id,
count() as cnt
from student a
left join score b
on a.s_id=b.s_id
where b.c_id is not null
group by b.c_id
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩(重点top):
select
*
from
(
select
a.
,
b.s_score,
row_number()over(order by b.s_score desc) as number
from student a
left join score b
on a.s_id=b.s_id
left join course c
on b.c_id=c.c_id
left join teacher d
on c.t_id=d.t_id
where b.c_id is not null and d.t_name=“张三”

) e
where number =1
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩(重点):
select
b.s_id,
b.c_id,
b.s_score
from score b
left join score c
on b.s_id=c.s_id
where b.s_score=c.s_score and b.c_id != c.c_id
42、查询每门课程成绩最好的前三名(重点):
select
*
from(
select
a.*,
row_number()over( partition by b.c_id order by b.s_score desc) as number
from student a
left join score b
on a.s_id=b.s_id
where b.s_score is not null
) a where number <=3
43、统计每门课程的学生选修人数(超过5人的课程才统计):
select
a.c_name,
count(a.c_name) as cnt
from course a
left join score b
on a.c_id=b.c_id
group by a.c_name
having cnt >5

44、检索至少选修两门课程的学生学号:
select
c_id,
count(c_id) as cnt
from score
group by c_id
having cnt >=2
45、查询选修了全部课程的学生信息:
select
a.*
from student a
left join
(
select
s_id,
count(c_id) as cnt1
from score b
group by s_id

) b
on a.s_id=b.s_id
inner join
(
select
count(*) as cnt2
from course
) c
where cnt1=cnt2

46、查询各学生的年龄(周岁):
select
s_name,
floor(datediff(now,s_birth)/365) as years
from
(
select
student.*,
from_unixtime(unix_timestamp(),‘yyyy-MM-dd’) as now
from student
) a

47、查询本周过生日的学生:
select
s_name,
cast(now AS BIGINT) as now,
cast(birth AS BIGINT) as birth
from
(
select
student.*,
weekofyear(s_birth )as birth,
weekofyear(from_unixtime(unix_timestamp(),‘yyyy-MM-dd’)) as now
from student

) a
where birth=now

48、查询下周过生日的学生:
select
s_name,
cast(now AS BIGINT) as now,
cast(birth AS BIGINT)+1 as birth
from
(
select
student.*,
weekofyear(s_birth )as birth,
weekofyear(from_unixtime(unix_timestamp(),‘yyyy-MM-dd’)) as now
from student

) a
where birth=now
49、查询本月过生日的学生:
select
s_name,
cast(now AS BIGINT) as now,
pmod(cast(birth AS BIGINT),12) as birth
from
(
select
student.*,
month(s_birth) as birth,
from_unixtime(unix_timestamp(),‘MM’) as now
from student

) a
where birth=now
50、查询12月份过生日的学生:
select
s_name,
pmod(cast(birth AS BIGINT),12) as birth
from
(
select
student.*,
month(s_birth) as birth

from student

) a
where birth=12

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值