前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >「求职」7道数据分析面试题,涵盖80%常考知识点『SQL篇』

「求职」7道数据分析面试题,涵盖80%常考知识点『SQL篇』

作者头像
小火龙说数据
发布2022-06-30 16:53:58
4370
发布2022-06-30 16:53:58
举报
文章被收录于专栏:小火龙说数据小火龙说数据

预计阅读时间:8min

解决痛点:本文为招聘过程中总结的7道SQL面试题,涵盖常考知识点,对于准备找工作的你会有很大帮助。

00

原始表介绍

以下面试题背景为电商类消费场景,其中包含「用户表」「购物消费流水表」两张。

【用户表】ubs_user_profile_di

当日活跃用户,ds+uid为唯一key,每个用户每日仅有一条数据。表结构如下:

【购物消费流水表】ubs_sales_di

当日用户消费详细数据,每一条代表用户购买一次商品,用户每日可购买多次商品。表结构如下:

01

题目1

考核点:单表处理类问题

难度系数:1星

题目:计算20220501日,各年龄活跃用户数,筛选用户数>10000的年龄,并按照用户数降序排列。

输出样式

考核代码:

代码语言:javascript
复制
select
    age
    ,count(*) as uv
from
    ubs_user_profile_di
where
    ds = '20220501'
group by
    age
having
    uv > 10000
order by
    uv desc
;

02

题目2

考核点:多表关联类问题

难度系数:2星

题目:计算20220501日及往前90日,新用户每日人均消费金额,且保留2位小数(四舍五入)。

输出样式:

考核代码:

代码语言:javascript
复制
select
    tmp1.ds as ds
    ,round(avg(money), 2) as money_per_user
from
    --筛选用户
    (
    select
        ds
        ,uid
    from
        ubs_user_profile_di
    where
        ds between date_add('20220501', -90) and '20220501'
        and is_new = 1
    )tmp1
inner join
    --关联用户消费情况
    (
    select
        ds
        ,uid
        ,sum(money) as money
    from
        ubs_sales_di
    where
        ds between date_add('20220501', -90) and '20220501'
    group by
        ds
        ,uid
    )tmp2
on
    tmp1.ds = tmp2.ds
    and tmp1.uid = tmp2.uid
group by
    tmp1.ds
;

03

题目3

考核点:嵌套类问题

难度系数:2星

题目:计算202205月中,用户消费天数分布,以及人均消费金额。

输出样式:

考核代码:

代码语言:javascript
复制
select
    days
    ,avg(money) as money_per_user
from
    (
    select
        uid
        ,count(distinct uid) as days
        ,sum(money) as money
    from
        ubs_sales_di
    where
        substr(ds, 1, 6) = '202205'
    group by
        uid
    )tmp
group by
    days
;

04

题目4

考核点:窗口函数类问题

难度系数:3星

题目:计算20220501日,用户第一次购买的一级品类用户分布量级,按照量级降序排列。

输出样式:

考核代码:

代码语言:javascript
复制
select
    category_first
    ,count(*) as uv
from
    (
    select
        uid
        ,category_first
        ,row_number()over(partition by uid order by report_time asc) as rank --分组排序
    from
        ubs_sales_di
    where
        ds = '20220501'
    )tmp
where
    rank = 1
group by
    category_first
order by
    uv desc
;

05

题目5

考核点:窗口函数类问题

难度系数:3星

题目:计算20220401至今,用户第一次购买与第二次购买相差天数,没有第二次购买则不输出。

输出样式:

考核代码:

代码语言:javascript
复制
select
    uid
    ,ds as first_sales_day
    ,ds_next as second_sales_day
    ,datediff(to_date(ds_next, 'yyyymmdd'),to_date(ds, 'yyyymmdd')) as date_diff
from
    (
    select
        ds
        ,uid
        ,row_number()over(partition by uid order by report_time asc) as rank --用于筛选首次
        ,lead(ds, 1, 'NULL')over(partition by uid order by report_time asc) as ds_next --用户获取下次购买日期
    from
        ubs_sales_di
    where
        ds >= '20220401'
    )tmp
