MySQL经典练习题

表名和字段

–1.学生表 
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 
–2.课程表 
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号 
–3.教师表 
Teacher(t_id,t_name) –教师编号,教师姓名 
–4.成绩表 
Score(s_id,c_id,s_score) –学生编号,课程编号,分数

测试数据

  1.  
    --建表
  2.  
    --学生表
  3.  
    CREATE TABLE `Student`(
  4.  
    `s_id` VARCHAR(20),
  5.  
    `s_name` VARCHAR(20) NOT NULL DEFAULT '',
  6.  
    `s_birth` VARCHAR(20) NOT NULL DEFAULT '',
  7.  
    `s_sex` VARCHAR(10) NOT NULL DEFAULT '',
  8.  
    PRIMARY KEY(`s_id`)
  9.  
    );
  10.  
    --课程表
  11.  
    CREATE TABLE `Course`(
  12.  
    `c_id` VARCHAR(20),
  13.  
    `c_name` VARCHAR(20) NOT NULL DEFAULT '',
  14.  
    `t_id` VARCHAR(20) NOT NULL,
  15.  
    PRIMARY KEY(`c_id`)
  16.  
    );
  17.  
    --教师表
  18.  
    CREATE TABLE `Teacher`(
  19.  
    `t_id` VARCHAR(20),
  20.  
    `t_name` VARCHAR(20) NOT NULL DEFAULT '',
  21.  
    PRIMARY KEY(`t_id`)
  22.  
    );
  23.  
    --成绩表
  24.  
    CREATE TABLE `Score`(
  25.  
    `s_id` VARCHAR(20),
  26.  
    `c_id` VARCHAR(20),
  27.  
    `s_score` INT(3),
  28.  
    PRIMARY KEY(`s_id`,`c_id`)
  29.  
    );
  30.  
    --插入学生表测试数据
  31.  
    insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
  32.  
    insert into Student values('02' , '钱电' , '1990-12-21' , '男');
  33.  
    insert into Student values('03' , '孙风' , '1990-05-20' , '男');
  34.  
    insert into Student values('04' , '李云' , '1990-08-06' , '男');
  35.  
    insert into Student values('05' , '周梅' , '1991-12-01' , '女');
  36.  
    insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
  37.  
    insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
  38.  
    insert into Student values('08' , '王菊' , '1990-01-20' , '女');
  39.  
    --课程表测试数据
  40.  
    insert into Course values('01' , '语文' , '02');
  41.  
    insert into Course values('02' , '数学' , '01');
  42.  
    insert into Course values('03' , '英语' , '03');
  43.  
     
  44.  
    --教师表测试数据
  45.  
    insert into Teacher values('01' , '张三');
  46.  
    insert into Teacher values('02' , '李四');
  47.  
    insert into Teacher values('03' , '王五');
  48.  
     
  49.  
    --成绩表测试数据
  50.  
    insert into Score values('01' , '01' , 80);
  51.  
    insert into Score values('01' , '02' , 90);
  52.  
    insert into Score values('01' , '03' , 99);
  53.  
    insert into Score values('02' , '01' , 70);
  54.  
    insert into Score values('02' , '02' , 60);
  55.  
    insert into Score values('02' , '03' , 80);
  56.  
    insert into Score values('03' , '01' , 80);
  57.  
    insert into Score values('03' , '02' , 80);
  58.  
    insert into Score values('03' , '03' , 80);
  59.  
    insert into Score values('04' , '01' , 50);
  60.  
    insert into Score values('04' , '02' , 30);
  61.  
    insert into Score values('04' , '03' , 20);
  62.  
    insert into Score values('05' , '01' , 76);
  63.  
    insert into Score values('05' , '02' , 87);
  64.  
    insert into Score values('06' , '01' , 31);
  65.  
    insert into Score values('06' , '03' , 34);
  66.  
    insert into Score values('07' , '02' , 89);
  67.  
    insert into Score values('07' , '03' , 98);

