滴滴/快手/头条数据分析实习岗SQL测试题总结

最近找实习基本上每个岗位都有SQL考试内容,有的岗位(滴滴国际化)是笔试题,有的岗位(抖音数据BP和快手)是面试现场做题。主要考察点包括group bycase whenorder by ... limit ...这些。下面列几道题的解法:

第一题(滴滴笔试)
学生表(tb_student)

学生姓名 (name) 学号 (id) 班级 (class) 入学时间 (in_time) 年龄 (age) 性别 (sex) 专业 (major)
张三 2017C3301001 2017C3301 2017 18 计算机

学生成绩表(tb_score)

学号 (id) 课程 (course) 分数 (score)
2017C3301001 数据库 75
  • 筛选出2017年入学“计算机”专业年龄最小的10位同学名单(姓名、学号、班级、年龄):
select name, id, class, age 
from tb_student 
where major = ‘计算机’ and in_time = 2017 
order by 年龄 asc, 
limit 10;
  • 统计每个班同学各科成绩平均分大于80分的人数和人数占比
select tb_student.class,
		sum(case when avg_score>80 then 1 else 0 end) as num,
		sum(case when avg_score>80 then 1 else 0 end)/count(tb_student.id) as prop
from tb_student as a
left join(
		select id, avg(score) as avg_score 
		from tb_score
		group by id) as b
on a.id = b.id
group by class;
  • 统计出班级“2017C3301”的同学中“数据库”课程的成绩分布,成绩按照5分分段
select 成绩分布区间, count(1) as 人数
from(
select case when score>95 and score<100 then '95-100'
		when score>90 and score<95 then '90-95'
		when score>85 and score<90 then '85-90'
		when score>80 and score<85 then '80-85' end as 成绩分布区间
from tb_score as a
left join tb_student on a.id = tb_student.id
where class = '2017C3301' and course = '数据库')
group by 成绩分布区间;

第二题(滴滴笔试)
有用户行为表tracking_log,字段有:user_id‘用户编号’,opr_id‘操作编号’,‘operation_time‘操作时间

  • 计算每天的访客数和他们的平均操作次数
select date(operation_time), count(distinct user_id) as 访客数, count(opr_id)/count(distinct user_id) as 平均操作次数
from tracking_log
group by date(operation_time);
  • 计算每天新增用户数
select date(a.start), count(distinct a.user_id) as 新增用户
from(
		select user_id, min(operation_time) as start from tracking_log group by user_id) as a
group by date(a.start);
  • 计算第2天(也是留存数和留存比例),第30天的回访比例
select date(a.start), count(distinct b.user_id)/count(distinct a.user_id) as 第二天回访比例, count(distinct c.user_id)/count(distinct a.user_id) as 第三十天回访比例
from(
		select user_id, min(operation_time) as start from tracking_log group by user_id) as a
left join(
		select user_id, operation_time from tracking_log) as b
on a.user_id = b.user_id and date(a.operation_time) = date(b.operation_time)-1
left join(
		select user_id, operation_time from tracking_log) as c
on a.user_id = c.user_id and date(a.operation_time) = date(c.operation_time)-29
group by date(a.start);

第三题(滴滴笔试)
表格Table_a

passenger_id call_time call_date driver_id origin destination
p0 yyyy-mm-dd hh:mm:ss dt d1 o1 d1
  • Please use SQL to find every passenger’s earliest call information from the following table(Table_a). Notice that you should include driver, origin and destination data.
select * from table_a as a
inner join(
		select min(call_time) as time
		from table_a
		group by passenger_id) as b
on a.passenger_id = b.passenger_id and a.call_time = b.time;

第四题(快手面试)
求中位数
我当时的想法是给所有数据排序,选择序数为ceil( count / 2)的一行。这里要注意总数除以二之后需要四舍五入。
后来在网上找到了另一种解法:

select id, company, salary
from(select id, company, salary,
cast(row_number() over(partition by company order by salary asc, id asc) as signed) as 'id1',
cast(row_number() over(partition by company order by salary desc, id desc) as signed) as 'id2'
from employee) as newtable
where abs(id1-id2)=1 or id1=id2;

这个答案的思路是将所有数据正向排列+反向排列,序数差距在一之内的就是中位数。答案参考知乎文章。

第五题(抖音面试)
有表adv_cost_table,包括advertiser_id,landing_type,content_type等信息。

  • 统计表内广告主数量、推广目的枚举值数量、推广类型枚举值数量以及广告分类数量
select count(advertiser_id) as 广告主, 
	   count(distinct landing_type) as 推广目的, 
	   count(distinct content_type) as 推广类型, 
	   count(distinct classify) as 分类
from adv_cost_table;
  • 统计推广类型为文章和抖音直播,且广告分类为内广的所有广告主的累计广告消耗、累计展示量和平均播放量、平均点击量
select sum(cost) as 累计广告消耗, sum(show_count) as 累计展示量, avg(play_count) as 平均播放量, avg(click_count) as 平均点击量
from adv_cost_table
where (content_type = ‘文章’ or content_type = ‘抖音直播’) and (classify = ‘内广’);

第六题(滴滴面试)
现有裂变工具分为主态(发起人)、客态(助力人),请按发起人所对应的助力人在’2021-06-01’ 至 '2021-06-30’日期间进行档位划分,计算出各档位对应助力人下单人数。档位划分规则是发起人拥有的助力人人数0-9为低档,10-69为中档,70以上为高档。

dwd_applicant_di 用户关系表 增量表
ref_uid 发起人uid string
apl_uid 助力人uid string
apl_time 绑定时间 string
dt 分区时间 string 日期格式(yyyy-mm-dd)

user_info_df 用户信息表 全量表
uid 用户id string
first_ord_date string 日期格式(yyyy-mm-dd)
last_ord_date string 日期格式(yyyy-mm-dd)
dt 分区时间 string 日期格式(yyyy-mm-dd)

我当时的思路是:

  • 算出每个发起人的助力人数
  • 对发起人分档
  • 筛选时间范围内的订单
  • 筛选有订单的名单

第七题(滴滴面试)
根据考试分数筛选每班前三名。
在网上找到了这种解法。原题是找出部门工资最高前三,有两张表:
Employee表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId;
Department 表包含公司所有部门的信息(Id和Name)。
答案来自LeetCode上面的解答。
大概思路是通过要求高于salary的值不超过三个把两个表连接找出前三的值。

select
	Department.NAME as Department,
	e1.NAME as Employee,
	e1.Salary as Salary 
from
	Employee as e1,Department 
where
	e1.DepartmentId = Department.Id 
	and 3 > (select  count( distinct e2.Salary ) 
			 from Employee as e2 
			 where	e1.Salary < e2.Salary and e1.DepartmentId = e2.DepartmentId 	) 
order by Department.NAME, Salary desc;

Guess you like

Origin blog.csdn.net/m0_59773145/article/details/119155584