首页 > 试题广场 >

得分不小于平均分的最低分

[编程题]得分不小于平均分的最低分
  • 热度指数:165827 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
请从试卷作答记录表中找到SQL试卷得分不小于该类试卷平均得分的用户最低得分。
示例数据 exam_record表(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分):
id uid exam_id start_time submit_time
score
1 1001 9001 2020-01-02 09:01:01 2020-01-02 09:21:01
80
2 1002
9001
2021-09-05 19:01:01 2021-09-05 19:40:01
89
3 1002 9002
2021-09-02 12:01:01
(NULL)
(NULL)
4 1002 9003
2021-09-01 12:01:01
(NULL) (NULL)
5 1002
9001 2021-02-02 19:01:01 2021-02-02 19:30:01
87
6 1002
9002
2021-05-05 18:01:01 2021-05-05 18:59:02
90
7 1003 9002
2021-02-06 12:01:01
(NULL)
(NULL)
8 1003 9003
2021-09-07 10:01:01
2021-09-07 10:31:01 86
9
1004 9003
2021-09-06 12:01:01 (NULL)
(NULL)

examination_info表(exam_id试卷ID, tag试卷类别, difficulty试卷难度, duration考试时长, release_time发布时间)
id exam_id tag difficulty
duration release_time
1 9001 SQL hard 60 2020-01-01 10:00:00
2
9002
SQL
easy 60 2020-02-01 10:00:00
3 9003
算法 medium 80 2020-08-02 10:00:00

示例输出数据:
min_score_over_avg
87

解释:试卷9001和9002为SQL类别,作答这两份试卷的得分有[80,89,87,90],平均分为86.5,不小于平均分的最小分数为87
示例1

输入

drop table if exists examination_info;
CREATE TABLE  examination_info (
    id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    exam_id int UNIQUE NOT NULL COMMENT '试卷ID',
    tag varchar(32) COMMENT '类别标签',
    difficulty varchar(8) COMMENT '难度',
    duration int NOT NULL COMMENT '时长',
    release_time datetime COMMENT '发布时间'
)CHARACTER SET utf8 COLLATE utf8_general_ci;

