HIVE50道经典练习题

来自:https://blog.csdn.net/wangqinyi574110/article/details/106444402/

建表

CREATE TABLE STUDENT(
S_ID STRING,
S_NAME STRING,
S_BIRTH STRING,
S_SEX STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

CREATE TABLE COURSE(
 C_ID STRING,
 C_NAME STRING,
 T_ID STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

CREATE TABLE TEACHER(
T_ID STRING,
T_NAME STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

CREATE TABLE SCORE (
S_ID STRING,
C_ID STRING,
S_SCORE INT
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

数据

vi /export/data/hivedatas/student.csv

01 赵雷 1990-01-01 男
02 钱电 1990-12-21 男
03 孙风 1990-05-20 男
04 李云 1990-08-06 男
05 周梅 1991-12-01 女
06 吴兰 1992-03-01 女
07 郑竹 1989-07-01 女
08 王菊 1990-01-20 女

vi /export/data/hivedatas/course.csv
01    语文    02
02    数学    01
03    英语    03

vi /export/data/hivedatas/teacher.csv
01    张三
02    李四
03    王五

vi /export/data/hivedatas/score.csv
01    01    80
01    02    90
01    03    99
02    01    70
02    02    60
02    03    80
03    01    80
03    02    80
03    03    80
04    01    50
04    02    30
04    03    20
05    01    76
05    02    87
06    01    31
06    03    34
07    02    89
07    03    98

在这里插入图片描述

1.查询"01"课程比"02"课程成绩高的学生的信息及课程分数:

with s1 as (
    select * from SCORE where C_ID='01'
), s2 as (
    select * from SCORE where C_ID='02'
), s3 as (
    select * from STUDENT
)
select s3.*,s1.S_SCORE,s2.S_SCORE from s1,s2,s3 
where s3.S_ID=s1.S_ID and s3.S_ID=s2.S_ID and s1.S_SCORE>s2.S_SCORE;

答案:
with s1 as (
    select * from SCORE where C_ID='01'
), s2 as (
    select * from SCORE where C_ID='02'
), s3 as (
    select st.*,s1.S_SCORE s1sc,s2.S_SCORE s2sc from STUDENT st,s1,s2 
    where st.S_ID=s1.S_ID and st.S_ID=s2.S_ID
)
select * from s3 where s1sc>s2sc;

 +---------+-----------+-------------+----------+----------------+-----------------+--+
| c.s_id  | c.s_name  |  c.s_birth  | c.s_sex  | c.first_score  | c.second_score  |
+---------+-----------+-------------+----------+----------------+-----------------+--+
| 02      | 钱电        | 1990-12-21  | 男        | 70             | 60              |
| 04      | 李云        | 1990-08-06  | 男        | 50             | 30              |
+---------+-----------+-------------+----------+----------------+-----------------+--+

2.查询"01"课程比"02"课程成绩低的学生的信息及课程分数:

with s1 as (
    select * from SCORE where C_ID='01'
), s2 as (
    select * from SCORE where C_ID='02'
), s3 as (
    select st.*,s1.S_SCORE s1sc,s2.S_SCORE s2sc from STUDENT st,s1,s2 
    where st.S_ID=s1.S_ID and st.S_ID=s2.S_ID
)
select * from s3 where s1sc<s2sc;
 +---------+-----------+-------------+----------+----------------+-----------------+--+
| c.s_id  | c.s_name  |  c.s_birth  | c.s_sex  | c.first_score  | c.second_score  |
+---------+-----------+-------------+----------+----------------+-----------------+--+
| 01      | 赵雷        | 1990-01-01  | 男        | 80             | 90              |
| 05      | 周梅        | 1991-12-01  | 女        | 76             | 87              |
+---------+-----------+-------------+----------+----------------+-----------------+--+

3.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩:

select sc.S_ID,S_NAME,round(avg(S_SCORE),2) savg from STUDENT st
right join SCORE sc on st.S_ID=sc.S_ID
group by sc.S_ID,S_NAME having round(avg(S_SCORE),2)>=60;

答案:
WITH A AS (
  SELECT S_ID,AVG(S_SCORE) AS AVG_SCORE FROM SCORE GROUP BY S_ID HAVING AVG_SCORE>=60  
)
SELECT A.S_ID,B.S_NAME,ROUND(A.AVG_SCORE,2) AVG_SCORE FROM A LEFT JOIN STUDENT B ON A.S_ID = B.S_ID;
+---------+-----------+--------+--+
| a.s_id  | b.s_name  |  avg_score|
+---------+-----------+--------+--+
| 01      | 赵雷        | 89.67  |
| 02      | 钱电        | 70.0   |
| 03      | 孙风        | 80.0   |
| 05      | 周梅        | 81.5   |
| 07      | 郑竹        | 93.5   |
+---------+-----------+--------+--+

4.查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩:– (包括有成绩的和无成绩的)

with a as (
    select sc.S_ID,S_NAME,round(if(avg(S_SCORE) is null,0,avg(S_SCORE)),2) savg from STUDENT st
    left join SCORE sc on st.S_ID=sc.S_ID
    group by sc.S_ID,S_NAME
) select * from a where savg<60 order by savg desc;

答案:
SELECT B.S_ID,B.S_NAME,ROUND(IF(AVG(A.S_SCORE) IS NULL,0,AVG(A.S_SCORE)),2) AS AVG_SCORE FROM SCORE A RIGHT JOIN STUDENT B ON A.S_ID = B.S_ID GROUP BY B.S_ID,B.S_NAME HAVING IF(AVG(A.S_SCORE) IS NULL,0,AVG(A.S_SCORE))<60
+---------+-----------+------------+--+
| b.s_id  | b.s_name  | avg_score  |
+---------+-----------+------------+--+
| 04      | 李云        | 33.33      |
| 06      | 吴兰        | 32.5       |
| 08      | 王菊        | 0.0        |
+---------+-----------+------------+--+

5.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩:

select st.S_ID,S_NAME,count(C_ID),sum(if(S_SCORE is null,0,S_SCORE)) from STUDENT st 
left join SCORE sc on st.S_ID=sc.S_ID
group by st.S_ID,S_NAME;

答案:
SELECT S.S_ID,S.S_NAME,SIZE(COLLECT_SET(C.C_ID)) CHANGE_COURSE_SUM,IF(SUM(C.S_SCORE) IS NULL,0, SUM(C.S_SCORE)) SUM_SCORE FROM STUDENT S LEFT JOIN SCORE C ON S.S_ID = C.S_ID GROUP BY S.S_ID,S.S_NAME
+---------+-----------+--------------------+------------+--+
| s.s_id  | s.s_name  | change_course_sum  | sum_score  |
+---------+-----------+--------------------+------------+--+
| 01      | 赵雷        | 3                  | 269        |
| 02      | 钱电        | 3                  | 210        |
| 03      | 孙风        | 3                  | 240        |
| 04      | 李云        | 3                  | 100        |
| 05      | 周梅        | 2                  | 163        |
| 06      | 吴兰        | 2                  | 65         |
| 07      | 郑竹        | 2                  | 187        |
| 08      | 王菊        | 0                  | 0          |
+---------+-----------+--------------------+------------+--+

6.查询"李"姓老师的数量:

select count(*) from TEACHER where T_NAME like '李%';
+------+--+
| COUNT|
+------+--+
| 1    |
+------+--+

7.查询学过"张三"老师授课的同学的信息:

with s1 as (
    select C_ID from TEACHER te right join COURSE co on te.T_ID=co.T_ID
    where T_NAME='张三'
),s2 as (
    select S_ID from SCORE sc,s1 where sc.C_ID=s1.C_ID
) select * from s2 left join STUDENT st on s2.S_ID=st.S_ID;

答案:
WITH T AS(
  SELECT C.C_ID FROM COURSE C LEFT JOIN TEACHER T ON C.T_ID = T.T_ID WHERE T.T_NAME = '张三'
),
T1 AS (
 SELECT DISTINCT S.S_ID FROM T LEFT JOIN SCORE S ON T.C_ID = S.C_ID  
)
SELECT * FROM T1 LEFT JOIN STUDENT S ON S.S_ID = T1.S_ID;
+-----------+-------------+--------------+------------+--+
| stu.s_id  | stu.s_name  | stu.s_birth  | stu.s_sex  |
+-----------+-------------+--------------+------------+--+
| 01        | 赵雷          | 1990-01-01   | 男          |
| 02        | 钱电          | 1990-12-21   | 男          |
| 03        | 孙风          | 1990-05-20   | 男          |
| 04        | 李云          | 1990-08-06   | 男          |
| 05        | 周梅          | 1991-12-01   | 女          |
| 07        | 郑竹          | 1989-07-01   | 女          |
+-----------+-------------+--------------+------------+--+

8.查询没学过"张三"老师授课的同学的信息:

with s1 as (
    select C_ID from TEACHER te right join COURSE co on te.T_ID=co.T_ID
    where T_NAME='张三'
),s2 as (
    select S_ID from SCORE sc,s1 where sc.C_ID=s1.C_ID
) select * from s2 right join STUDENT st on s2.S_ID=st.S_ID where s2.S_ID is null;
+---------+-----------+-------------+----------+--+
| s.s_id  | s.s_name  |  s.s_birth  | s.s_sex  |
+---------+-----------+-------------+----------+--+
| 06      | 吴兰        | 1992-03-01  | 女        |
| 08      | 王菊        | 1990-01-20  | 女        |
+---------+-----------+-------------+----------+--+

9.查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息:

with s1 as (
    select * from SCORE where C_ID='01'
),s2 as (
    select * from SCORE where C_ID='02'
) select st.* from STUDENT st,s1,s2 where st.S_ID=s1.S_ID and st.S_ID=s2.S_ID;

答案:
WITH A AS(
 SELECT * FROM SCORE WHERE C_ID ='01'
),
B AS(
 SELECT * FROM SCORE WHERE C_ID ='02'
)
SELECT STU.* FROM STUDENT STU JOIN B ON B.S_ID = STU.S_ID JOIN A ON A.S_ID = STU.S_ID;
+-----------+-------------+--------------+------------+--+
| stu.s_id  | stu.s_name  | stu.s_birth  | stu.s_sex  |
+-----------+-------------+--------------+------------+--+
| 01        | 赵雷          | 1990-01-01   | 男          |
| 02        | 钱电          | 1990-12-21   | 男          |
| 03        | 孙风          | 1990-05-20   | 男          |
| 04        | 李云          | 1990-08-06   | 男          |
| 05        | 周梅          | 1991-12-01   | 女          |
+-----------+-------------+--------------+------------+--+

10.查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息:

with s1 as (
    select * from SCORE where C_ID="01"
),s2 as (
    select * from SCORE where C_ID="02"
),s3 as (
    select s1.S_ID from s1 left join s2 on s1.S_ID=s2.S_ID where s2.C_ID is null
) select st.* from s3 left join STUDENT st on s3.S_ID=st.S_ID;

答案:
WITH A AS(
 SELECT STU.* FROM SCORE A LEFT JOIN STUDENT STU ON A.S_ID = STU.S_ID WHERE A.C_ID ='01'
),
B AS(
  SELECT STU.* FROM SCORE A LEFT JOIN STUDENT STU ON A.S_ID = STU.S_ID WHERE A.C_ID ='02'
)
SELECT A.* FROM A LEFT JOIN B ON A.S_ID = B.S_ID WHERE B.S_ID IS NULL
+---------+-----------+-------------+----------+--+
| a.s_id  | a.s_name  |  a.s_birth  | a.s_sex  |
+---------+-----------+-------------+----------+--+
| 06      | 吴兰        | 1992-03-01  | 女        |
+---------+-----------+-------------+----------+--+

11.查询没有学全所有课程的同学的信息:

with s1 as (
    select count(*) c from COURSE
), s2 as (
    select st.S_ID sid,s_name sname,s_birth sbirth,s_sex ssex,count(*) cc from STUDENT st 
        left join SCORE sc on sc.S_ID=st.S_ID 
        group by st.S_ID,s_name,s_birth,s_sex
) select sid,sname,sbirth,ssex from s2,s1 where cc<s1.c;

答案:
WITH A AS(
 SELECT COUNT(DISTINCT C_ID) COUNT FROM COURSE
),
B AS (
 SELECT SIZE(COLLECT_SET(SCORE.C_ID)) NUM,STUDENT.S_ID FROM SCORE RIGHT JOIN STUDENT ON SCORE.S_ID = STUDENT.S_ID GROUP BY STUDENT.S_ID
),
C AS (
  SELECT S_ID FROM B,A WHERE A.COUNT != B.NUM
)
SELECT STUDENT.* FROM C ,STUDENT WHERE C.S_ID = STUDENT.S_ID
+---------------+-----------------+------------------+----------------+--+
| student.s_id  | student.s_name  | student.s_birth  | student.s_sex  |
+---------------+-----------------+------------------+----------------+--+
| 05            | 周梅              | 1991-12-01       | 女              |
| 06            | 吴兰              | 1992-03-01       | 女              |
| 07            | 郑竹              | 1989-07-01       | 女              |
| 08            | 王菊              | 1990-01-20       | 女              |
+---------------+-----------------+------------------+----------------+--+

12查询至少有一门课与学号为"01"的同学所学相同的同学的信息:

with s1 as (
    select C_ID cid from SCORE where S_ID='01'
),s2 as (
    select distinct S_ID sid from s1 right join SCORE sc on s1.cid=sc.C_ID where sc.S_ID!='01'
) select st.* from STUDENT st 
        right join s2 on st.S_ID=s2.sid;


答案:
WITH A AS(
  SELECT C_ID FROM SCORE WHERE S_ID ='01' 
),
B AS (
    SELECT DISTINCT S.S_ID  FROM SCORE S LEFT JOIN A ON A.C_ID = S.C_ID WHERE S.S_ID !='01' 
)
SELECT STUDENT.* FROM B LEFT JOIN STUDENT ON B.S_ID = STUDENT.S_ID;
+---------------+-----------------+------------------+----------------+--+
| student.s_id  | student.s_name  | student.s_birth  | student.s_sex  |
+---------------+-----------------+------------------+----------------+--+
| 02            | 钱电              | 1990-12-21       | 男              |
| 03            | 孙风              | 1990-05-20       | 男              |
| 04            | 李云              | 1990-08-06       | 男              |
| 05            | 周梅              | 1991-12-01       | 女              |
| 06            | 吴兰              | 1992-03-01       | 女              |
| 07            | 郑竹              | 1989-07-01       | 女              |
+---------------+-----------------+------------------+----------------+--+

13.查询和"01"号的同学学习的课程完全相同的其他同学的信息:

with s1 as (
    select concat_ws(',',collect_set(C_ID)) c from SCORE where S_ID='01' 
), s2 as (
    select S_ID sid,concat_ws(',',collect_set(C_ID)) cc from SCORE 
    group by S_ID having sid!='01'
), s3 as (
    select sid from s2 left join s1 on s2.cc=s1.c where s2.cc=s1.c 
) 
select st.* from STUDENT st right join s3 on st.S_ID=s3.sid;

答案:
WITH A AS(
  SELECT COLLECT_SET(C_ID) FIRST_C_ID FROM SCORE GROUP BY S_ID HAVING S_ID ='01' 
),
B AS(
 SELECT COLLECT_SET(C_ID) OTHER_C_ID,S_ID FROM SCORE GROUP BY S_ID HAVING S_ID !='01' 
),
C AS (
  SELECT B.S_ID FROM B RIGHT JOIN A ON B.OTHER_C_ID = A.FIRST_C_ID
)
SELECT STUDENT.* FROM C LEFT JOIN STUDENT ON C.S_ID = STUDENT.S_ID;
+---------------+-----------------+------------------+----------------+--+
| student.s_id  | student.s_name  | student.s_birth  | student.s_sex  |
+---------------+-----------------+------------------+----------------+--+
| 02            | 钱电              | 1990-12-21       | 男              |
| 03            | 孙风              | 1990-05-20       | 男              |
| 04            | 李云              | 1990-08-06       | 男              |
+---------------+-----------------+------------------+----------------+--+

------------------ 以下未验证 ----------------------

14.查询没学过"张三"老师讲授的任一门课程的学生姓名:

BiandongdeiMac.local (9:58):
with s1 as (
    select C_ID from COURSE co 
    left join TEACHER te on co.T_ID=te.T_ID 
    where T_NAME='张三'
),s2 as (
    select st.S_ID sid,st.S_NAME sname,st.S_BIRTH sbirth,st.S_SEX ssex,
        collect_set(if(sc.C_ID is null,0,sc.C_ID)) as con from SCORE sc 
        right join STUDENT st on sc.S_ID=st.S_ID group by st.S_ID,st.S_NAME,st.S_BIRTH,st.S_SEX
) select sid,sname,sbirth,ssex from s1,s2 where !array_contains(s2.con,s1.C_ID);

答案:
WITH T AS(
  SELECT C.C_ID FROM COURSE C LEFT JOIN TEACHER T ON C.T_ID = T.T_ID WHERE T.T_NAME = '张三'
),
T1 AS (
 SELECT DISTINCT S.S_ID FROM T LEFT JOIN SCORE S ON T.C_ID = S.C_ID  
)
SELECT S.* FROM T1 RIGHT JOIN STUDENT S ON  S.S_ID = T1.S_ID WHERE T1.S_ID IS NULL;
+---------+-----------+-------------+----------+--+
| s.s_id  | s.s_name  |  s.s_birth  | s.s_sex  |
+---------+-----------+-------------+----------+--+
| 06      | 吴兰        | 1992-03-01  | 女        |
| 08      | 王菊        | 1990-01-20  | 女        |
+---------+-----------+-------------+----------+--+

15.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩:

with s1 as (
    select S_ID,count(*) c,round(avg(S_SCORE),2) a from SCORE where S_SCORE<60 group by S_ID having c>=2
) select st.*,s1.a from s1 left join STUDENT st on s1.S_ID=st.S_ID;

答案:
WITH A AS (
 SELECT S_ID,ROUND(AVG(S_SCORE),2) AVG_SCORE FROM SCORE WHERE S_SCORE<60 GROUP BY S_ID HAVING COUNT(S_ID)>1 
)
SELECT STUDENT.S_ID,STUDENT.S_NAME,A.AVG_SCORE FROM A LEFT JOIN STUDENT ON A.S_ID = STUDENT.S_ID
+---------------+-----------------+--------------+--+
| student.s_id  | student.s_name  | a.avg_score  |
+---------------+-----------------+--------------+--+
| 04            | 李云              | 33.33        |
| 06            | 吴兰              | 32.5         |
+---------------+-----------------+--------------+--+

16.检索"01"课程分数小于60,按分数降序排列的学生信息:

select * from SCORE sc left
 join STUDENT st on sc.S_ID=st.S_ID
 where C_ID='01' and S_SCORE<60 order by S_SCORE desc;

 答案:
 SELECT * FROM SCORE S LEFT JOIN STUDENT STU ON STU.S_ID = S.S_ID WHERE S.C_ID ='01' AND S.S_SCORE < 60 ORDER BY S.S_SCORE DESC ;
+---------+---------+------------+-----------+-------------+--------------+------------+--+
| s.s_id  | s.c_id  | s.s_score  | stu.s_id  | stu.s_name  | stu.s_birth  | stu.s_sex  |
+---------+---------+------------+-----------+-------------+--------------+------------+--+
| 04      | 01      | 50         | 04        | 李云          | 1990-08-06   | 男          |
| 06      | 01      | 31         | 06        | 吴兰          | 1992-03-01   | 女          |
+---------+---------+------------+-----------+-------------+--------------+------------+--+

17.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩:

with s1 as (
    select sc.C_ID cid,C_NAME,S_ID,S_SCORE from COURSE co right join SCORE sc on co.C_ID=sc.C_ID
)   select S_NAME,
        max(case when s1.C_NAME='英语' then s1.S_SCORE else 0 end),
        max(case when s1.C_NAME='数学' then s1.S_SCORE else 0 end),
        max(case when s1.C_NAME='语文' then s1.S_SCORE else 0 end),
        round(if(avg(s1.S_SCORE) is null,0,avg(s1.S_SCORE)),2) sco
    from STUDENT st 
    left join s1 on st.S_ID=s1.S_ID
    group by st.S_ID,S_NAME order by sco desc;

答案:
WITH B AS (
  SELECT C.C_NAME ,S.* FROM COURSE C RIGHT JOIN SCORE S ON S.C_ID = C.C_ID     
)
SELECT S.S_NAME,
MAX(CASE WHEN B.C_NAME='英语' THEN B.S_SCORE ELSE 0 END) AS `英语`,
MAX(CASE WHEN B.C_NAME='数学' THEN B.S_SCORE ELSE 0 END) AS `数学`,
MAX(CASE WHEN B.C_NAME='语文' THEN B.S_SCORE ELSE 0 END) AS `语文`,
ROUND(IF(AVG(B.S_SCORE) IS NULL,0,AVG(B.S_SCORE)),2) AVG_SCORE
FROM B RIGHT JOIN STUDENT S ON S.S_ID = B.S_ID GROUP BY S.S_NAME ORDER BY AVG_SCORE DESC
+-----------+-----+-----+-----+------------+--+
| s.s_name  | 英语  | 数学  | 语文  | avg_score  |
+-----------+-----+-----+-----+------------+--+
| 郑竹        | 98  | 89  | 0   | 93.5       |
| 赵雷        | 99  | 90  | 80  | 89.67      |
| 周梅        | 0   | 87  | 76  | 81.5       |
| 孙风        | 80  | 80  | 80  | 80.0       |
| 钱电        | 80  | 60  | 70  | 70.0       |
| 李云        | 20  | 30  | 50  | 33.33      |
| 吴兰        | 34  | 0   | 31  | 32.5       |
| 王菊        | 0   | 0   | 0   | 0.0        |
+-----------+-----+-----+-----+------------+--+

18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率:

select 
    s.C_ID,
    c.C_NAME,
    max(S_SCORE),
    min(S_SCORE),
    round(avg(S_SCORE),2),
    round(sum(case when S_SCORE>=60 then 1 else 0 end)/count(*),2),
    round(sum(case when S_SCORE>=60 and S_SCORE<80 then 1 else 0 end)/count(*),2),
    round(sum(case when S_SCORE>=80 and S_SCORE<90 then 1 else 0 end)/count(*),2),
    round(sum(case when S_SCORE>=90 then 1 else 0 end)/count(*),2)
from COURSE c
right join SCORE s on c.C_ID=s.C_ID group by s.C_ID,c.C_NAME;

答案:
SELECT S.C_ID,C.C_NAME,MAX(S.S_SCORE) MAX_SCORE,MIN(S.S_SCORE) MIN_SCORE,ROUND(AVG(S.S_SCORE),2) AVG_SCORE ,ROUND(SUM(CASE WHEN S.S_SCORE>=60 THEN 1 ELSE 0 END)/COUNT(*),2) AS `及格率`,
ROUND(SUM(CASE WHEN S.S_SCORE>=60 AND S.S_SCORE<80 THEN 1 ELSE 0 END)/COUNT(*),2) AS `中等率`,
ROUND(SUM(CASE WHEN S.S_SCORE>=80 AND S.S_SCORE<90 THEN 1 ELSE 0 END)/COUNT(*),2) AS `优良率`,
ROUND(SUM(CASE WHEN S.S_SCORE>=90 THEN 1 ELSE 0 END)/COUNT(*),2) AS `优秀率`
FROM SCORE S LEFT JOIN COURSE C ON S.C_ID = C.C_ID  GROUP BY S.C_ID,C.C_NAME;
+---------+-----------+------------+------------+------------+-------+-------+-------+-------+--+
| s.c_id  | c.c_name  | max_score  | min_score  | avg_score  |  及格率  |  中等率  |  优良率  |  优秀率  |
+---------+-----------+------------+------------+------------+-------+-------+-------+-------+--+
| 01      | 语文        | 80         | 31         | 64.5       | 0.67  | 0.33  | 0.33  | 0.0   |
| 02      | 数学        | 90         | 30         | 72.67      | 0.83  | 0.17  | 0.5   | 0.17  |
| 03      | 英语        | 99         | 20         | 68.5       | 0.67  | 0.0   | 0.33  | 0.33  |
+---------+-----------+------------+------------+------------+-------+-------+-------+-------+--+

19.按各科成绩进行排序,并显示排名:

select *,row_number() over(partition by C_ID order by S_SCORE desc) rn from SCORE;
+-------------+-------------+----------------+-----+--+
| score.s_id  | score.c_id  | score.s_score  | cn  |
+-------------+-------------+----------------+-----+--+
| 03          | 01          | 80             | 1   |
| 01          | 01          | 80             | 2   |
| 05          | 01          | 76             | 3   |
| 02          | 01          | 70             | 4   |
| 04          | 01          | 50             | 5   |
| 06          | 01          | 31             | 6   |
| 01          | 02          | 90             | 1   |
| 07          | 02          | 89             | 2   |
| 05          | 02          | 87             | 3   |
| 03          | 02          | 80             | 4   |
| 02          | 02          | 60             | 5   |
| 04          | 02          | 30             | 6   |
| 01          | 03          | 99             | 1   |
| 07          | 03          | 98             | 2   |
| 02          | 03          | 80             | 3   |
| 03          | 03          | 80             | 4   |
| 06          | 03          | 34             | 5   |
| 04          | 03          | 20             | 6   |
+-------------+-------------+----------------+-----+--+

20.查询学生的总成绩并进行排名:

with s1 as (
    select S_ID,sum(S_SCORE) ssc from SCORE group by S_ID
)select st.*,ssc,dense_rank() over(order by ssc desc) from s1 left join STUDENT st on s1.S_ID=st.S_ID;

答案:
WITH T AS (
SELECT S_ID,SUM(S_SCORE) AS SUM_SCORE FROM SCORE GROUP BY S_ID
),
 T1 AS(
 SELECT *,ROW_NUMBER() OVER (ORDER BY SUM_SCORE DESC) CN FROM T
 )
 SELECT STU.*,T1.SUM_SCORE,T1.CN FROM T1 LEFT JOIN STUDENT STU ON T1.S_ID = STU.S_ID
+-----------+-------------+--------------+------------+---------------+--------+--+
| stu.s_id  | stu.s_name  | stu.s_birth  | stu.s_sex  | t1.sum_score  | t1.cn  |
+-----------+-------------+--------------+------------+---------------+--------+--+
| 01        | 赵雷          | 1990-01-01   | 男          | 269           | 1      |
| 03        | 孙风          | 1990-05-20   | 男          | 240           | 2      |
| 02        | 钱电          | 1990-12-21   | 男          | 210           | 3      |
| 07        | 郑竹          | 1989-07-01   | 女          | 187           | 4      |
| 05        | 周梅          | 1991-12-01   | 女          | 163           | 5      |
| 04        | 李云          | 1990-08-06   | 男          | 100           | 6      |
| 06        | 吴兰          | 1992-03-01   | 女          | 65            | 7      |
+-----------+-------------+--------------+------------+---------------+--------+--+

21.查询不同老师所教不同课程平均分从高到低显示:

with s1 as (
    select C_ID cid,te.T_ID tid from TEACHER te 
        right join COURSE co on te.T_ID=co.T_ID
)select s1.tid,sc.C_ID,round(avg(S_SCORE),2) a from s1 
        right join SCORE sc on s1.cid=sc.C_ID
    group by s1.tid,sc.C_ID order by a desc;

答案:
SELECT COURSE.T_ID,SCORE.C_ID,ROUND(AVG(S_SCORE),2) AS AVG_SCORE FROM SCORE LEFT JOIN COURSE ON SCORE.C_ID = COURSE.C_ID GROUP BY COURSE.T_ID,SCORE.C_ID ORDER BY  AVG_SCORE DESC;
+--------------+-------------+------------+--+
| course.t_id  | score.c_id  | avg_score  |
+--------------+-------------+------------+--+
| 01           | 02          | 72.67      |
| 03           | 03          | 68.5       |
| 02           | 01          | 64.5       |
+--------------+-------------+------------+--+

22.查询所有课程的成绩第2名到第3名的学生信息及该课程成绩:

with s1 as (
    select *,dense_rank() over(partition by C_ID order by S_SCORE desc) dr from SCORE
) select * from s1 where dr>=2 or dr<=3;

with s1 as (
    select *,row_number() over(partition by C_ID order by S_SCORE desc) dr from SCORE
) select * from s1 where dr>=2 and dr<=3;

答案:
WITH A AS(
  SELECT *,ROW_NUMBER() OVER(PARTITION BY C_ID ORDER BY S_SCORE DESC  ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) CN  FROM SCORE
)
SELECT * FROM A WHERE CN BETWEEN 2 AND 3;
+---------+---------+------------+-------+--+
| a.s_id  | a.c_id  | a.s_score  | a.cn  |
+---------+---------+------------+-------+--+
| 01      | 01      | 80         | 2     |
| 05      | 01      | 76         | 3     |
| 07      | 02      | 89         | 2     |
| 05      | 02      | 87         | 3     |
| 07      | 03      | 98         | 2     |
| 02      | 03      | 80         | 3     |
+---------+---------+------------+-------+--+

23.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比

select C_ID,
        sum(if(S_SCORE<=100 and S_SCORE>85,1,0)),
        sum(case when S_SCORE<=85 and S_SCORE>70 then 1 else 0 end),
        sum(case when S_SCORE<=70 and S_SCORE>60 then 1 else 0 end),
        sum(case when S_SCORE<=60 then 1 else 0 end),
        round(sum(if(S_SCORE<=100 and S_SCORE>85,1,0))/count(*),2),
        round(sum(case when S_SCORE<=85 and S_SCORE>70 then 1 else 0 end)/count(*),2),
        round(sum(case when S_SCORE<=70 and S_SCORE>60 then 1 else 0 end)/count(*),2),
        round(sum(case when S_SCORE<=60 then 1 else 0 end)/count(*),2)
from SCORE group by C_ID;

答案:
SELECT C_ID ,
SUM((CASE WHEN S_SCORE<=100 AND S_SCORE>85 THEN 1 ELSE 0 END)) AS `[100-85]人数`,
SUM((CASE WHEN S_SCORE<=85 AND S_SCORE>70 THEN 1 ELSE 0 END)) AS `[85-70]人数`,
SUM((CASE WHEN S_SCORE<=70 AND S_SCORE>60 THEN 1 ELSE 0 END)) AS `[70-60]人数`,
SUM((CASE WHEN S_SCORE<=60 AND S_SCORE>0 THEN 1 ELSE 0 END)) AS `[0-60]人数`,
ROUND(SUM((CASE WHEN S_SCORE<=100 AND S_SCORE>85 THEN 1 ELSE 0 END))/COUNT(*),2) AS `[100-85]`,
ROUND(SUM((CASE WHEN S_SCORE<=85 AND S_SCORE>70 THEN 1 ELSE 0 END))/COUNT(*),2) AS `[85-70]`,
ROUND(SUM((CASE WHEN S_SCORE<=70 AND S_SCORE>60 THEN 1 ELSE 0 END))/COUNT(*),2) AS `[70-60]`,
ROUND(SUM((CASE WHEN S_SCORE<=60 AND S_SCORE>0 THEN 1 ELSE 0 END))/COUNT(*),2) AS `[0-60]`
FROM SCORE GROUP BY C_ID
+-----+------------+----------+-----------+----------+--------+-------+-------+------+--+
| c_id|[100-85]人数|[85-70]人数|[70-60]人数|[0-60]人数|[100-85]|[85-70]|[70-60]|[0-60]|
+-----+----- ------+----------+-----------+----------+--------+- -----+-------+------+--+
| 01  |     0      |    3     |     1     |    2     |  0.0   | 0.5   | 0.17  | 0.33 |
| 02  |     3      |    1     |     0     |    2     |  0.5   | 0.17  | 0.0   | 0.33 |
| 03  |     2      |    2     |     0     |    2     | 0.33   | 0.33  | 0.0   | 0.33 |
+-----+------------+----------+-----------+----------+--------+-------+-------+------+--+

24.查询学生平均成绩及其名次:

select st.S_ID,S_NAME,S_BIRTH,S_SEX,round(avg(S_SCORE),2),row_number() over(order by avg(S_SCORE) desc) rn 
from SCORE sc left join STUDENT st on sc.S_ID=st.S_ID 
group by st.S_ID,S_NAME,S_BIRTH,S_SEX
order by rn;

答案:
WITH T AS(
    SELECT  S_ID ,AVG(S_SCORE) AS AVG_SCORE FROM SCORE GROUP BY S_ID
),
T3 AS(
SELECT STU.*,ROUND(CAST(T.AVG_SCORE AS DOUBLE),2) AVG_SCORE FROM STUDENT STU RIGHT JOIN T ON STU.S_ID = T.S_ID
)
SELECT *,ROW_NUMBER() OVER(ORDER BY AVG_SCORE DESC) AS RANK FROM T3;
+----------+------------+-------------+-----------+---------------+-------+--+
| t3.s_id  | t3.s_name  | t3.s_birth  | t3.s_sex  | t3.avg_score  | rank  |
+----------+------------+-------------+-----------+---------------+-------+--+
| 07       | 郑竹         | 1989-07-01  | 女         | 93.5          | 1     |
| 01       | 赵雷         | 1990-01-01  | 男         | 89.67         | 2     |
| 05       | 周梅         | 1991-12-01  | 女         | 81.5          | 3     |
| 03       | 孙风         | 1990-05-20  | 男         | 80.0          | 4     |
| 02       | 钱电         | 1990-12-21  | 男         | 70.0          | 5     |
| 04       | 李云         | 1990-08-06  | 男         | 33.33         | 6     |
| 06       | 吴兰         | 1992-03-01  | 女         | 32.5          | 7     |
+----------+------------+-------------+-----------+---------------+-------+--+

25.查询各科成绩前三名的记录

with s1 as (
    select *,dense_rank() over(partition by C_ID order by S_SCORE desc) ds from SCORE
) select * from s1 where ds<=3;

答案:
WITH T AS(
  SELECT *,ROW_NUMBER() OVER (PARTITION BY C_ID ORDER BY S_SCORE DESC) AS CN FROM SCORE
)
SELECT * FROM T WHERE T.CN = 1;
+---------+---------+------------+-------+--+
| t.s_id  | t.c_id  | t.s_score  | t.cn  |
+---------+---------+------------+-------+--+
| 03      | 01      | 80         | 1     |
| 01      | 02      | 90         | 1     |
| 01      | 03      | 99         | 1     |
+---------+---------+------------+-------+--+

26.查询每门课程被选修的学生数:

select C_ID,count(S_ID) from SCORE group by C_ID;
+-------+------+--+
| c_id  | num  |
+-------+------+--+
| 01    | 6    |
| 02    | 6    |
| 03    | 6    |
+-------+------+--+

27.查询出只有两门课程的全部学生的学号和姓名:

with s1 as (
    select S_ID,count(C_ID) c from SCORE group by S_ID
) select st.S_ID,st.S_NAME from s1 left join STUDENT st on s1.S_ID=st.S_ID where c=2;

答案:
WITH A AS (
 SELECT S_ID ,COUNT(C_ID) AS NUM  FROM SCORE GROUP BY S_ID HAVING NUM = 2
)
SELECT A.S_ID,STUDENT.S_NAME FROM A LEFT JOIN STUDENT ON STUDENT.S_ID  = A.S_ID
+---------+-----------------+--+
| a.s_id  | student.s_name  |
+---------+-----------------+--+
| 05      | 周梅              |
| 06      | 吴兰              |
| 07      | 郑竹              |
+---------+-----------------+--+

28.查询男生、女生人数:

select S_SEX,count(*) from STUDENT group by S_SEX;
答案:
SELECT SIZE(COLLECT_SET(S_ID)) COUNT_NUM,S_SEX FROM STUDENT GROUP BY S_SEX;
+------------+--------+--+
| count_num  | s_sex  |
+------------+--------+--+
| 4          | 女      |
| 4          | 男      |
+------------+--------+--+

29.查询名字中含有"风"字的学生信息:

select * from STUDENT where S_NAME like '%风%';
+---------------+-----------------+------------------+----------------+--+
| student.s_id  | student.s_name  | student.s_birth  | student.s_sex  |
+---------------+-----------------+------------------+----------------+--+
| 03            | 孙风              | 1990-05-20       | 男              |
+---------------+-----------------+------------------+----------------+--+

30.查询同名同性学生名单,并统计同名人数:

select *,count(S_NAME) over(partition by S_NAME) c from STUDENT;
+---------+-----------+------------+----------+-----------------+--+
| a.s_id  | a.s_name  | a.s_birth  | a.s_sex  | a.count_s_name  |
+---------+-----------+------------+----------+-----------------+--+
+---------+-----------+------------+----------+-----------------+--+

31.查询1990年出生的学生名单:

select * from STUDENT where year(to_date(S_BIRTH))='1990';

答案:
SELECT * FROM STUDENT WHERE YEAR(S_BIRTH) = '1990';
+---------------+-----------------+------------------+----------------+--+
| student.s_id  | student.s_name  | student.s_birth  | student.s_sex  |
+---------------+-----------------+------------------+----------------+--+
| 01            | 赵雷              | 1990-01-01       | 男              |
| 02            | 钱电              | 1990-12-21       | 男              |
| 03            | 孙风              | 1990-05-20       | 男              |
| 04            | 李云              | 1990-08-06       | 男              |
| 08            | 王菊              | 1990-01-20       | 女              |
+---------------+-----------------+------------------+----------------+--+

32.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列:

select C_ID,round(avg(S_SCORE),2) a from SCORE group by C_ID order by a desc,C_ID;
答案:
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
+-------+------------+--+
| c_id  | avg_score  |
+-------+------------+--+
| 02    | 72.67      |
| 03    | 68.5       |
| 01    | 64.5       |
+-------+------------+--+

33.查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩:

with s1 as (
    select S_ID,avg(S_SCORE) a from SCORE group by S_ID
)
select st.*,round(a,2) from s1 left join STUDENT st on s1.S_ID=st.S_ID where a>=85;

答案:
WITH T AS(
  SELECT AVG(S_SCORE) AS AVG_SCORE,S_ID  FROM SCORE GROUP BY S_ID HAVING AVG_SCORE>=85
)
SELECT STU.*,ROUND(AVG_SCORE,2) AS AVG_SCORE FROM T LEFT JOIN STUDENT STU ON T.S_ID = STU.S_ID;
+-----------+-------------+--------------+------------+------------+--+
| stu.s_id  | stu.s_name  | stu.s_birth  | stu.s_sex  | avg_score  |
+-----------+-------------+--------------+------------+------------+--+
| 01        | 赵雷          | 1990-01-01   | 男          | 89.67      |
| 07        | 郑竹          | 1989-07-01   | 女          | 93.5       |
+-----------+-------------+--------------+------------+------------+--+

34.查询课程名称为"数学",且分数低于60的学生姓名和分数:

select st.*,S_SCORE from COURSE co 
right join SCORE sc on co.C_ID=sc.C_ID left join STUDENT st on sc.S_ID=st.S_ID
where C_NAME='数学' and S_SCORE<60;

答案:
WITH T AS (
  SELECT S.S_ID ,S.S_SCORE FROM SCORE S LEFT JOIN COURSE C ON C.C_ID = S.C_ID WHERE S.S_SCORE < 60 AND C.C_NAME ='数学'
)
SELECT S.S_NAME,T.S_SCORE FROM T LEFT JOIN STUDENT S ON S.S_ID = T.S_ID
+-----------+------------+--+
| s.s_name  | t.s_score  |
+-----------+------------+--+
| 李云        | 30         |
+-----------+------------+--+

35.查询所有学生的课程及分数情况:

select st.*,C_NAME,S_SCORE from STUDENT st left join SCORE sc on st.S_ID=sc.S_ID;
+---------+-----------+-------------+----------+------------+------------+--+
| s.s_id  | s.s_name  |  s.s_birth  | s.s_sex  | co.c_name  | c.s_score  |
+---------+-----------+-------------+----------+------------+------------+--+
| 01      | 赵雷        | 1990-01-01  | 男        | 语文         | 80         |
| 01      | 赵雷        | 1990-01-01  | 男        | 数学         | 90         |
| 01      | 赵雷        | 1990-01-01  | 男        | 英语         | 99         |
| 02      | 钱电        | 1990-12-21  | 男        | 语文         | 70         |
| 02      | 钱电        | 1990-12-21  | 男        | 数学         | 60         |
| 02      | 钱电        | 1990-12-21  | 男        | 英语         | 80         |
| 03      | 孙风        | 1990-05-20  | 男        | 语文         | 80         |
| 03      | 孙风        | 1990-05-20  | 男        | 数学         | 80         |
| 03      | 孙风        | 1990-05-20  | 男        | 英语         | 80         |
| 04      | 李云        | 1990-08-06  | 男        | 语文         | 50         |
| 04      | 李云        | 1990-08-06  | 男        | 数学         | 30         |
| 04      | 李云        | 1990-08-06  | 男        | 英语         | 20         |
| 05      | 周梅        | 1991-12-01  | 女        | 语文         | 76         |
| 05      | 周梅        | 1991-12-01  | 女        | 数学         | 87         |
| 06      | 吴兰        | 1992-03-01  | 女        | 语文         | 31         |
| 06      | 吴兰        | 1992-03-01  | 女        | 英语         | 34         |
| 07      | 郑竹        | 1989-07-01  | 女        | 数学         | 89         |
| 07      | 郑竹        | 1989-07-01  | 女        | 英语         | 98         |
| 08      | 王菊        | 1990-01-20  | 女        | NULL       | NULL       |
+---------+-----------+-------------+----------+------------+------------+--+

36.查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数:

select st.*,S_SCORE,sc.C_ID,C_NAME from STUDENT st 
left join SCORE sc on st.S_ID=sc.S_ID 
left join COURSE co on sc.C_ID=co.C_ID
where S_SCORE>=70;

答案:
WITH T AS (
  SELECT STU.*,S.S_SCORE,S.C_ID FROM SCORE  S LEFT JOIN STUDENT STU ON STU.S_ID =S.S_ID  WHERE S.S_SCORE >= 70
)
SELECT T.*,C.C_NAME FROM T LEFT JOIN COURSE C ON C.C_ID = T.C_ID ;
+---------+-----------+-------------+----------+------------+---------+-----------+--+
| t.s_id  | t.s_name  |  t.s_birth  | t.s_sex  | t.s_score  | t.c_id  | c.c_name  |
+---------+-----------+-------------+----------+------------+---------+-----------+--+
| 01      | 赵雷        | 1990-01-01  | 男        | 80         | 01      | 语文        |
| 01      | 赵雷        | 1990-01-01  | 男        | 90         | 02      | 数学        |
| 01      | 赵雷        | 1990-01-01  | 男        | 99         | 03      | 英语        |
| 02      | 钱电        | 1990-12-21  | 男        | 70         | 01      | 语文        |
| 02      | 钱电        | 1990-12-21  | 男        | 80         | 03      | 英语        |
| 03      | 孙风        | 1990-05-20  | 男        | 80         | 01      | 语文        |
| 03      | 孙风        | 1990-05-20  | 男        | 80         | 02      | 数学        |
| 03      | 孙风        | 1990-05-20  | 男        | 80         | 03      | 英语        |
| 05      | 周梅        | 1991-12-01  | 女        | 76         | 01      | 语文        |
| 05      | 周梅        | 1991-12-01  | 女        | 87         | 02      | 数学        |
| 07      | 郑竹        | 1989-07-01  | 女        | 89         | 02      | 数学        |
| 07      | 郑竹        | 1989-07-01  | 女        | 98         | 03      | 英语        |
+---------+-----------+-------------+----------+------------+---------+-----------+--+

37.查询课程不及格的学生:

select st.*,S_SCORE,sc.C_ID,C_NAME from STUDENT st 
left join SCORE sc on st.S_ID=sc.S_ID 
left join COURSE co on sc.C_ID=co.C_ID
where S_SCORE<60;

答案:
WITH T AS (
  SELECT STU.*,S.S_SCORE,S.C_ID FROM SCORE  S LEFT JOIN STUDENT STU ON STU.S_ID =S.S_ID  WHERE S.S_SCORE < 60
)
SELECT T.*,C.C_NAME FROM T LEFT JOIN COURSE C ON C.C_ID = T.C_ID ;
+---------+-----------+-------------+----------+------------+---------+-----------+--+
| t.s_id  | t.s_name  |  t.s_birth  | t.s_sex  | t.s_score  | t.c_id  | c.c_name  |
+---------+-----------+-------------+----------+------------+---------+-----------+--+
| 04      | 李云        | 1990-08-06  | 男        | 50         | 01      | 语文        |
| 04      | 李云        | 1990-08-06  | 男        | 30         | 02      | 数学        |
| 04      | 李云        | 1990-08-06  | 男        | 20         | 03      | 英语        |
| 06      | 吴兰        | 1992-03-01  | 女        | 31         | 01      | 语文        |
| 06      | 吴兰        | 1992-03-01  | 女        | 34         | 03      | 英语        |
+---------+-----------+-------------+----------+------------+---------+-----------+--+

38.查询课程编号为01且课程成绩在80分以上的学生的学号和姓名:

select st.S_ID,S_NAME from STUDENT st 
left join SCORE sc on st.S_ID=sc.S_ID
where C_ID='01' and S_SCORE>=80;

答案:
WITH T AS(
  SELECT S_ID FROM SCORE WHERE C_ID ='01' AND S_SCORE >= 80
)
SELECT T.S_ID,STU.S_NAME FROM T LEFT JOIN STUDENT STU ON STU.S_ID = T.S_ID
+---------+-------------+--+
| t.s_id  | stu.s_name  |
+---------+-------------+--+
| 01      | 赵雷          |
| 03      | 孙风          |
+---------+-------------+--+

39.求每门课程的学生人数:

select co.C_ID,C_NAME,count(co.C_ID) cn from COURSE co 
right join SCORE sc on co.C_ID=sc.C_ID
group by co.C_ID,C_NAME;

答案:
WITH T AS (
  SELECT SIZE(COLLECT_SET(S_ID)) AS CN,C_ID FROM SCORE GROUP BY C_ID 
)
SELECT C.C_ID,C.C_NAME,T.CN FROM T LEFT JOIN COURSE C ON C.C_ID = T.C_ID;
+---------+-----------+-------+--+
| c.c_id  | c.c_name  | t.cn  |
+---------+-----------+-------+--+
| 01      | 语文        | 6     |
| 02      | 数学        | 6     |
| 03      | 英语        | 6     |
+---------+-----------+-------+--+

40.查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩:



+-----------+-------------+--------------+------------+-------------+--+
| stu.s_id  | stu.s_name  | stu.s_birth  | stu.s_sex  | t1.s_score  |
+-----------+-------------+--------------+------------+-------------+--+
| 01        | 赵雷          | 1990-01-01   | 男          | 90          |
+-----------+-------------+--------------+------------+-------------+--+

41.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩:



+---------+---------+------------+--+
| b.s_id  | b.c_id  | b.s_score  |
+---------+---------+------------+--+
| 01      | 01      | 80         |
| 02      | 03      | 80         |
| 03      | 01      | 80         |
| 03      | 02      | 80         |
| 03      | 03      | 80         |
+---------+---------+------------+--+

42.查询每门课程成绩最好的前三名:



+----------+------------+-------------+-----------+----------+-----------+--+
| t1.s_id  | t1.s_name  | t1.s_birth  | t1.s_sex  | t1.c_id  | c.c_name  |
+----------+------------+-------------+-----------+----------+-----------+--+
| 03       | 孙风         | 1990-05-20  | 男         | 01       | 语文        |
| 01       | 赵雷         | 1990-01-01  | 男         | 01       | 语文        |
| 05       | 周梅         | 1991-12-01  | 女         | 01       | 语文        |
| 01       | 赵雷         | 1990-01-01  | 男         | 02       | 数学        |
| 07       | 郑竹         | 1989-07-01  | 女         | 02       | 数学        |
| 05       | 周梅         | 1991-12-01  | 女         | 02       | 数学        |
| 01       | 赵雷         | 1990-01-01  | 男         | 03       | 英语        |
| 07       | 郑竹         | 1989-07-01  | 女         | 03       | 英语        |
| 02       | 钱电         | 1990-12-21  | 男         | 03       | 英语        |
+----------+------------+-------------+-----------+----------+-----------+--+

43.统计每门课程的学生选修人数(超过5人的课程才统计):– 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列



+-------+---------+--+
| t.cn  | t.c_id  |
+-------+---------+--+
| 6     | 01      |
| 6     | 02      |
| 6     | 03      |
+-------+---------+--+

44.检索至少选修两门课程的学生学号:



+-------+-----+--+
| s_id  | cn  |
+-------+-----+--+
| 01    | 3   |
| 02    | 3   |
| 03    | 3   |
| 04    | 3   |
| 05    | 2   |
| 06    | 2   |
| 07    | 2   |
+-------+-----+--+

45.查询选修了全部课程的学生信息:



+-----------+-------------+--------------+------------+--+
| stu.s_id  | stu.s_name  | stu.s_birth  | stu.s_sex  |
+-----------+-------------+--------------+------------+--+
| 01        | 赵雷          | 1990-01-01   | 男          |
| 02        | 钱电          | 1990-12-21   | 男          |
| 03        | 孙风          | 1990-05-20   | 男          |
| 04        | 李云          | 1990-08-06   | 男          |
+-----------+-------------+--------------+------------+--+

46.查询各学生的年龄(周岁):

– 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一



+---------------+-----------------+------------------+----------------+------+--+
| student.s_id  | student.s_name  | student.s_birth  | student.s_sex  | age  |
+---------------+-----------------+------------------+----------------+------+--+
| 01            | 赵雷             | 1990-01-01      | 男              | 30   |
| 02            | 钱电             | 1990-12-21      | 男              | 29   |
| 03            | 孙风             | 1990-05-20      | 男              | 30   |
| 04            | 李云             | 1990-08-06      | 男              | 29   |
| 05            | 周梅             | 1991-12-01      | 女              | 28   |
| 06            | 吴兰             | 1992-03-01      | 女              | 28   |
| 07            | 郑竹             | 1989-07-01      | 女              | 30   |
| 08            | 王菊             | 1990-01-20      | 女              | 30   |
+---------------+-----------------+------------------+----------------+------+--+

47.查询本周过生日的学生:



+---------------+-----------------+------------------+----------------+--+
| student.s_id  | student.s_name  | student.s_birth  | student.s_sex  |
+---------------+-----------------+------------------+----------------+--+
+---------------+-----------------+------------------+----------------+--+

48.查询下周过生日的学生:



+---------------+-----------------+------------------+----------------+--+
| student.s_id  | student.s_name  | student.s_birth  | student.s_sex  |
+---------------+-----------------+------------------+----------------+--+
+---------------+-----------------+------------------+----------------+--+

49.查询本月过生日的学生:



+---------------+-----------------+------------------+----------------+--+
| student.s_id  | student.s_name  | student.s_birth  | student.s_sex  |
+---------------+-----------------+------------------+----------------+--+
| 03            | 孙风              | 1990-05-20       | 男              |
+---------------+-----------------+------------------+----------------+--+

50.查询12月份过生日的学生:



+---------------+-----------------+------------------+----------------+--+
| student.s_id  | student.s_name  | student.s_birth  | student.s_sex  |
+---------------+-----------------+------------------+----------------+--+
| 02            | 钱电              | 1990-12-21       | 男              |
| 05            | 周梅              | 1991-12-01       | 女              |
+---------------+-----------------+------------------+----------------+--+

results matching ""

    No results matching ""