26、查询每门课程被选修的学生数:
select a.c_id,a.c_name,count(b.s_score) num_course
from course a
join score b on a.c_id = b.c_id
group by a.c_id,a.c_name
27、查询出只有两门课程的全部学生的学号和姓名:
select b.s_id,a.s_name,count(b.c_id) num_course
from student a
join score b on a.s_id = b.s_id
group by b.s_id,a.s_name
having num_course =2
28、查询男生、女生人数:
select s_sex,count(s_id) total
from student
group by s_sex
29、查询名字中含有"风"字的学生信息:
select *
from student
where s_name like '%风%'
30、查询同名同性学生名单,并统计同名人数:
select
s_name,s_sex,
count(1) over(distribute by s_name,s_sex) num
from student
31、查询1990年出生的学生名单:
substr,分割匹配,很好用的函数,使用时注意下标从1起始
select *
from student
where substr(s_birth,1,4)='1990'
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列:
select a.c_id,a.c_name,round(avg(b.s_score),2) avg_score
from course a
join score b on a.c_id = b.c_id
group by a.c_id,a.c_name
order by avg_score desc,a.c_id asc
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩:
select w.*
from (select a.s_id,a.s_name,round(avg(b.s_score),2) avg_score
from student a
join score b on a.s_id = b.s_id
group by a.s_id,a.s_name) w
where w.avg_score >=85
34、查询课程名称为"数学",且分数低于60的学生姓名和分数:
select a.s_name,c.c_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、查询所有学生的课程及分数情况:
union all 也是一个常用重点语法需要重点掌握
select p.*,(p.01_score+p.02_score+p.03_score) as total_score from(
select
t.s_id as s_id,
t.s_name as s_name,
sum(t.01_score) as 01_score,
sum(t.02_score) as 02_score,
sum(t.03_score) as 03_score
from(
select a.s_id,a.s_name,b.s_score as 01_score,0 as 02_score,0 as 03_score from student a join score b on a.s_id=b.s_id where b.c_id='01' union all
select a.s_id,a.s_name,0 as 01_score,b.s_score as 02_score,0 as 03_score from student a join score b on a.s_id=b.s_id where b.c_id='02' union all
select a.s_id,a.s_name,0 as 01_score,0 as 02_score,b.s_score as 03_score from student a join score b on a.s_id=b.s_id where b.c_id='03'
) t
group by t.s_id,t.s_name
) p
36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数:
select
a.s_name,
c.c_name,
b.s_score
from student a
join score b on a.s_id=b.s_id
join course c on b.c_id=c.c_id
where b.s_score>70
37、查询课程不及格的学生:
select
a.s_name,
c.c_name,
b.s_score
from student a
join score b on a.s_id=b.s_id
join course c on b.c_id=c.c_id
where b.s_score<60
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名:
select a.s_id,a.s_name,b.s_score
from student a
join score b on a.s_id = b.s_id
where b.c_id = '01' and b.s_score >80
39、求每门课程的学生人数:
select a.c_id,a.c_name,count(b.s_id) num_student
from course a
join score b on a.c_id = b.c_id
group by a.c_id,a.c_name
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩:
select a.s_id,a.s_name,b.s_score
from student a
join score b on a.s_id = b.s_id
join course c on b.c_id = c.c_id
join teacher d on d.t_id = c.t_id
where d.t_name = '张三'
order by b.s_score desc
limit 1
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩:
select distinct a.s_id,a.c_id,a.s_score
from score a,score b
where a.c_id!=b.c_id and a.s_score=b.s_score
42、查询每门课程成绩最好的前三名:
select a.s_id,a.s_name,c.c_name,w.s_score,w.rm
from student a
join (SELECT a.s_id,a.c_id,a.s_score,
row_number() over(distribute by c_id sort by s_score desc) rm
from score a) w on a.s_id = w.s_id
join course c on w.c_id = c.c_id
where w.rm <=3
order by c.c_name,w.rm asc
43、统计每门课程的学生选修人数(超过5人的课程才统计):
– 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select c_id,count(s_id) num_student
from score
group by c_id
having num_student >=5
order by num_student desc,c_id asc
44、检索至少选修两门课程的学生学号:
select a.s_id,count(1) num_course
from score a
group by a.s_id
having num_course>=2
45、查询选修了全部课程的学生信息:
select
b.*
from(
select
s_id,
count(c_id) as num_course
from score
group by s_id
) a
join student b on a.s_id=b.s_id
join (select count(*) as total_course from course) c on c.total_course=a.num_course
46、查询各学生的年龄(周岁):
**– 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
时间函数也是比较重要,需要重点掌握的函数**
select *,
(case when month(current_date())<month(s_birth)
then year(current_date()) - year(s_birth) -1
else year(current_date()) - year(s_birth) end
) as age
from student
47、查询本周过生日的学生:
select *
from student
where weekofyear(current_date()) = weekofyear(s_birth)
48、查询下周过生日的学生:
select *
from student
where weekofyear(current_date())+1 = weekofyear(s_birth)
49、查询本月过生日的学生:
select *
from student
where month(s_birth) = month(current_date())
50、查询12月份过生日的学生:
select *
from student
where month(s_birth) = 12