一、1、 作业要求如下
1、 针对上方的表结构,编写create table建表语句
2、 针对上方的数据要求,编写insert into语句
   3、 针对下面的要求,完成每道题
   
   a) 查询student表的所有记录
   
   b) 查询student表的第2条到4条记录
   
   c) 从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
   
   d) 从student表中查询计算机系和英语系的学生的信息(写出两种答案)
   
   e) 从student表中查询年龄18~29岁的学生信息(写出两种答案)
   
   f) 从student表中查询每个院系有多少人
   
   g) 从score表中查询每个科目的最高分
   
   h) 计算每个考试科目的平均成绩
   
   i) 将计算机考试成绩按从高到低进行排序
   
    
     
      
       答案:
      
      
     
    
   
   create table student
   
   (
   
   id int(10) primary key,
   
   name varchar(20) not null,
   
   sex varchar(4),
   
   birth year,
   
   department varchar(20) not null,
   
   address varchar(50)
   
   );
   create table score
   
   (
   
   id int(10) primary key,
   
   stu_id int(10) not null,
   
   c_name varchar(20),
   
   grade int(10)
   
   );
   
   insert into student(id,name,sex,birth,department,address)
   
   values(901,’张老大’,’男’,1985,’计算机系’,’北京市海淀区’),
   
   (902,’张老二’,’男’,1986,’中文系’,’北京市昌平区’),
   
   (903,’张三’,’女’,1990,’中文系’,’湖南省永州市’),
   
   (904,’李四’,’男’,1990,’英语系’,’辽宁省阜新市’),
   
   (905,’王五’,’女’,1991,’英语系’,’福建省厦门市’),
   
   (906,’王六’,’男’,1988,’计算机系’,’湖南省衡阳市’);
   
   insert into score(id,stu_id,c_name,grade)
   
   values(1,901,’英语’,80),
   
   (2,902,’计算机’,65),
   
   (3,902,’中文’,88),
   
   (4,903,’中文’,95),
   
   (5,904,’计算机’,70),
   
   (6,904,’英语’,92),
   
   (7,905,’英语’,94),
   
   (8,906,’计算机’,90),
   
   (9,901,’计算机’,null),
   
   (10,901,’中文’,null),
   
   (11,902,’英语’,null);
   select * from student;
   
   select * from student limit 1,3;
   
   select id,name,department from student ;
   
   select * from student where department=’计算机系’ or department =’英语系’;
   
   select * from student where department in (‘英语系’,’计算机系’);
   
   select * from student where birth between (Year(curdate())-29) and (Year(curdate())-18);
   
   select * from student where birth >= (Year(curdate())-29) and birth <= (Year(curdate())-18);
   
   select count(*),department  from student group by department;
   
   select max(grade),c_name from score group by c_name;
   
   select avg(grade),c_name from score group by c_name;
   
   select * from score where c_name = ‘计算机’ order by grade desc;
   
    2、
    
     
      作业要求如下
     
    
   
    单表查询练习
    
    1、查询<学生信息表>,查询学生”张三”的全部基本信息
    
    2、查询<学生信息表>,查询学生”张三”或”李四”的基本信息
    
    3、查询<学生信息表>,查询姓”张”学生的姓名,出生年份,系别。
    
    4、查询<学生信息表>,查询姓名中含有”四”字的学生的基本信息
    
    5、查询<学生信息表>,查询姓名长度为三个字,姓“李”,且最后一个字是“强”的全部学生的基本信息
    
    6、查询<学生信息表>,查询姓”张”或者姓”李”的学生的基本信息。
    
    7、查询<学生信息表>,查询姓”张”并且是”计算机系”的学生信息
    
    9、查询<学生信息表>,查询姓”张”,但是不是”计算机系”的学生信息
    
    10、查询不是姓”张”,且是”计算机系”的学生信息
    
    11、查询所属省份不是”英语”、”计算机系”的学生的信息
    
    12、查询<学生信息表>,查询全部学生信息,并按照“性别”降序排序
    
    13、查询<学生信息表>,查询现有学生都来自于哪些不同的学院
    
    14、查询<学生选修信息表>,查询没有填写成绩的学生的学号、课程名称和成绩,并按学号升序排序
    
    15、查询<学生选修信息表>,查询全部填写了成绩的学生的学号、课程名称和成绩,并按照“成绩”从高到低进行排序
    
   
    
     答案:
    
   
    
     
      select * from student where name=’张三’;
      
      select * from student where name in (‘张三’,’李四’);
      
      /*遗留问题,怎么根据出生时间,显示年龄*/
      
      select name,birth,department from student where name like ‘张%’;
      
      select * from student where name like ‘%四%’;
      
      select * from student where name like ‘李_强’;
      
      select * from student where name like ‘李%’ or name like ‘张%’;
      
      select * from student where name like ‘张%’ and department =’计算机系’;
      
      select * from student where name like ‘张%’ and department <>’计算机系’;
      
      select * from student where name not like ‘张%’ and department=’计算机系’;
      
      select * from student where department not in (‘英语系’,’计算机系’);
      
      select * from student order by sex desc;
      
      select department from student group by department;
      
      select stu_id,c_name,grade from score where grade is null order by stu_id;
      
     
    
   
    
     
      select stu_id,c_name,grade from score where grade is not null order by grade desc;
      
     
    
   
    
     
      聚合函数练习
     
    
   
    
     
      1、统计<学生信息表>,统计共有多少个 学生
      
      2、统计<学生信息表>,统计年龄小于20岁的学生有多少个
      
      3、统计<学生选修信息表>,统计学号为”901″的学生的平均成绩
      
      4、统计<学生选修信息表>,统计学号为”902″的学生的总成绩
      
      5、统计<学生选修信息表>,查询课程号为” 902”的课程的最高成绩
      
      6、统计<学生信息表>,查询所有学生中的最大年龄是多少
     
    
    
   
    
     
      
       答案:
      
     
    
   
    
     
      select count(*) from student;
      
      select count(*) from student where (Year(curdate()) – birth) <20;
      
      select avg(grade) from score where stu_id = ‘901’;
      
      select sum(grade) from score where stu_id=’902′;
      
      select max(grade) from score where stu_id =’902′;
      
      select max(Year(curdate())-birth) from student;
      分组查询练习
      
      1、统计<学生选修信息表>,统计每个课程的选修人数
      
      2、统计<学生选修信息表>,统计每个同学的总成绩
      
      3、统计<学生信息表>,统计每个班级中每种性别的学生人数,并按照班级排序
      
      4、统计<学生选修信息表>,统计每门课程的平均成绩,并按照平均成绩降序排序
      
      5、统计<学生选修信息表>,显示有两门以上课程不及格的学生的学号
      
     
    
   
    
     
      6、统计<学生信息表>,统计每个班级中的最大年龄是多少
     
    
   
    
     答案:
    
   
    
     
     
    
    select count(*),c_name as ‘课程名称’  from score group by c_name;
    
    select sum(grade), stu_id as ‘学号’ from score group by stu_id;
    
    select count(*) from student group by bj,sex order by bj desc;/*无数据,未执行过*/
    
    select avg(grade) from score group by c_name order by avg(grade) desc;
    
    select stu_id from score where grade<60 group by stu_id having count(*)>2;
    
    select min(birth) from student group by bj;/*无数据,未执行*/