drop table if exists exam_record;
CREATE TABLE  exam_record (
    id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    uid int NOT NULL COMMENT '用户ID',
    exam_id int NOT NULL COMMENT '试卷ID',
    start_time datetime NOT NULL COMMENT '开始时间',
    submit_time datetime COMMENT '提交时间',
    score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;

INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES
  (9001, 'SQL', 'hard', 60, '2020-01-01 10:00:00'),
  (9002, 'SQL', 'easy', 60, '2020-02-01 10:00:00'),
  (9003, '算法', 'medium', 80, '2020-08-02 10:00:00');

INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES
(1001, 9001, '2020-01-02 09:01:01', '2020-01-02 09:21:01', 80),
(1002, 9001, '2021-09-05 19:01:01', '2021-09-05 19:40:01', 89),
(1002, 9002, '2021-09-02 12:01:01', null, null),
(1002, 9003, '2021-09-01 12:01:01', null, null),
(1002, 9001, '2021-02-02 19:01:01', '2021-02-02 19:30:01', 87),
(1002, 9002, '2021-05-05 18:01:01', '2021-05-05 18:59:02', 90),
(1003, 9002, '2021-02-06 12:01:01', null, null),
(1003, 9003, '2021-09-07 10:01:01', '2021-09-07 10:31:01', 86),
(1004, 9003, '2021-09-06 12:01:01', null, null);

输出

87

备注:
保证至少有一个有效的SQL类别的试卷作答分数
select
    min(score) min_score_over_avg
from
    (select
        score,
        avg(score) over(partition by tag) avg_score
    from
        exam_record er
    join
        examination_info ei
    on
        er.exam_id = ei.exam_id
    where
        ei.tag = "SQL" and score is not null)t1
where
    score >= avg_score;

发表于 2024-04-28 10:43:07 回复(0)
select
min(score) as min_score_over_avg
from exam_record a join examination_info b on a.exam_id = b.exam_id
where b.tag = 'SQL'
and score >=(
            select avg(score)
            from exam_record a join examination_info b on a.exam_id =
            b.exam_id
            where tag = 'SQL'
)

编辑于 2024-04-24 10:36:36 回复(1)
select min(A.score) as min_score_over_avg
from
     (select exam_record.exam_id, score
     from exam_record
     left join examination_info
     on examination_info.exam_id = exam_record.exam_id
     where tag = 'SQL' and score is not NULL) as A
where min_score_over_avg >= sum(A.score)/COUNT(A.score)
;
为什么这样显示错误呢?
编辑于 2024-04-12 20:15:25 回复(0)
-- 先连接表,再筛选数据
with t1 as (
    select a.exam_id, a.score
    from exam_record a
    inner join examination_info b using(exam_id)
    where tag = "SQL"
)
select score as min_score_over_avg
from t1
where score >= (
    select avg(score)
    from t1
)
order by score
limit 1;

编辑于 2024-03-15 12:23:36 回复(0)
select 
score as min_score_over_avg
from exam_record er
inner join examination_info ei
on er.exam_id = ei.exam_id
where tag = 'SQL' and score >= (
    select avg(score)
    from from exam_record e_r
    inner join examination_info e_i
    on e_r.exam_id = e_i.exam_id
    where tag = 'SQL'
)
order by score
limit 1;这个代码有什么问题??
发表于 2024-03-08 19:35:13 回复(0)
with tmp as (
select 
    t1.exam_id
    ,t1.score
from 
    exam_record t1
left join 
    examination_info t2 
on t1.exam_id = t2.exam_id
where t2.tag = 'SQL'
and t1.score is not null)

select 
    min(score) as min_score_over_avg
from 
    exam_record
where score >= (select avg(score) from tmp)
and exam_id in (select exam_id from examination_info where tag = 'SQL');

编辑于 2024-03-01 19:20:58 回复(0)
select min(score) as min_score_over_avg
from (
     select  score
     from   exam_record join examination_info using(exam_id)
     where   tag="SQL" and score >=(
        select avg(score)
        from exam_record join examination_info using(exam_id)
        where  tag="SQL"
     )
)t
编辑于 2024-02-22 21:53:14 回复(0)
select score as min_score_over_avg from exam_record 
where exam_id in (select exam_id from examination_info where tag = 'SQL') 
and score >= (select avg(score) from exam_record where exam_id in (select exam_id from examination_info where tag = 'SQL')) 
order by score limit 1;
编辑于 2024-01-20 03:34:42 回复(1)
请问这一段为什么执提交报错呀

select
    min(score) as score
from exam_record
where score >=
(

    select
        round(AVG(score),1) as avg_score
    from exam_record
    where exam_id in
    (
        select exam_id
        from examination_info
        where tag='SQL'
    )
)

发表于 2023-12-05 00:28:01 回复(0)
select min(er.score) as min_score_over_avg
from exam_record as er, examination_info as ei
where ei.tag='SQL'
    and er.exam_id=ei.exam_id  #exam_id是作为2张表之间的桥梁
    and score>=(
        select
            avg(score) as ave_score
        from
            exam_record as er, examination_info as ei
        where
            ei.tag='SQL'
            and er.exam_id=ei.exam_id
    )
select min(er.score) as min_score_over_avg
from exam_record as er, examination_info as ei
where ei.tag='SQL'
    and er.exam_id=ei.exam_id  #exam_id是作为2张表之间的桥梁
    and score>=(
        select 
            avg(score) as ave_score
        from
            exam_record as er, examination_info as ei
        where
            ei.tag='SQL'
            and er.exam_id=ei.exam_id
    )

发表于 2023-10-19 16:25:27 回复(0)
各位大佬问下这个哪里错了啊感谢,第二个示例不对
with t as
(select a.uid,a.exam_id,a.score,b.tag
from exam_record a join examination_info b
using(exam_id))
select min(score) from t
where score>=(select avg(score) from t where tag='SQL' and score is not null)

发表于 2023-09-26 16:53:51 回复(0)
SELECT min(a.score) AS min_score_over_avg
FROM exam_record a, (
		SELECT avg(score) AS avg_score
		FROM exam_record
		WHERE exam_id IN (
			SELECT exam_id
			FROM examination_info
			WHERE tag = 'SQL'
		)
	) b
WHERE a.exam_id IN (
		SELECT exam_id
		FROM examination_info
		WHERE tag = 'SQL'
	)
	AND a.score IS NOT NULL
	AND a.score >= b.avg_score;

发表于 2023-09-19 23:15:46 回复(0)
# 请从试卷作答记录表中找到SQL试卷得分不小于该类试卷平均得分的用户最低得分。

with dws_data as (
select 
uid,
t1.exam_id,
score, 
avg(score)over() avg_score -- 平均得分
from exam_record t1 
inner join examination_info t2
on t1.exam_id = t2.exam_id
where submit_time is not null and tag = 'SQL'
) 

select 
min(score) as min_score
from dws_data
where score >= avg_score;

发表于 2023-09-05 10:44:42 回复(0)
with s as (
    select score
    from exam_record left join examination_info using(exam_id)
    where tag = 'SQL' and score is not null
)

select min(score) min_score_over_avg 
from s
where score >= (select avg(score) from s)

发表于 2023-08-28 14:14:38 回复(0)
select r.uid,r.score as min_score_over_avg from examination_info i, exam_record r
where i.exam_id = r.exam_id
    and tag = 'SQL'
    and r.score >= (select avg(score) from exam_record
                    where exam_id in
                                    (select exam_id from examination_info where tag = 'SQL')
                        and score is not null)
order by r.score
limit 1;

发表于 2023-08-22 15:36:50 回复(0)
-- 相关子查询
SELECT MIN(er.score) AS min_score_over_avg
FROM exam_record er
JOIN examination_info ei USING (exam_id)
WHERE ei.tag = 'SQL' AND er.score >= (
    SELECT AVG(score)
    FROM exam_record
    WHERE exam_id = er.exam_id
);

-- JOIN 下的子查询
SELECT MIN(er.score) AS min_score_over_avg
FROM exam_record er
JOIN examination_info ei ON er.exam_id = ei.exam_id
JOIN (
	SELECT exam_id, AVG(score) as avg_score
	FROM exam_record
	GROUP BY exam_id
) a ON er.exam_id = a.exam_id
WHERE ei.tag = 'SQL' AND er.score >= a.avg_score;

发表于 2023-08-08 23:50:12 回复(0)
为什么我不对,只通过一个case
with c as (
    select a.uid, a.score
    from exam_record as a
    left join examination_info as b
    on a.exam_id = b.exam_id and b.tag = 'SQL'
    where a.score IS NOT NULL
)
select score as min_score_over_avg
from c
where score >= (select AVG(score) from c)
order by score
limit 1;

发表于 2023-08-07 16:55:36 回复(1)
  • 二维码

    扫描二维码,关注牛客网

  • 二维码

    下载牛客APP,随时随地刷题