当前位置:网站首页>Have you learned the common SQL interview questions on the short video platform?
Have you learned the common SQL interview questions on the short video platform?
2022-07-28 04:53:00 【Monkey data analysis】
【 subject 】
There are three tables in the database of a short video company , List of user video information 、 Anchor start schedule 、 Detailed list of user information in the live broadcasting room .
The list of user video information includes users id, Release video id, Video likes , Video release date , Video category and number of fans .
The anchor start schedule contains the anchor id, The host opens the live broadcast room id Number , And the time of broadcasting .
The user information list of the live broadcasting room includes the audience who enter the live broadcasting room to watch id, Enter the live studio id And the time the audience entered .
Business needs :
1. Find the video with the highest number of likes per user , When the number of likes is the same, follow the video id The biggest record .
2. Find out the number of fans in 6 month 2 No. 1 has been promoted the most 20 Users id ( contrast 6 month 1 Number ).
3. Find out the number of the live broadcast room that no one has entered within three minutes of broadcasting .
【 Their thinking 】
1. Find the video with the highest number of likes per user , When the number of likes is the same, follow the video id The biggest record .
Let's first translate this business requirement into Chinese :
1) The fields required for query results are users id、 video id、 Number of likes
2) According to the user id The group is then sorted according to the number of likes of each user's video , If the likes are the same, follow the video id Sort
3) Select the video with the highest number of likes per user
Each video uploaded by each user is required to be displayed , We know group by The number of rows in the table is changed after grouping , There is only one category in a row . Using the window function does not reduce the number of rows in the original table .
By user id grouping (partiotion by user id)、 And click the most like number 、 video id Descending order (order by Number of likes , video id ), Descending order desc Use the syntax of nested window functions , Get the following SQL sentence :
select
user id , video id , Number of likes ,
row_number()over(partition by user id order by Number of likes desc, video id desc) as ranking
from List of user video information ;Query results :
Rank the bottom according to the video likes of each user , We ranked first by screening , That is, the video with the most likes .SQL It is written as follows :
select user id , video id , Number of likes
from
(select user id , video id , Number of likes ,row_number()over(partition by user id
order by Number of likes desc, video id desc) as ranking
from List of user video information )t
where ranking =1;Query results :
2. Find out the number of fans in 6 month 2 No. 1 has been promoted the most 3 Users id ( contrast 6 month 1 Number ).
Let's split this business requirement first :
1) Get that each user is 6 month 2 The number of rising fans of No
2) Before finding out 3 Users with the most fans id
1) Get that each user is 6 month 2 The number of rising fans of No
Take a look at the list of user video information , To find out 6 month 2 No. 1 is the fastest growing user , Will know 6 month 1 What is the number of fans of user No , hold 6 month 2 The number of fans on the number minus 6 month 1 The number of fans per user can be calculated from the number of fans per user .
We will limit the release time to 6 month 1 Number -2 Between , Use if Functions and sum Function to calculate the number of rising powder , If the release date is 6 month 2 Number , Show fields ” Accumulated fans of users ”, If not, it shows ”( negative )- Accumulated fans of users ”, Finally, sum to get the number of rising powder .
SQL It is written as follows :
select user id ,sum(if ( Release date ="2022/6/2", Accumulated fans of users ,- Accumulated fans of users )) as " Rising powder number "
from List of user video information
where Release date in ("2022/6/2","2022/6/1")
group by user id;Query results :
2) Before finding out 3 Users with the most fans id
Get that each user is 6 month 2 As a temporary table t, use order by Sort the number of users in descending order (desc) after , use limit 3 Get the top three users with the most fans id.
SQL It is written as follows :
select user id, Rising powder number
from
(select user id ,sum(if ( Release date ="2022/6/2", Accumulated fans of users ,- Accumulated fans of users )) as " Rising powder number "
from List of user video information
where Release date in ("2022/6/2","2022/6/1")
group by user id )t
order by Rising powder number desc
limit 3;Query results :
3. Find out the number of the live broadcast room that no one has entered within three minutes of broadcasting .
Observe the anchor start schedule and the live room user information schedule , We can know the starting time of each anchor and the time when the audience enters the live room . Use the studio id Connect the two tables to get the user information of the live broadcast room .
SQL It is written as follows :
select a. The host id, a. studio id, The audience id,a. Start time ,b. Entry time
from Anchor start schedule a
left join Detailed list of user information in the live broadcasting room b
on a. studio id =b. Enter the live studio id;Query results :
It is obvious from the query results that R004 There is no audience in this studio , We can use the audience id Whether it is empty is used to judge whether there is an audience in the live broadcasting room ( The audience id is null),R005 The audience of this live studio will enter the live studio three minutes later .
Business requirements: we find out the live broadcast that has no audience within three minutes after the anchor starts broadcasting id, use date_add Function to calculate the start time and the audience's entry time to calculate the time difference .date_add Function usage is as follows :
SQL It is written as follows :
b. Entry time > date_add(a. Start time ,interval +3 minute)Substitute into the whole SQL in
select a. The host id, a. studio id, The audience id,a. Start time ,b. Entry time
from Anchor start schedule a
left join Detailed list of user information in the live broadcasting room b
on a. studio id =b. Enter the live studio id
and b. Entry time > date_add(a. Start time ,interval +3 minute)
where b. The audience id is null;Query results :
【 The test point of this question 】
1. Be familiar with the usage of window functions , Most of them are used to sort similar business requirements for each category under each user .
2. Encounter complex business requirements , Try to disassemble the multi-dimensional analysis method into several simple problems .
3. Apply to multi table information , First, think about multi table join , Then the connection type is obtained according to the specific business scenario .
边栏推荐
- 01 node express system framework construction (express generator)
- Evolution of ape counseling technology: helping teaching and learning conceive future schools
- [Sylar] framework -chapter15 stream module
- Redux basic syntax
- What should testers know about login security?
- 【sylar】框架篇-Chapter23-模块篇总结
- 王爽汇编语言详细学习笔记三:寄存器(内存访问)
- [Sylar] framework -chapter20- daemon module
- Can plastics comply with gb/t 2408 - Determination of flammability
- np. The data returned from delete details is the data after deleting the specified dimension
猜你喜欢

