根据数据库表格查询数据--笔试题

113 阅读1分钟

1.有如下几个数据表(Mysql库),根据要求写出Sql。
——————————————————————————
学生表:(字段:编号、学生姓名、出生日期、性别)
t_student

idnamebirthdaygender
3张天诚1989-01-01
4李偲偲1990-12-21
1庞中华1989-01-01
2席永社1990-12-21
5马小雨1991-12-21
6王思亮1990-05-20
7庞中华1993-09-08

——————————————————————————

课程表:(字段:课程号、课程名称、教师编号)

t_course

idcou_nametea_id
C01语文T002
C02数学T003
C03英语T004

——————————————————————————

成绩表:(字段:学生编号、课程编号、分数)

t_score

stu_idcou_idscore
1C0180
2C0290
3C0399
4C0260
5C0380
6C0180
7C0250
8C0380
1C0267
3C0188
6C0366
7C0359
3C0246

——————————————————————————

教师表:(字段:教师编号、教师姓名)

t_teacher

idname
T001李四
T002张三
T003王五
T004赵六

——————————————————————————
查找1990年出生的学生名单;

select id name from t_student where year(birthday) = 1990;

查询所有学生的学号、姓名、选课数、总成绩;

select a.id,a.name,count(b.cou_id) as 选课数,SUM(b.score) as 总成绩
from t_student as a left join t_score as b
on a.id = b.stu_id
group by a.id,a.name;

查询出每门课程的及格人数和不及格人数;

SELECT cou_id,
sum(case when score>=60 then 1
else 0
end) as 及格人数,
sum(case when score < 60 then 1
else 0
end) as 不及格人数
from t_score
group by cou_id;

查询没有学全所有课的学生的学号、姓名;

select id,name
from t_student
where id in(
select stu_id
from t_score
group by stu_id
having count(cou_id) < (select count(id) from t_course)
);