滴滴/快手/头条数据分析实习岗SQL测试题总结_快手sql面试题-程序员宅基地

技术标签: 面试  数据分析  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;
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_59773145/article/details/119155584

智能推荐

《第一行代码》(第二版)广播的问题及其解决_代码里的广播错误-程序员宅基地

文章浏览阅读2.6k次,点赞5次,收藏13次。1)5.2.1弹出两次已连接或者未连接这是因为你同时打开了流量和WiFi,他就会发出两次广播。2)5.3.1中发送自定义广播问题标准广播未能弹出消息:Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");sendBroadcast(intent);上述已经失效了。修改:Intent intent=new Intent("com.example.broadcasttest...._代码里的广播错误

K8s 学习者绝对不能错过的最全知识图谱(内含 58个知识点链接)-程序员宅基地

文章浏览阅读249次。作者 |平名 阿里服务端开发技术专家导读:Kubernetes 作为云原生时代的“操作系统”,熟悉和使用它是每名用户的必备技能。本篇文章概述了容器服务 Kubernet..._k8知识库

TencentOS3.1安装PHP+Nginx+redis测试系统_tencentos-3.1-程序员宅基地

文章浏览阅读923次。分别是etc/pear.conf,etc/php-fpm.conf, etc/php-fpm.d/www.conf,lib/php.ini。php8安装基本一致,因为一个服务期内有2个版本,所以注意修改不同的安装目录和端口号。可以直接使用sbin下的nginx命令启动服务。完成编译安装需要gcc支持,如果没有,使用如下命令安装。安装过程基本一致,下面是安装7.1.33的步骤。执行如下命令,检查已经安装的包和可安装的包。执行如下命令,检查已经安装的包和可安装的包。执行如下命令,检查已经安装的包和可安装的包。_tencentos-3.1

urllib.request.urlopen()基本使用_urllib.request.urlopen(url)-程序员宅基地

文章浏览阅读3.1w次,点赞21次,收藏75次。import urllib.requesturl = 'https://www.python.org'# 方式一response = urllib.request.urlopen(url)print(type(response)) # <class 'http.client.HTTPResponse'># 方式二request = urllib.request.Req..._urllib.request.urlopen(url)

如何用ChatGPT+GEE+ENVI+Python进行高光谱,多光谱成像遥感数据处理?-程序员宅基地

文章浏览阅读1.5k次,点赞12次,收藏15次。如何用ChatGPT+GEE+ENVI+Python进行高光谱,多光谱成像遥感数据处理?

RS485总线常识_rs485 差分走綫間距-程序员宅基地

文章浏览阅读1.2k次。RS485总线常识 2010-10-12 15:56:36| 分类: 知识储备 | 标签:rs485 总线 传输 差分 |字号大中小 订阅RS485总线RS485采用平衡发送和差分接收方式实现通信:发送端将串行口的TTL电平信号转换成差分信号A,B两路输出,经过线缆传输之后在接收端将差分信号还原成TTL电平信号。由于传输线通常使用双绞线,又是差分传输,所_rs485 差分走綫間距

随便推点

移植、制作uboot、Linux(一)_uboot制作-程序员宅基地

文章浏览阅读621次。u-boot、linux烧录_uboot制作

windows下安装git和gitbash安装教程_64-bit git for windows setup.-程序员宅基地

文章浏览阅读1.2w次,点赞10次,收藏44次。windos上git安装,git bash安装_64-bit git for windows setup.

环形链表(算法java)_java 实现环形链表-程序员宅基地

文章浏览阅读196次。环形链表(算法java)的两种解决方法_java 实现环形链表

docker部署Airflow(修改URL-path、更换postgres -->myslq数据库、LDAP登录)_airflow docker-程序员宅基地

文章浏览阅读5.7k次。Airflow什么是 Airflow?Airflow 的架构Airflow 解决哪些问题一、docker-compose 安装airflow(postgres)1、创建启动文件airflow-docker-compose.yml.1.1、添加挂载卷,需要修改airflow-docker-compose.yml的位置2、创建本地配置文件airflow.cfg2.1、如果想修改WEB URL地址,需要修改airflow.cfg中以下两个地方3、之后up -d直接启动即可web访问地址:二、存储数据库更换post_airflow docker

计算机毕业设计springboot高校教务管理系统532k79【附源码+数据库+部署+LW】-程序员宅基地

文章浏览阅读28次。选题背景:随着社会的发展和教育的普及,高校教务管理系统在现代高等教育中扮演着至关重要的角色。传统的手工管理方式已经无法满足高校日益增长的规模和复杂的管理需求。因此,开发一套高效、智能的教务管理系统成为了当今高校管理的迫切需求。选题意义:高校教务管理系统的开发具有重要的意义和价值。首先,它可以提高高校教务管理的效率和准确性。通过自动化处理学生选课、排课、考试安排等繁琐的事务,大大减轻了教务人员的工作负担,提高了工作效率。同时,系统可以实时更新学生信息和课程信息,减少了数据错误和冗余,保证了管理的准确性

javaint接收float_Java Integer转换double,float,int,long,string-程序员宅基地

文章浏览阅读132次。首页>基础教程>常用类>常用 Integer类Java Integer转换double,float,int,long,stringjava中Integer类可以很方便的转换成double,float,int,long,string等类型,都有固定的方法进行转换。方法double doubleValue() //以 double 类型返回该 Integer 的值。flo..._java integet接收float类型的参数

推荐文章

热门文章

相关标签