After a year of unemployment, I learned to do cross-border e-commerce and earned 520000. Only then did I know that going to work really delayed making money!
![[idea] check out master invalid path problem](/img/83/d36362ba314177cd6f1f74f3e922cd.png)
[idea] check out master invalid path problem

Leetcode 18. sum of four numbers

Blooming old trees -- quickly build a map bed application with imageprocessor

提升学生群体中的STEAM教育核心素养

Youxuan database participated in the compilation of the Research Report on database development (2022) of the China Academy of communications and communications

After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?

excel实战应用案例100讲(十一)-Excel插入图片小技巧

解析智能扫地机器人中蕴含的情感元素

低代码是开发的未来吗?浅谈低代码平台
随机推荐
Analysis of the reason why easycvr service can't be started and tips for dealing with easy disk space filling
[函数文档] torch.histc 与 paddle.histogram 与 numpy.histogram
[Sylar] framework chapter -chapter10-address module
Redis配置文件详解/参数详解及淘汰策略
Take out system file upload
Wang Shuang assembly language detailed learning notes 3: registers (memory access)
[Sylar] framework -chapter15 stream module
(2.4) [service Trojan -slimftp] introduction and use
[daily question 1] 735. Planetary collision
[Sylar] framework Chapter 8 timer module
Leetcode 454. Adding four numbers II
Redis configuration file explanation / parameter explanation and elimination strategy
[Hongke technology] Application of network Multimeter in data center
What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?
[Sylar] framework Chapter 6 collaborative scheduling module
printf()打印char* str
Summary and review of puppeter
Design and development of C language ATM system project
scipy.stats.chi2
[每日一氵]上古年代的 Visual Studio2015 安装