where
    rank = 1
    and ds_next != 'NULL'
;

06

题目6

考核点:留存/复购类问题

难度系数:4星

题目:计算20220401-20220430,每日用户量级、次日留存率、3日留存率、7日留存率。

输出样式:

考核代码:

代码语言:javascript
复制
select
    ds
    ,count(*) as uv
    ,count(if(1_remain>0, 1, null))/count(*) as 1_retention_rate
    ,count(if(3_remain>0, 1, null))/count(*) as 3_retention_rate
    ,count(if(7_remain>0, 1, null))/count(*) as 7_retention_rate
from
    (
    select
        user_now.ds as ds
        ,user_now.uid as uid
        ,count(if(datediff(to_date(user_after.ds, 'yyyymmdd'),to_date(user_now.ds, 'yyyymmdd')) = 1, 1, null)) as 1_remain --计算用户未来第1日是否能匹配上
        ,count(if(datediff(to_date(user_after.ds, 'yyyymmdd'),to_date(user_now.ds, 'yyyymmdd')) = 3, 1, null)) as 3_remain --计算用户未来第3日是否能匹配上
        ,count(if(datediff(to_date(user_after.ds, 'yyyymmdd'),to_date(user_now.ds, 'yyyymmdd')) = 7, 1, null)) as 7_remain --计算用户未来第7日是否能匹配上
    from
        --当日用户
        (
        select
            ds
            ,uid
        from
            ubs_user_profile_di
        where
            ds between '20220401' and '20220430'
        )user_now
    left join
        --匹配用户未来是否来
        (
        select
            ds
            ,uid
        from
            ubs_user_profile_di
        where
            ds between '20220402' and '20220507' --注意时间
        )user_after
    on
        user_now.uid = user_after.uid
    group by
        user_now.ds
        ,user_now.uid
    )tmp
group by
    ds
;

07

题目7

考核点:连续消费/登录类问题

难度系数:5星

题目:计算20220401至今,连续消费3日及以上的用户占比。

输出样式:

考核代码:

代码语言:javascript
复制
select
    count(distinct if(ct>=3, uid, null))/count(distinct uid) as 3days_uv_rate
from
    --步骤4:计算用户每次连续消费的天数,例如:用户在0401开始连续3日、在0415开始连续2日
    (
    select
        uid
        ,base_ds
        ,count(*) as ct
    from
        --步骤3:计算基准时间,通过基准时间的数量判断连续几日消费,例如:用户在[0401、0402、0403消费],[rank为1、2、3],[计算的base_ds为0401往前1日、0402往前2日、0403往前3日,均为0331,则连续3日]
        (
        select
            *
            ,date_add(ds, -rank) as base_ds
        from
            --步骤2:按照用户消费日期升序排序
            (
            select
                *
                ,row_number()over(partition by uid order by ds asc) as rank
            from
                --步骤1:将用户每日多消费记录去重(单天多条会影响计算)
                (
                select
                    ds
                    ,uid
                from
                    ubs_sales_di
                where
                    ds >= '20220401'
                group by
                    ds
                    ,uid
                )tmp1
            )tmp2
        )tmp3
    group by
        uid
        ,base_ds
    )tmp4
;

注意:针对该类问题,除了可以采用以上方式,还可以通过创建udf来实现,后面会针对udf/udtf/udaf做一期分享。

08

注意事项

最后和大家谈谈针对面试中遇到的SQL问题的关注点:

由于是面试,面试官重点关注的是思路,因此在忘记某些函数的情况下,可以将思路输出给面试官,函数是工具,可以随时查询,而思路才是你掌握这个知识的关键;另外,针对某些问题,如果你有多种解答方式,不要吝惜,全部输出给面试官,会是很好的加分项。

以上就是本期的内容分享

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-04-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小火龙说数据 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档