• SQL每日一练(牛客新题库)——第6天:必会常用函数


    1. 计算25岁以上和以下的用户数量

    题目:现在运营想要将用户划分为25岁以下和25岁及以上两个年龄段,分别查看这两个年龄段用户数量
    在这里插入图片描述

    建表语句

    drop table if exists `user_profile`;
    drop table if  exists `question_practice_detail`;
    CREATE TABLE `user_profile` (
    `id` int NOT NULL,
    `device_id` int NOT NULL,
    `gender` varchar(14) NOT NULL,
    `age` int ,
    `university` varchar(32) NOT NULL,
    `gpa` float,
    `active_days_within_30` int ,
    `question_cnt` int ,
    `answer_cnt` int 
    );
    CREATE TABLE `question_practice_detail` (
    `id` int NOT NULL,
    `device_id` int NOT NULL,
    `question_id`int NOT NULL,
    `result` varchar(32) NOT NULL
    );
    CREATE TABLE `question_detail` (
    `id` int NOT NULL,
    `question_id`int NOT NULL,
    `difficult_level` varchar(32) NOT NULL
    );
    
    INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
    INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
    INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
    INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
    INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
    INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
    INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
    INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong');
    INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong');
    INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong');
    INSERT INTO question_practice_detail VALUES(4,6543,111,'right');
    INSERT INTO question_practice_detail VALUES(5,2315,115,'right');
    INSERT INTO question_practice_detail VALUES(6,2315,116,'right');
    INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong');
    INSERT INTO question_practice_detail VALUES(8,5432,117,'wrong');
    INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong');
    INSERT INTO question_practice_detail VALUES(10,2131,113,'right');
    INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong');
    INSERT INTO question_practice_detail VALUES(12,2315,115,'right');
    INSERT INTO question_practice_detail VALUES(13,2315,116,'right');
    INSERT INTO question_practice_detail VALUES(14,2315,117,'wrong');
    INSERT INTO question_practice_detail VALUES(15,5432,117,'wrong');
    INSERT INTO question_practice_detail VALUES(16,5432,112,'wrong');
    INSERT INTO question_practice_detail VALUES(17,2131,113,'right');
    INSERT INTO question_practice_detail VALUES(18,5432,113,'wrong');
    INSERT INTO question_practice_detail VALUES(19,2315,117,'wrong');
    INSERT INTO question_practice_detail VALUES(20,5432,117,'wrong');
    INSERT INTO question_practice_detail VALUES(21,5432,112,'wrong');
    INSERT INTO question_practice_detail VALUES(22,2131,113,'right');
    INSERT INTO question_practice_detail VALUES(23,5432,113,'wrong');
    INSERT INTO question_detail VALUES(1,111,'hard');
    INSERT INTO question_detail VALUES(2,112,'medium');
    INSERT INTO question_detail VALUES(3,113,'easy');
    INSERT INTO question_detail VALUES(4,115,'easy');
    INSERT INTO question_detail VALUES(5,116,'medium');
    INSERT INTO question_detail VALUES(6,117,'easy');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    解题答案

    select (case when age>=25 then '25岁及以上' else '25岁以下' end) as age_cut, 
    count(device_id) as number
    from user_profile
    group by age_cut
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    2. 查看不同年龄段的用户明细

    题目:现在运营想要将用户划分为20岁以下,20-24岁,25岁及以上三个年龄段,分别查看不同年龄段用户的明细情况,请取出相应数据。(注:若年龄为空请返回其他。)
    在这里插入图片描述

    建表语句

    drop table if exists `user_profile`;
    drop table if  exists `question_practice_detail`;
    CREATE TABLE `user_profile` (
    `id` int NOT NULL,
    `device_id` int NOT NULL,
    `gender` varchar(14) NOT NULL,
    `age` int ,
    `university` varchar(32) NOT NULL,
    `gpa` float,
    `active_days_within_30` int ,
    `question_cnt` int ,
    `answer_cnt` int 
    );
    CREATE TABLE `question_practice_detail` (
    `id` int NOT NULL,
    `device_id` int NOT NULL,
    `question_id`int NOT NULL,
    `result` varchar(32) NOT NULL
    );
    CREATE TABLE `question_detail` (
    `id` int NOT NULL,
    `question_id`int NOT NULL,
    `difficult_level` varchar(32) NOT NULL
    );
    
    INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
    INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
    INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
    INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
    INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
    INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
    INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
    INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong');
    INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong');
    INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong');
    INSERT INTO question_practice_detail VALUES(4,6543,111,'right');
    INSERT INTO question_practice_detail VALUES(5,2315,115,'right');
    INSERT INTO question_practice_detail VALUES(6,2315,116,'right');
    INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong');
    INSERT INTO question_practice_detail VALUES(8,5432,117,'wrong');
    INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong');
    INSERT INTO question_practice_detail VALUES(10,2131,113,'right');
    INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong');
    INSERT INTO question_practice_detail VALUES(12,2315,115,'right');
    INSERT INTO question_practice_detail VALUES(13,2315,116,'right');
    INSERT INTO question_practice_detail VALUES(14,2315,117,'wrong');
    INSERT INTO question_practice_detail VALUES(15,5432,117,'wrong');
    INSERT INTO question_practice_detail VALUES(16,5432,112,'wrong');
    INSERT INTO question_practice_detail VALUES(17,2131,113,'right');
    INSERT INTO question_practice_detail VALUES(18,5432,113,'wrong');
    INSERT INTO question_practice_detail VALUES(19,2315,117,'wrong');
    INSERT INTO question_practice_detail VALUES(20,5432,117,'wrong');
    INSERT INTO question_practice_detail VALUES(21,5432,112,'wrong');
    INSERT INTO question_practice_detail VALUES(22,2131,113,'right');
    INSERT INTO question_practice_detail VALUES(23,5432,113,'wrong');
    INSERT INTO question_detail VALUES(1,111,'hard');
    INSERT INTO question_detail VALUES(2,112,'medium');
    INSERT INTO question_detail VALUES(3,113,'easy');
    INSERT INTO question_detail VALUES(4,115,'easy');
    INSERT INTO question_detail VALUES(5,116,'medium');
    INSERT INTO question_detail VALUES(6,117,'easy');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    解题答案

    SELECT device_id,gender,
    case
    when age>=25 then '25岁及以上'
    when age between 20 and 24 then '20-24岁'
    when age<24 then '20岁以下'
    else '其他'
    end
    AS age_cut
    
    FROM user_profile
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述在这里插入图片描述

    3. 计算用户8月每天的练题数量

    题目:现在运营想要计算出2021年8月每天用户练习题目的数量,请取出相应数据。

    在这里插入图片描述

    建表语句

    drop table if exists `user_profile`;
    drop table if  exists `question_practice_detail`;
    drop table if  exists `question_detail`;
    CREATE TABLE `user_profile` (
    `id` int NOT NULL,
    `device_id` int NOT NULL,
    `gender` varchar(14) NOT NULL,
    `age` int ,
    `university` varchar(32) NOT NULL,
    `gpa` float,
    `active_days_within_30` int ,
    `question_cnt` int ,
    `answer_cnt` int 
    );
    CREATE TABLE `question_practice_detail` (
    `id` int NOT NULL,
    `device_id` int NOT NULL,
    `question_id`int NOT NULL,
    `result` varchar(32) NOT NULL,
    `date` date NOT NULL
    );
    CREATE TABLE `question_detail` (
    `id` int NOT NULL,
    `question_id`int NOT NULL,
    `difficult_level` varchar(32) NOT NULL
    );
    
    INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
    INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
    INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
    INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
    INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
    INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
    INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
    INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03');
    INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');
    INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');
    INSERT INTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');
    INSERT INTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');
    INSERT INTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');
    INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');
    INSERT INTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');
    INSERT INTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');
    INSERT INTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');
    INSERT INTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');
    INSERT INTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');
    INSERT INTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');
    INSERT INTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');
    INSERT INTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');
    INSERT INTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');
    INSERT INTO question_detail VALUES(1,111,'hard');
    INSERT INTO question_detail VALUES(2,112,'medium');
    INSERT INTO question_detail VALUES(3,113,'easy');
    INSERT INTO question_detail VALUES(4,115,'easy');
    INSERT INTO question_detail VALUES(5,116,'medium');
    INSERT INTO question_detail VALUES(6,117,'easy');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    解题答案

    --法一:like运算符
    select
        day(date) as day,
        count(question_id) as question_cnt
    from question_practice_detail
    where date like '2021-08%'
    group by day(date);
    
    --法二:regexp运算符
    select
        day(date) as day,
        count(question_id) as question_cnt
    from question_practice_detail
    where date regexp '2021-08'
    group by day(date);
    
    --法三:substring提取日期
    select
        day(date) as day,
        count(question_id) as question_cnt
    from question_practice_detail
    where substring(date,1,7) = '2021-08'
    group by day(date);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    4. 计算用户的平均次日留存率

    题目:现在运营想要查看用户在某天刷题后第二天还会再来刷题的平均概率。请你取出相应数据。
    在这里插入图片描述

    建表语句

    drop table if exists `user_profile`;
    drop table if  exists `question_practice_detail`;
    drop table if  exists `question_detail`;
    CREATE TABLE `user_profile` (
    `id` int NOT NULL,
    `device_id` int NOT NULL,
    `gender` varchar(14) NOT NULL,
    `age` int ,
    `university` varchar(32) NOT NULL,
    `gpa` float,
    `active_days_within_30` int ,
    `question_cnt` int ,
    `answer_cnt` int 
    );
    CREATE TABLE `question_practice_detail` (
    `id` int NOT NULL,
    `device_id` int NOT NULL,
    `question_id`int NOT NULL,
    `result` varchar(32) NOT NULL,
    `date` date NOT NULL
    );
    CREATE TABLE `question_detail` (
    `id` int NOT NULL,
    `question_id`int NOT NULL,
    `difficult_level` varchar(32) NOT NULL
    );
    
    INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
    INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
    INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
    INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
    INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
    INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
    INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
    INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03');
    INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');
    INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');
    INSERT INTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');
    INSERT INTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');
    INSERT INTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');
    INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');
    INSERT INTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');
    INSERT INTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');
    INSERT INTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');
    INSERT INTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');
    INSERT INTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');
    INSERT INTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');
    INSERT INTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');
    INSERT INTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');
    INSERT INTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');
    INSERT INTO question_detail VALUES(1,111,'hard');
    INSERT INTO question_detail VALUES(2,112,'medium');
    INSERT INTO question_detail VALUES(3,113,'easy');
    INSERT INTO question_detail VALUES(4,115,'easy');
    INSERT INTO question_detail VALUES(5,116,'medium');
    INSERT INTO question_detail VALUES(6,117,'easy');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    解题答案

    select avg(if(b.device_id is not null,1,0)) as avg_ret
    from 
    (select distinct device_id,date
    from question_practice_detail
    )a
    left join 
    (
    select distinct device_id,date_sub(date,interval 1 day) as date 
    from question_practice_detail
    )b
    on a.device_id = b.device_id and a.date = b.date
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    5. 如何让刷题变得更高效?

    最近很多学了基础的小伙伴问我该怎么提升编程水平?学了基础该上哪刷题?明明学了很多,做项目却不知道怎么上手,其实这就是练得太少,只注重了学,却忽视了刷题,只有不断练习才能提高和巩固编程思维和能力!
    在这里插入图片描述
    链接地址牛客网 | SQL刷题篇,废话少说速度上号!!!

  • 相关阅读:
    HTML+CSS+JS大作业:生态环境网站设计——环境保护主题 大学生环保主题网页制
    docker搭建个人镜像仓库
    【SSM】Spring系列——IoC 控制反转
    稳定性实践:限流降级
    Hbuilder本地调试微信H5项目(二)--添加UView框架插件
    python获取上一年,上一月,上一日relativedelta
    (九)admin-boot项目之代码生成器
    flink1.18.0 开始支持配置算子级别的TTL
    Leetcode 1670. Design Front Middle Back Queue
    3D数学之三角公式
  • 原文地址:https://blog.csdn.net/yuan2019035055/article/details/126292833