sql语句练习50题

sql语句练习50题

00 相关参考资料

MySQL 函数

试题

MySQL教程之concat以及group_concat的用法

01 表和测试数据

CREATE TABLE `course`  (
`c_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`c_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`t_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`c_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '课程表' ROW_FORMAT = Dynamic;

INSERT INTO `course` VALUES ('01', '语文', '02');
INSERT INTO `course` VALUES ('02', '数学', '01');
INSERT INTO `course` VALUES ('03', '英语', '03');
INSERT INTO `course` VALUES ('04', '化学', '01');

CREATE TABLE `score` (
`s_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`c_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`s_score` int(3) NULL DEFAULT NULL,
PRIMARY KEY (`s_id`, `c_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '成绩表' ROW_FORMAT = Dynamic;

INSERT INTO `score` VALUES ('01', '01', 80);
INSERT INTO `score` VALUES ('01', '02', 56);
INSERT INTO `score` VALUES ('01', '03', 99);
INSERT INTO `score` VALUES ('01', '04', 80);
INSERT INTO `score` VALUES ('02', '01', 70);
INSERT INTO `score` VALUES ('02', '02', 59);
INSERT INTO `score` VALUES ('02', '03', 80);
INSERT INTO `score` VALUES ('03', '01', 80);
INSERT INTO `score` VALUES ('03', '02', 80);
INSERT INTO `score` VALUES ('03', '03', 80);
INSERT INTO `score` VALUES ('04', '01', 50);
INSERT INTO `score` VALUES ('04', '02', 80);
INSERT INTO `score` VALUES ('04', '03', 20);
INSERT INTO `score` VALUES ('05', '02', 80);
INSERT INTO `score` VALUES ('05', '03', 87);
INSERT INTO `score` VALUES ('06', '02', 58);
INSERT INTO `score` VALUES ('06', '04', 99);
INSERT INTO `score` VALUES ('07', '01', 58);
INSERT INTO `score` VALUES ('07', '03', 98);

CREATE TABLE `student` (
`s_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`s_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`s_birth` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`s_sex` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
PRIMARY KEY (`s_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '学生表' ROW_FORMAT = Dynamic;

INSERT INTO `student` VALUES ('01', '赵雷', '1990-01-01', '男');
INSERT INTO `student` VALUES ('02', '郑竹', '1990-12-21', '男');
INSERT INTO `student` VALUES ('03', '孙风', '2000-04-12', '男');
INSERT INTO `student` VALUES ('04', '李云', '2002-04-16', '男');
INSERT INTO `student` VALUES ('05', '周梅', '2003-04-17', '女');
INSERT INTO `student` VALUES ('06', '吴兰', '2008-04-11', '女');
INSERT INTO `student` VALUES ('07', '郑竹', '1989-05-01', '女');
INSERT INTO `student` VALUES ('08', '王菊', '1990-01-20', '女');
INSERT INTO `student` VALUES ('09', '王菊', '1998-01-20', '女');

CREATE TABLE `teacher` (
`t_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`t_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
PRIMARY KEY (`t_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '教师表' ROW_FORMAT = Dynamic;

INSERT INTO `teacher` VALUES ('01', '张三');
INSERT INTO `teacher` VALUES ('02', '李四');
INSERT INTO `teacher` VALUES ('03', '王五');
INSERT INTO `teacher` VALUES ('04', '李师师');

02 试题

1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数(03版)
SELECT
a.*,
b.s_score AS '01',
c.s_score AS '02'
FROM
student a,
score b,
score c
WHERE
a.s_id = b.s_id
AND a.s_id = c.s_id
AND b.c_id = '01'
AND c.c_id = '02'
AND b.s_score > c.s_score;

-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数(02版)
SELECT
a.*,
b.s_score AS '01',
c.s_score AS '02'
FROM
student a
INNER JOIN score b ON a.s_id = b.s_id
AND b.c_id = '01'
INNER JOIN score c ON b.s_id = c.s_id
AND c.c_id = '02'
WHERE
b.s_score > c.s_score;

-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数(01版)
SELECT
a.*,
b.s_score AS '01',
c.s_score AS '02'
FROM
student a
INNER JOIN ( SELECT s_id, s_score FROM score WHERE c_id = '01' ) b ON a.s_id = b.s_id
INNER JOIN ( SELECT s_id, s_score FROM score WHERE c_id = '02' ) c ON b.s_id = c.s_id
WHERE
b.s_score > c.s_score;

 

2、查询平均成绩大于60分的学生的学号和平均成绩
-- 2、查询平均成绩大于60分的学生的学号和平均成绩
SELECT
s_id,
round( avg( s_score ), 2 ) AS avg_score
FROM
score
GROUP BY
s_id
HAVING
avg_score > 60;

ROUND() 函数

在mysql中,round函数用于数据的四舍五入,它有两种形式:

1、round(x,d) ,x指要处理的数,d是指保留几位小数

 

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

注意是所有同学

-- 3、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT
a.s_id AS '编号',
a.s_name AS '姓名',
count( b.s_id ) AS '选课总数',
IFNULL( sum( b.s_score ), 0 ) AS '总成绩'
FROM
student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY
a.s_id;

IFNULL() 函数

IFNULL() 函数用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值。

IFNULL() 函数语法格式为:

IFNULL(expression, alt_value)

 

4、查询"李"姓老师的数量
-- 4、查询"李"姓老师的数量 
SELECT
count(*)
FROM
teacher
WHERE
t_name LIKE '李%';
5、查询学过"张三"老师授课的同学的信息
-- 5、查询学过"张三"老师授课的同学的信息 
SELECT
d.*
FROM
teacher a
INNER JOIN course b ON a.t_id = b.t_id
AND a.t_name = '张三'
INNER JOIN score c ON b.c_id = c.c_id
INNER JOIN student d ON c.s_id = d.s_id GROUP BY d.s_id
6、查询没学过"张三"老师授课的同学的信息
-- 6、查询没学过"张三"老师授课的同学的信息 
SELECT
*
FROM
student
WHERE
s_id NOT IN (
SELECT DISTINCT
d.s_id
FROM
teacher a
INNER JOIN course b ON a.t_id = b.t_id
AND a.t_name = '张三'
INNER JOIN score c ON b.c_id = c.c_id
INNER JOIN student d ON c.s_id = d.s_id)

 

7、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

求交集一般用inner join

-- 7、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT
c.*
FROM
( SELECT * FROM score WHERE c_id = '01' ) a
INNER JOIN score b ON a.s_id = b.s_id
AND b.c_id = '02'
INNER JOIN student c ON a.s_id = c.s_id;

 

SELECT
*
FROM
student
WHERE
s_id IN ( SELECT s_id FROM score WHERE c_id = '01' )
AND s_id IN ( SELECT s_id FROM score WHERE c_id = '02' );

 

8、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
-- 8、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT
c.*
FROM
( SELECT * FROM score WHERE c_id = '01' ) a
INNER JOIN student c ON a.s_id = c.s_id
WHERE
c.s_id NOT IN ( SELECT s_id FROM score WHERE c_id = '02' );

 

SELECT
*
FROM
student
WHERE
s_id IN ( SELECT s_id FROM score WHERE c_id = '01' )
AND s_id NOT IN ( SELECT s_id FROM score WHERE c_id = '02' );
9、查询学全所有课程的同学的信息

统计函数不分组也能用,统计函数后不要留空格

-- 9、查询学全所有课程的同学的信息 
select a.* from student a inner join (select s_id,count(c_id)as num from score group by s_id having num=(select count(*) from course ))b on a.s_id=b.s_id

 

-- 9、查询学全所有课程的同学的信息
SELECT * FROM student WHERE s_id IN ( SELECT s_id FROM score GROUP BY s_id HAVING count( c_id )=( SELECT count(*) FROM course ))

 

10、查询至少有一门课与学号为"07"的同学所学相同的同学的信息
-- 10、查询至少有一门课与学号为"07"的同学所学相同的同学的信息 
select * from student where s_id in(select distinct s_id from score where c_id in(select c_id from score where s_id='07') and s_id!='07')
11、查询和"07"号的同学学习的课程完全相同的其他同学的信息
-- 11、查询和"01"号的同学学习的课程完全相同的其他同学的信息 
-- 1>查询07同学没有学过的课程
select c_id from course where c_id not in ( SELECT c_id FROM score WHERE s_id = '07' );
-- 2>找出学过07同学没有学过的课程的同学
select distinct s_id from score where c_id in (select c_id from course where c_id not in ( SELECT c_id FROM score WHERE s_id = '07' ));
-- 3> 排除第二步的同学并且剩下同学学的课程数和07相同,就是最终结果
select * from student where s_id in (select s_id from score where s_id not in (select distinct s_id from score where c_id in (select c_id from course where c_id not in ( SELECT c_id FROM score WHERE s_id = '07' ))) and s_id!='07' group by s_id having count(c_id)=(select count(*) from score where s_id = '07' ));

优解

-- 11、查询和"01"号的同学学习的课程完全相同的其他同学的信息 
select * from student where s_id  in ( select s_id from score where s_id!='07' group by s_id having group_concat(c_id order by c_id) = (select group_concat(c_id order by c_id) from score where s_id='07' group by s_id) );

group_concat()函数

1、功能:将group by产生的同一个分组中的值连接起来,返回一个字符串结果。

2、语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator '分隔符'] )

说明:通过使用distinct可以排除重复值;如果希望对结果中的值进行排序,可以使用order by子句;separator是一个字符串值,缺省为一个逗号。

 

注意:必须在group by后使用,效果将分组后的指定字段拼接成字符串,连接符默认为逗号

12、查询没学过"张三"老师讲授的任一门课程的学生姓名
-- 12、查询没学过"张三"老师讲授的任一门课程的学生姓名 
select s_name from student where s_id not in( select distinct s_id from score where c_id in( select b.c_id  from teacher a inner join course b on a.t_id=b.t_id and a.t_name='张三') group by s_id);

 

13、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
-- 13、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 
select a.s_id,b.s_name,a.avg_score from (select s_id,round(avg(s_score),2)as avg_score from score where s_score<60 group by s_id having count(c_id)>=2) a inner join student b on a.s_id=b.s_id;

 

14、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
-- 14、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT
a.s_name,
b.*
FROM
student a
LEFT JOIN (
SELECT
s_id,
max( CASE WHEN c_id = '01' THEN s_score ELSE NULL END ) AS '01',
max( CASE WHEN c_id = '02' THEN s_score ELSE NULL END ) AS '02',
max( CASE WHEN c_id = '03' THEN s_score ELSE NULL END ) AS '03',
max( CASE WHEN c_id = '04' THEN s_score ELSE NULL END ) AS '04',
round ( avg( s_score ), 2 ) AS avg_score
FROM
score
GROUP BY
s_id
) b ON a.s_id = b.s_id
ORDER BY
b.avg_score DESC

 

15.查询各科成绩最高分、最低分和平均分

查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率,及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

SELECT
a.c_id '课程ID',
c_name '课程name',
max( s_score ) '最高分',
min( s_score ) '最低分',
round( avg( s_score ), 2 ) '平均分',
concat( round( sum( CASE WHEN s_score >= 60 THEN 1 ELSE 0 END )/ count( 1 )* 100, 2 ), '%' ) '及格率',
concat(
round( sum( CASE WHEN s_score >= 70 AND s_score < 80 THEN 1 ELSE 0 END )/ count( 1 )* 100, 2 ),
'%'
) '中等率',
concat(
round( sum( CASE WHEN s_score >= 80 AND s_score < 90 THEN 1 ELSE 0 END )/ count( 1 )* 100, 2 ),
'%'
) '优良率',
concat( round( sum( CASE WHEN s_score >= 90 THEN 1 ELSE 0 END )/ count( 1 )* 100, 2 ), '%' ) '优秀率'
FROM
score a
INNER JOIN course b ON a.c_id = b.c_id
GROUP BY
a.c_id,
b.c_name;

注意:关联查询时,多表共有的字段返回时需要指定表,如c_id为score和course共有的字段,返回时指定a.c_id

16、按各科成绩进行排序,并显示排名(未练习)
-- 16、按各科成绩进行排序,并显示排名
select a.s_id,a.c_id,
      @i:=@i +1 as i保留排名,
      @k:=(case when @score=a.s_score then @k else @i end) as rank不保留排名,
      @score:=a.s_score as score
   from (
       select s_id,c_id,s_score from score GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s;
17、查询学生的总成绩并进行排名(未练习)
-- 17、查询学生的总成绩并进行排名
select a.s_id,
@i:=@i+1 as i,
@k:=(case when @score=a.sum_score then @k else @i end) as rank,
@score:=a.sum_score as score
from (select s_id,SUM(s_score) as sum_score from score GROUP BY s_id ORDER BY sum_score DESC)a,
(select @k:=0,@i:=0,@score:=0)s;
18、查询不同老师所教不同课程平均分从高到低显示
-- 21、查询不同老师所教不同课程平均分从高到低显示 
SELECT
a.t_id,
t_name,
c.c_id,
c_name,
round ( avg( s_score ), 2 ) avg_score
FROM
teacher a
LEFT JOIN course b ON a.t_id = b.t_id
LEFT JOIN score c ON b.c_id = c.c_id
GROUP BY
c.c_id,
a.t_id
ORDER BY
avg_score DESC;
19、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩(未练习)
-- 19、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select d.*,c.排名,c.s_score,c.c_id from (
               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'  
ORDER BY a.s_score DESC  
          )c
          left join student d on c.s_id=d.s_id
           where 排名 BETWEEN 2 AND 3
           UNION
           select d.*,c.排名,c.s_score,c.c_id from (
               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'  
ORDER BY a.s_score DESC
          )c
          left join student d on c.s_id=d.s_id
           where 排名 BETWEEN 2 AND 3
           UNION
           select d.*,c.排名,c.s_score,c.c_id from (
               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'
ORDER BY a.s_score DESC
          )c
          left join student d on c.s_id=d.s_id
           where 排名 BETWEEN 2 AND 3;
20、统计各科成绩各分数段人数
-- 20、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],(85-70],(70-60],(0-60]及所占百分比
SELECT
c_id '课程编号',
c_name '课程名称',
c1 '[100-85]',
c2 '(85-70]',
c3 '(70-60]',
c4 '(0-60]',
round( c1 / num, 2 ) '[100-85]百分比',
round( c2 / num, 2 ) '(85-70]百分比',
round( c3 / num, 2 ) '(70-60]百分比',
round( c4 / num, 2 ) '(0-60]百分比',
num '报考人数'
FROM
(
SELECT
a.c_id,
b.c_name,
count( 1 ) num,
sum( CASE WHEN s_score >= 85 AND s_score <= 100 THEN 1 ELSE 0 END ) c1,
sum( CASE WHEN s_score >= 70 AND s_score < 85 THEN 1 ELSE 0 END ) c2,
sum( CASE WHEN s_score >= 60 AND s_score < 70 THEN 1 ELSE 0 END ) c3,
sum( CASE WHEN s_score >= 0 AND s_score < 60 THEN 1 ELSE 0 END ) c4
FROM
score a
INNER JOIN course b ON a.c_id = b.c_id
GROUP BY
a.c_id
) c;
21、查询学生平均成绩及其名次(未练习)
select a.s_id,
@i:=@i+1 as '不保留空缺排名',
@k:=(case when @avg_score=a.avg_s then @k else @i end) as '保留空缺排名',
@avg_score:=avg_s as '平均分'
from (select s_id,ROUND(AVG(s_score),2) as avg_s from score GROUP BY s_id ORDER BY avg_s DESC)a,(select @avg_score:=0,@i:=0,@k:=0)b;
22、查询各科成绩前三名的记录(未练习)
-- 22、查询各科成绩前三名的记录
SELECT
a.s_id,
a.c_id,
a.s_score
FROM
score a
LEFT JOIN score b ON a.c_id = b.c_id
AND a.s_score < b.s_score
GROUP BY
a.s_id,
a.c_id,
a.s_score
HAVING
COUNT( b.s_id )< 3
ORDER BY
a.c_id,
a.s_score DESC
23、查询每门课程被选修的学生数
-- 23、查询每门课程被选修的学生数 
SELECT
a.c_id,
a.c_name,
count( 1 )
FROM
course a
INNER JOIN score b ON a.c_id = b.c_id
GROUP BY
a.c_id;
24、查询出只有两门课程的全部学生的学号和姓名
-- 24、查询出只有两门课程的全部学生的学号和姓名
SELECT
b.s_id,
b.s_name
FROM
score a
INNER JOIN student b ON a.s_id = b.s_id
GROUP BY
a.s_id
HAVING
count( 1 )= 2;
25、查询男生、女生人数查询男生、女生人数
--  25、查询男生、女生人数
SELECT
s_sex,
count( 1 )
FROM
student
GROUP BY
s_sex;
26、查询名字中含有"风"字的学生信息
-- 26、查询名字中含有"风"字的学生信息
SELECT
*
FROM
student
WHERE
s_name LIKE '%风%';
27、查询同名同性学生名单,并统计同名人数
-- 27、查询同名同性学生名单,并统计同名人数
SELECT
s_sex,
s_name,
count( 1 ) '同名人数'
FROM
student
GROUP BY
s_sex,
s_name
HAVING
count( 1 )> 1;
28、查询1990年出生的学生名单
-- 28、查询1990年出生的学生名单
SELECT
*
FROM
student
WHERE
s_birth LIKE '1990%'
29、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
-- 29、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT
c_id,
avg( s_score ) avg_score
FROM
score
GROUP BY
c_id
ORDER BY
avg_score DESC,
c_id ASC;
30、查询平均成绩大于等于80的所有学生的学号、姓名和平均成绩
-- 30、查询平均成绩大于等于80的所有学生的学号、姓名和平均成绩
SELECT
b.s_id,
b.s_name,
round( avg( s_score ), 2 ) avg_score
FROM
score a
INNER JOIN student b ON a.s_id = b.s_id
GROUP BY
b.s_id
HAVING
avg_score >= 80;

 

31、查询所有学生的课程及分数情况;
-- 31、查询所有学生的课程及分数情况; 
SELECT
b.s_id,
a.s_name,
max( CASE WHEN c_id = '01' THEN s_score ELSE NULL END ) '语文',
max( CASE WHEN c_id = '02' THEN s_score ELSE NULL END ) '数学',
max( CASE WHEN c_id = '03' THEN s_score ELSE NULL END ) '英语',
max( CASE WHEN c_id = '04' THEN s_score ELSE NULL END ) '化学'
FROM
student a
INNER JOIN score b ON a.s_id = b.s_id
GROUP BY
b.s_id;
32、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
-- 32、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
SELECT
c.s_name,
a.c_name,
b.s_score,
FRO course a
INNER JOIN score b ON a.c_id = b.c_id
AND s_score >= 70
INNER JOIN student c ON b.s_id = c.s_id;

 

33、查询不及格的课程(略)
34、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
-- 34、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
SELECT
a.s_id,
b.s_name
FROM
score a
INNER JOIN student b ON a.s_id = b.s_id
AND a.c_id = '01'
AND a.s_score >= 80;

 

35、求每门课程的学生人数
-- 35、求每门课程的学生人数
SELECT
a.c_id,
a.c_name,
count( 1 ) '报考人数'
FROM
course a
INNER JOIN score b ON a.c_id = b.c_id
GROUP BY
a.c_id;
36、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
-- 36、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT
d.*,
c.c_id,
b.c_name,
c.s_score
FROM
teacher a
INNER JOIN course b ON a.t_id = b.t_id
AND a.t_name = '张三'
INNER JOIN score c ON b.c_id = c.c_id
INNER JOIN student d ON c.s_id = d.s_id
ORDER BY
c.s_score DESC
LIMIT 1
37、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩(题意不清晰)
38、查询每门功成绩最好的前两名(重点)
-- 38、查询每门功成绩最好的前两名 
SELECT
a.c_id,
d.c_name,
a.s_score,
a.s_id,
c.s_name
FROM
score a
LEFT JOIN score b ON a.c_id = b.c_id
AND a.s_score < b.s_score
INNER JOIN student c ON a.s_id = c.s_id
INNER JOIN course d ON a.c_id = d.c_id
GROUP BY
a.c_id,
a.s_id
HAVING
count( a.c_id )< 2;

这种方法的原理是查询同一门课程中,比自己分数高的学生的数量,第一名比自己高的数量为0,第二名为1,第三名为2.以此类推,所以只要查询count()<2即可

39、统计每门课程的学生选修人数
-- 39、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT
c_id,
count( 1 ) num
FROM
score
GROUP BY
c_id
HAVING
num > 5
ORDER BY
num DESC,
c_id ASC;
40、检索至少选修两门课程的学生学号
-- 40、检索至少选修两门课程的学生学号
SELECT
s_id
FROM
score
GROUP BY
s_id
HAVING
count( c_id )>= 2;
41、查询选修了全部课程的学生信息
-- 41、查询选修了全部课程的学生信息
select * from student where s_id in(select s_id from score group by s_id having count(c_id)=(select count(1) from course));
42、查询各学生的年龄(重点)
-- 42、查询各学生的年龄
-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select s_id,s_name,DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y')-(case when DATE_FORMAT(now(),'%m%d')<DATE_FORMAT(s_birth,'%m%d') then 1 else 0 end) age from student ;
43、查询本周过生日的学生
-- 43、查询本周过生日的学生
SELECT
*
FROM
student
WHERE
WEEK (
DATE_FORMAT( now(), '%Y%m%d' ))= WEEK (
concat(
DATE_FORMAT( now(), '%Y' ),
DATE_FORMAT( s_birth, '-%m-%d' )));

现有问题:周日不算本周,算下周

44、查询下周过生日的学生
-- 44、查询下周过生日的学生
SELECT
*
FROM
student
WHERE
(WEEK (
DATE_FORMAT( now(), '%Y%m%d' ))+1)= (WEEK (
concat(
DATE_FORMAT( now(), '%Y' ),
DATE_FORMAT( s_birth, '-%m-%d' ))));

 

WEEK(d),计算日期 d 是本年的第几个星期,范围是 0 到 53

DATE_FORMAT(d,f),按表达式 f的要求显示日期 d

 

45、查询本月过生日的学生
-- 45、查询本月过生日的学生
SELECT
*
FROM
student
WHERE
DATE_FORMAT( s_birth, '%m' )= DATE_FORMAT(
now(),
'%m');

 

-- 45、查询本月过生日的学生
SELECT
*
FROM
student
WHERE
MONTH ( s_birth )= MONTH (
now());

MONTH(d),返回日期d中的月份值,1 到 12

NOW(),返回当前日期和时间

46、查询下月过生日的学生
-- 46、查询下月过生日的学生
SELECT
*
FROM
student
WHERE
MONTH ( s_birth )= MONTH (
now())+ 1;

 

posted @ 2022-05-08 21:08  进击的小蔡鸟  阅读(236)  评论(0编辑  收藏  举报