SQL面经
-
-
- mysql主键和唯一索引的区别
- 每门课的及格率
- 表结构为user_id,reg_time,age, 写一句sql按user_id尾数随机抽样2000个用户 写一句sql取出按各年龄段(每10岁一个分段,如(0,10))分别抽样1%的用户
- mysql rank
- mysql随机选取数据
- 交易表结构为user_id,order_id,pay_time,order_amount
- 表user_id,visit_date,page_name,plat 统计近7天每天到访的新用户数 统计每个访问渠道plat7天前的新用户的3日留存率和7日留存率
- 用户登录日志表为user_id,log_id,session_id,plat 用sql查询近30天每天平均登录用户数量 用sql查询出近30天连续访问7天以上的用户数量
- 表结构为user_id,reg_time,age, 写一句sql按user_id尾数随机抽样2000个用户 写一句sql取出按各年龄段(每10岁一个分段,如(0,10))分别抽样1%的用户
- 学校中有一个年级表,有两个核心字段,student_id和class_id,把年级中大于30人的班级查出来?
- Swap salary
- Second Highest salary
- Rank Scores
- Trips and Users
- [Find Meadian Given Frequency of numbers](https://leetcode.com/problems/find-median-given-frequency-of-numbers/description/)
- [Department Top Three Salaries](https://leetcode.com/problems/department-top-three-salaries/description/)
- [Department Highest Salary](https://leetcode.com/problems/department-highest-salary/)
- [Human Traffic of Stadium](https://leetcode.com/problems/human-traffic-of-stadium/submissions/1)
-
【秋招】拼多多_数据分析岗_面试题整理
mysql主键和唯一索引的区别
1)主键一定会创建一个唯一索引,但是有唯一索引的列不一定是主键; 2)主键不允许为空值,唯一索引列允许空值;
3)一个表只能有一个主键,但是可以有多个唯一索引;
4)主键可以被其他表引用为外键,唯一索引列不可以;
5)主键是一种约束,而唯一索引是一种索引,是表的冗余数据结构,两者有本质的差别
每门课的及格率
select
sum( case when course='c1' and score>=60 then 1 else 0 end)/sum( case when course='c1' then 1 else 0 end) as rate1,
sum( case when course='c2' and score>=60 then 1 else 0 end)/sum( case when course='c2' then 1 else 0 end) as rate2
from table;
表结构为user_id,reg_time,age, 写一句sql按user_id尾数随机抽样2000个用户 写一句sql取出按各年龄段(每10岁一个分段,如(0,10))分别抽样1%的用户
select user_id form table where mod(user_id/10,1)*10=round(rand()*10) limit 200;
mysql rank
SELECT a1.Name, a1.Sales, COUNT(a2.Sales) Sales_Rank
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.Sales OR (a1.Sales=a2.Sales AND a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;
mysql随机选取数据
交易表结构为user_id,order_id,pay_time,order_amount
写sql查询过去一个月付款用户量(提示 用户量需去重)最高的3天分别是哪几天
select date_format(pay_time,'%Y-%m-%d') as date, count(distinct user_id) as num_users
from table
where pay_time>=date_sub(now(),interval 1 month) #过去一个月
group by date
order by num_users desc
limit 3;
写sql查询昨天每个用户最后付款的订单ID及金额
select a.user_id, a.order_amount
from
(select user_id, order_amount,
row_number() over(partition by user_id order by pay_time desc) as rank
from table
where date_format(pay_time,"%Y-%m-%d")=date_sub(curdate(),interval 1 day) #昨天)
as a
where rank=1
2、用户登录日志表为user_id,log_time,session_id,plat
写sql查询近30天,每天平均登录用户数量?
select a.date, average