MySQL学习-基础练习题

day1

学生表操作:

    1. 查询出班级205有多少个男生

    2. 查询出名字为4个字的所有学生信息(编号、姓名,年龄,班级)

    3. 查询出所有姓王的学生信息(编号、姓名,年龄,班级)

    4. 查询出班级编号为201,202,203的女生总人数

    5. 查询出学号整十的所有女生信息(姓名、年龄、个人简介)

    6. 删除301班级中年龄在23岁以上的学生信息

    7. 把一个叫'卫然'的学生的姓别改成女的

    8. 把401班级中的姓名为'吴杰'的学生信息删除

    9. 计算305班中所有学生的平均年龄以及他们的最大年龄和最小年龄

    10. 查询401,402,403,404,405中所有学生的年龄平均值

    11. 查询出所有学生中"白"的学生信息,并对他们使用年龄进行升序排列

    12. 添加以下学生记录到数据表中
         姓名     年龄    性别    班级   个性签名
         张三丰    22      1      301   我是武当老板
         张翠山    21      1      302   我是武当老板的五弟子
         张无忌    20      1      302   明教老板

    13. 查询年龄在18-20之间的姓李的女生

    14. 查询年龄在18-20之间的所有女生,并按照编号进行降序排序

    15. 查询出301,302,303,304,305,306中每个班级总人数。

成绩表操作

    16. 查询出学号为9的学生的总成绩

    17. 查询出课程编号为4的课程平均成绩

    18. 查询出学号为6的学生的所有成绩,并显示对应的课程编号。

    19. 查询出课程编号为20的课程成绩,并进行分数的降序排列,显示10个成绩即可。

    20. 查询出学号为1,2,3,4,5,6这几个学员的平均成绩。

    21. 查询出证成绩表中每个学科的平均成绩。

day2

python操作数据库

    1. 查询student表的所有记录

    2. 查询student表的第2条到第4条记录

    3. 查询所有学生的学号(id)、姓名(name)和报读课程(department)的信息

    4. 删除305班年龄最小的学生

    5. 修改id为66的学员的姓名为男

SQL语句编写
    
     1. 查询Django课程和Django项目的所有学生的信息[学号,姓名,年龄,成绩]

mysql> select a.id,name,age,achievement
    -> from student as a
    -> left join achievement as b
    -> on a.id = b.sid
    -> left join course as c
    -> on b.cid = c.id
    -> where c.course like 'Django%';

    2. 查询id=5的老师的所有学生信息[学号、姓名、年龄]

mysql> select a.id,a.name,a.age
    -> from student as a
    -> left join achievement as b
    -> on a.id = b.sid
    -> left join course as c
    -> on b.cid = c.id
    -> left join lecturer as d
    -> on c.lecturer_id = d.id
    -> where d.id =5;

    3. 查询306班、307、308班中成绩及格的学生

mysql> select a.name,a.age,a.sex,c.course,b.achievement
    -> from student as a
    -> left join achievement as b
    -> on a.id= b.sid
    -> left join course as c
    -> on b.cid = c.id
    -> where a.class in (306,307,308) and b.achievement >=60;

    4. 查询python成绩及格的所有学生信息[学号、姓名、年龄、班级]

mysql> select a.id,a.name,a.age,a.class,b.achievement
    -> from student as a
    -> left join achievement as b
    -> on a.id = b.sid
    -> left join course as c
    -> on b.cid = c.id
    -> where c.course='python' and b.achievement >=60;

    5. 查出所有女生的平均成绩和报读课程。

mysql> select c.course,avg(b.achievement)
    -> from student as a
    -> left join achievement as b
    -> on a.id = b.sid
    -> left join course as c
    -> on b.cid = c.id
    -> where a.sex =2 group by c.course;

    6. 查询出女生人数最多的5个课程。

mysql>select c.course,c.id,count(a.id) 
        -> from student as a 
        -> left join achievement as b 
        -> on a.id = b.sid 
        -> left join course as c 
        -> on b.cid = c.id 
        -> where a.sex =2  group by c.course order by count(a.id) desc, c.id asc limit 5;

    7. 查询出报读flask课程的学生中女生的数量

mysql> select count(a.id)
    -> from student as a
    -> left join achievement as b
    -> on a.id = b.sid
    -> left join course as c
    -> on b.cid = c.id
    -> where c.course ='flask' and a.sex =2;

    8. 查询出 赵华 报读的课程信息[ 上课老师名字,课程分数,平均分 ]

mysql> select d.name,c.course,b.achievement,avg(b.achievement)
    -> from student as a
    -> left join achievement as b
    -> on a.id = b.sid
    -> left join course as c
    -> on b.cid = c.id
    -> left join lecturer as d
    -> on c.lecturer_id = d.id
    -> where a.name = '赵华' group by c.course;

    9.查出 401 班所有学生的 平均分和总分[分组查询]。

mysql> select c.course,avg(b.achievement),sum(b.achievement)
    -> from student as a
    -> left join achievement as b
    -> on a.id= b.sid
    -> left join course as c
    -> on b.cid = c.id
    -> where a.class=401 group by c.course;

    10.查出报读了flask、django课程中年龄在21到23岁之间的所有男生的成绩

mysql> select c.course,avg(b.achievement)
    -> from student as a
    -> left join achievement as b
    -> on a.id = b.sid
    -> left join course as c
    -> on b.cid = c.id
    -> where c.course in ('flask','django') and a.age between 21 and 23 group by c.course;

    11.查处总分在200分以上的所有学生的姓名、班级

select a.class,a.name,sum(b.achievement) 
from student as a 
left join achievement as b 
on a.id = b.sid 
group by a.id having sum(b.achievement)>200 order by sum(b.achievement) desc ;

ok

posted @ 2019-06-16 16:34  柴犬砍柴  阅读(1205)  评论(0编辑  收藏  举报