练习题和sql语句

    1.  
      -- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
    2.  
       
    3. select a.* ,b.s_score as 01_score,c.s_score as 02_score from
    4.  
      student a
    5.  
      join score b on a.s_id=b.s_id and b.c_id='01'
    6.  
      left join score c on a.s_id=c.s_id and c.c_id='02' or c.c_id = NULL where b.s_score>c.s_score
    7.  
       
    8. -- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
    9.  
       
    10. select a.* ,b.s_score as 01_score,c.s_score as 02_score from
    11.  
      student a left join score b on a.s_id=b.s_id and b.c_id='01' or b.c_id=NULL
    12.  
      join score c on a.s_id=c.s_id and c.c_id='02' where b.s_score<c.s_score
    13.  
       
    14.  
       
    15.  
      -- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
    16.  解法一:
      select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from
    17. student b
    18. join score a on b.s_id = a.s_id
    19. GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)>=60;
    20.  
       解法二:
    21.  select a.s_id,a.s_name,avg(b.s_score) from student a join score  b on a.s_id=b.s_id group by a.s_id having avg(b.s_score)>60;
       
    22.  
      -- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
    23. -- (包括有成绩的和无成绩的)
    24.  解法一:
       select a.s_id,a.s_name,avg(b.s_score) from student a left join score b on
    25. a.s_id=b.s_id group by a.s_id having avg(b.s_score)<60 or avg(b.s_score) is null;
    26.  
      解法二:
      select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from
    27. student b
    28. left join score a on b.s_id = a.s_id
    29. GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)<60
    30. union
    31. select a.s_id,a.s_name,0 as avg_score from
    32. student a
    33. where a.s_id not in (
    34. select distinct s_id from score);
    35.  
       
    36. -- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
    37.  
      select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score from
    38. student a
    39. left join score b on a.s_id=b.s_id
    40. GROUP BY a.s_id;
    41.  
       
    42. -- 6、查询"李"姓老师的数量
    43.  
      select count(t_id) from teacher where t_name like '李%';
    44.  
       
    45.  
      -- 7、查询学过"张三"老师授课的同学的信息
    46. 解法一: 
    47.  
      select a.* 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 c.t_id=d.t_id and d.t_name='张三';
       
      解法二:
      select a.* from
    48. student a
    49. join score b on a.s_id=b.s_id where b.c_id in(
    50. select c_id from course where t_id =(
    51. select t_id from teacher where t_name = '张三'));
    52.  
       
    53. -- 8、查询没学过"张三"老师授课的同学的信息
    54.  解法一:
      select * from
    55. student c
    56. where c.s_id not in(
    57. select a.s_id from student a join score b on a.s_id=b.s_id where b.c_id in(
    58. select c_id from course where t_id =(
    59. select t_id from teacher where t_name = '张三')));
       
      解法二:
      select * from student a where a.s_id not in (select a.s_id 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 c.t_id=d.t_id and d.t_name='张三');
    60.  
      -- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
    61.  
       
    62. select a.* from
    63. student a,score b,score c
    64. where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id='01' and c.c_id='02';
    65.  
       
    66. -- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
    67.  
    68. select a.* from
    69. student a
    70. where a.s_id in (select s_id from score where c_id='01' ) and a.s_id not in(select s_id from score where c_id='02')
    71.   
    72.  
      -- 11、查询没有学全所有课程的同学的信息
    73.  
    74. 解法一:
    75.  
       select * from student a where a.s_id in (select b.s_id from score b group by b.s_id
    76. having count(b.c_id)<3);
    77.  
    78. 解法二:
    79.  
      select s.* from
    80. student s where s.s_id in(
    81. select s_id from score where s_id not in(
    82. select a.s_id from score a
    83. join score b on a.s_id = b.s_id and b.c_id='02'
    84. join score c on a.s_id = c.s_id and c.c_id='03'
    85. where a.c_id='01'))
    86.  
    87. -- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
      解法一:
       
    88.  select distinct a.* from student a join score b on a.s_id=b.s_id where b.c_id in (select c_id from score where s_id='01') and b.s_id!='01';
    89.  
      解法二:
    90. select * from student where s_id in(
    91. select distinct a.s_id from score a where a.c_id in
      (select a.c_id from score a where a.s_id='01'));
    92.  
    93. -- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
    94.  
    95. select a.* from student a where a.s_id in(
    96.  
      select distinct s_id from score where s_id!='01' and c_id in(select c_id from score where s_id='01')
    97.  
      group by s_id
    98.  
      having count(1)=(select count(1) from score where s_id='01'));
    99.  
      -- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
    100.  
      select a.s_name from student a where a.s_id not in (
    101.  
      select s_id from score where c_id =
    102.  
      (select c_id from course where t_id =(
    103.  
      select t_id from teacher where t_name = '张三'))
    104.  
      group by s_id);
    105.  
       
    106.  
      -- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
    107.  
      select a.s_id,a.s_name,ROUND(AVG(b.s_score)) from
    108.  
      student a
    109.  
      left join score b on a.s_id = b.s_id
    110.  
      where a.s_id in(
    111.  
      select s_id from score where s_score<60 GROUP BY s_id having count(1)>=2)
    112.  
      GROUP BY a.s_id,a.s_name
    113.  
       
    114.  
      -- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
    115.  
      select a.*,b.c_id,b.s_score from
    116.  
      student a,score b
    117.  
      where a.s_id = b.s_id and b.c_id='01' and b.s_score<60 ORDER BY b.s_score DESC;
    118.  
       
    119.  
      -- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
    120.  
      select a.s_id,(select s_score from score where s_id=a.s_id and c_id='01') as 语文,
    121.  
      (select s_score from score where s_id=a.s_id and c_id='02') as 数学,
    122.  
      (select s_score from score where s_id=a.s_id and c_id='03') as 英语,
    123.  
      round(avg(s_score),2) as 平均分 from score a GROUP BY a.s_id ORDER BY 平均分 DESC;
    124.  
       
    125.  
      -- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
    126.  
      --及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
    127.  
      select a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2),
    128.  
      ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率,
    129.  
      ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 中等率,
    130.  
      ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优良率,
    131.  
      ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率
    132.  
      from score a left join course b on a.c_id = b.c_id GROUP BY a.c_id,b.c_name
    133.  
       
    134.  
      -- 19、按各科成绩进行排序,并显示排名(实现不完全)
    135.  
      -- mysql没有rank函数
    136.  
      select a.s_id,a.c_id,
    137.  
      @i:=@i +1 as i保留排名,
    138.  
      @k:=(case when @score=a.s_score then @k else @i end) as rank不保留排名,
    139.  
      @score:=a.s_score as score
    140.  
      from (
    141.  
      select s_id,c_id,s_score from score WHERE c_id='01' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
    142.  
      )a,(select @k:=0,@i:=0,@score:=0)s
    143.  
      union
    144.  
      select a.s_id,a.c_id,
    145.  
      @i:=@i +1 as i,
    146.  
      @k:=(case when @score=a.s_score then @k else @i end) as rank,
    147.  
      @score:=a.s_score as score
    148.  
      from (
    149.  
      select s_id,c_id,s_score from score WHERE c_id='02' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
    150.  
      )a,(select @k:=0,@i:=0,@score:=0)s
    151.  
      union
    152.  
      select a.s_id,a.c_id,
    153.  
      @i:=@i +1 as i,
    154.  
      @k:=(case when @score=a.s_score then @k else @i end) as rank,
    155.  
      @score:=a.s_score as score
    156.  
      from (
    157.  
      select s_id,c_id,s_score from score WHERE c_id='03' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
    158.  
      )a,(select @k:=0,@i:=0,@score:=0)s
    159.  
       
    160.  
      -- 20、查询学生的总成绩并进行排名
    161.  
      select a.s_id,
    162.  
      @i:=@i+1 as i,
    163.  
      @k:=(case when @score=a.sum_score then @k else @i end) as rank,
    164.  
      @score:=a.sum_score as score
    165.  
      from (select s_id,SUM(s_score) as sum_score from score GROUP BY s_id ORDER BY sum_score DESC)a,
    166.  
      (select @k:=0,@i:=0,@score:=0)s
    167.  
       
    168.  
      -- 21、查询不同老师所教不同课程平均分从高到低显示
    169.  
       
    170.  
      select a.t_id,c.t_name,a.c_id,ROUND(avg(s_score),2) as avg_score from course a
    171.  
      left join score b on a.c_id=b.c_id
    172.  
      left join teacher c on a.t_id=c.t_id
    173.  
      GROUP BY a.c_id,a.t_id,c.t_name ORDER BY avg_score DESC;
    174.  
      -- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
    175.  
       
    176.  
      select d.*,c.排名,c.s_score,c.c_id from (
    177.  
      select a.s_id,a.s_score,a.c_id,@i:=@i+1 as 排名 from score a,(select @i:=0)s where a.c_id='01'
    178.  
      )c
    179.  
      left join student d on c.s_id=d.s_id
    180.  
      where 排名 BETWEEN 2 AND 3
    181.  
      UNION
    182.  
      select d.*,c.排名,c.s_score,c.c_id from (
    183.  
      select a.s_id,a.s_score,a.c_id,@j:=@j+1 as 排名 from score a,(select @j:=0)s where a.c_id='02'
    184.  
      )c
    185.  
      left join student d on c.s_id=d.s_id
    186.  
      where 排名 BETWEEN 2 AND 3
    187.  
      UNION
    188.  
      select d.*,c.排名,c.s_score,c.c_id from (
    189.  
      select a.s_id,a.s_score,a.c_id,@k:=@k+1 as 排名 from score a,(select @k:=0)s where a.c_id='03'
    190.  
      )c
    191.  
      left join student d on c.s_id=d.s_id
    192.  
      where 排名 BETWEEN 2 AND 3;
    193.  
       
    194.  
       
    195.  
      -- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
    196.  
       
    197.  
       
    198.  
      select distinct f.c_name,a.c_id,b.`85-100`,b.百分比,c.`70-85`,c.百分比,d.`60-70`,d.百分比,e.`0-60`,e.百分比 from score a
    199.  
      left join (select c_id,SUM(case when s_score >85 and s_score <=100 then 1 else 0 end) as `85-100`,
    200.  
      ROUND(100*(SUM(case when s_score >85 and s_score <=100 then 1 else 0 end)/count(*)),2) as 百分比
    201.  
      from score GROUP BY c_id)b on a.c_id=b.c_id
    202.  
      left join (select c_id,SUM(case when s_score >70 and s_score <=85 then 1 else 0 end) as `70-85`,
    203.  
      ROUND(100*(SUM(case when s_score >70 and s_score <=85 then 1 else 0 end)/count(*)),2) as 百分比
    204.  
      from score GROUP BY c_id)c on a.c_id=c.c_id
    205.  
      left join (select c_id,SUM(case when s_score >60 and s_score <=70 then 1 else 0 end) as `60-70`,
    206.  
      ROUND(100*(SUM(case when s_score >60 and s_score <=70 then 1 else 0 end)/count(*)),2) as 百分比
    207.  
      from score GROUP BY c_id)d on a.c_id=d.c_id
    208.  
      left join (select c_id,SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end) as `0-60`,
    209.  
      ROUND(100*(SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end)/count(*)),2) as 百分比
    210.  
      from score GROUP BY c_id)e on a.c_id=e.c_id
    211.  
      left join course f on a.c_id = f.c_id
    212.  
       
    213.  
      -- 24、查询学生平均成绩及其名次
    214.  
       
    215.  
      select a.s_id,
    216.  
      @i:=@i+1 as '不保留空缺排名',
    217.  
      @k:=(case when @avg_score=a.avg_s then @k else @i end) as '保留空缺排名',
    218.  
      @avg_score:=avg_s as '平均分'
    219.  
      from (select s_id,ROUND(AVG(s_score),2) as avg_s from score GROUP BY s_id)a,(select @avg_score:=0,@i:=0,@k:=0)b;
    220.  
      -- 25、查询各科成绩前三名的记录
    221.  
      -- 1.选出b表比a表成绩大的所有组
    222.  
      -- 2.选出比当前id成绩大的 小于三个的
    223.  
      select a.s_id,a.c_id,a.s_score from score a
    224.  
      left join score b on a.c_id = b.c_id and a.s_score<b.s_score
    225.  
      group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3
    226.  
      ORDER BY a.c_id,a.s_score DESC
    227.  
       
    228.  
      -- 26、查询每门课程被选修的学生数
    229.  
       
    230.  
      select c_id,count(s_id) from score a GROUP BY c_id
    231.  
       
    232.  
      -- 27、查询出只有两门课程的全部学生的学号和姓名
    233.  
      select s_id,s_name from student where s_id in(
    234.  
      select s_id from score GROUP BY s_id HAVING COUNT(c_id)=2);
    235.  
       
    236.  
      -- 28、查询男生、女生人数
    237.  
      select s_sex,COUNT(s_sex) as 人数 from student GROUP BY s_sex
    238.  
       
    239.  
      -- 29、查询名字中含有"风"字的学生信息
    240.  
       
    241.  
      select * from student where s_name like '%风%';
    242.  
       
    243.  
      -- 30、查询同名同性学生名单,并统计同名人数
    244.  
       
    245.  
      select a.s_name,a.s_sex,count(*) from student a JOIN
    246.  
      student b on a.s_id !=b.s_id and a.s_name = b.s_name and a.s_sex = b.s_sex
    247.  
      GROUP BY a.s_name,a.s_sex
    248.  
       
    249.  
       
    250.  
       
    251.  
      -- 31、查询1990年出生的学生名单
    252.  
       
    253.  
      select s_name from student where s_birth like '1990%'
    254.  
       
    255.  
      -- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
    256.  
       
    257.  
      select c_id,ROUND(AVG(s_score),2) as avg_score from score GROUP BY c_id ORDER BY avg_score DESC,c_id ASC
    258.  
       
    259.  
      -- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
    260.  
       
    261.  
      select a.s_id,b.s_name,ROUND(avg(a.s_score),2) as avg_score from score a
    262.  
      left join student b on a.s_id=b.s_id GROUP BY s_id HAVING avg_score>=85
    263.  
       
    264.  
      -- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
    265.  
       
    266.  
      select a.s_name,b.s_score from score b LEFT JOIN student a on a.s_id=b.s_id where b.c_id=(
    267.  
      select c_id from course where c_name ='数学') and b.s_score<60
    268.  
       
    269.  
      -- 35、查询所有学生的课程及分数情况;
    270.  
       
    271.  
       
    272.  
      select a.s_id,a.s_name,
    273.  
      SUM(case c.c_name when '语文' then b.s_score else 0 end) as '语文',
    274.  
      SUM(case c.c_name when '数学' then b.s_score else 0 end) as '数学',
    275.  
      SUM(case c.c_name when '英语' then b.s_score else 0 end) as '英语',
    276.  
      SUM(b.s_score) as '总分'
    277.  
      from student a left join score b on a.s_id = b.s_id
    278.  
      left join course c on b.c_id = c.c_id
    279.  
      GROUP BY a.s_id,a.s_name
    280.  
       
    281.  
       
    282.  
      -- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
    283.  
      select a.s_name,b.c_name,c.s_score from course b left join score c on b.c_id = c.c_id
    284.  
      left join student a on a.s_id=c.s_id where c.s_score>=70
    285.  
       
    286.  
       
    287.  
       
    288.  
      -- 37、查询不及格的课程
    289.  
      select a.s_id,a.c_id,b.c_name,a.s_score from score a left join course b on a.c_id = b.c_id
    290.  
      where a.s_score<60
    291.  
       
    292.  
      --38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
    293.  
      select a.s_id,b.s_name from score a LEFT JOIN student b on a.s_id = b.s_id
    294.  
      where a.c_id = '01' and a.s_score>80
    295.  
       
    296.  
      -- 39、求每门课程的学生人数
    297.  
      select count(*) from score GROUP BY c_id;
    298.  
       
    299.  
      -- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
    300.  
       
    301.  
       
    302.  
      -- 查询老师id
    303.  
      select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三'
    304.  
      -- 查询最高分(可能有相同分数)
    305.  
      select MAX(s_score) from score where c_id='02'
    306.  
      -- 查询信息
    307.  
      select a.*,b.s_score,b.c_id,c.c_name from student a
    308.  
      LEFT JOIN score b on a.s_id = b.s_id
    309.  
      LEFT JOIN course c on b.c_id=c.c_id
    310.  
      where b.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三')
    311.  
      and b.s_score in (select MAX(s_score) from score where c_id='02')
    312.  
       
    313.  
       
    314.  
      -- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
    315.  
      select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score
    316.  
       
    317.  
       
    318.  
      -- 42、查询每门功成绩最好的前两名
    319.  
      -- 牛逼的写法
    320.  
      select a.s_id,a.c_id,a.s_score from score a
    321.  
      where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 ORDER BY a.c_id
    322.  
       
    323.  
       
    324.  
      -- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
    325.  
      select c_id,count(*) as total from score GROUP BY c_id HAVING total>5 ORDER BY total,c_id ASC
    326.  
       
    327.  
      -- 44、检索至少选修两门课程的学生学号
    328.  
      select s_id,count(*) as sel from score GROUP BY s_id HAVING sel>=2
    329.  
       
    330.  
      -- 45、查询选修了全部课程的学生信息
    331.  
      select * from student where s_id in(
    332.  
      select s_id from score GROUP BY s_id HAVING count(*)=(select count(*) from course))
    333.  
       
    334.  
       
    335.  
      --46、查询各学生的年龄
    336.  
      -- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
    337.  
       
    338.  
      select s_birth,(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y') -
    339.  
      (case when DATE_FORMAT(NOW(),'%m%d')>DATE_FORMAT(s_birth,'%m%d') then 0 else 1 end)) as age
    340.  
      from student;
    341.  
       
    342.  
       
    343.  
      -- 47、查询本周过生日的学生
    344.  
      select * from student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))=WEEK(s_birth)
    345.  
      select * from student where YEARWEEK(s_birth)=YEARWEEK(DATE_FORMAT(NOW(),'%Y%m%d'))
    346.  
       
    347.  
      select WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))
    348.  
       
    349.  
      -- 48、查询下周过生日的学生
    350.  
      select * from student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =WEEK(s_birth)
    351.  
       
    352.  
      -- 49、查询本月过生日的学生
    353.  
       
    354.  
      select * from student where MONTH(DATE_FORMAT(NOW(),'%Y%m%d')) =MONTH(s_birth)
    355.  
       
    356.  
      -- 50、查询下月过生日的学生
    357.  
      select * from student where MONTH(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =MONTH(s_birth)

posted on 2019-03-15 18:33  窮山霧繞(静妙)  阅读(149)  评论(0编辑  收藏  举报

导航