Database LeetCode Daily Practice (3)

content

foreword

Topic 1: Gameplay Analysis

Topic 2: Customers with the most orders

Topic 3: Big countries

Topic 4: Consecutive Numbers

Topic summary


foreword

  • Review the previous highlights

Daily practice of database LeetCode (1) _Xiaojie312 's blog Exercise ( 2 )

Topic 1: Gameplay Analysis

511. Gameplay Analysis I

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5p2wMzEy,size_20,color_FFFFFF,t_70,g_se,x_16

  • Question requirements: Get the date of each player's first login to the platform
  • Processing idea: The date of logging in to the platform for the first time turned out to be the date of logging in to the platform for the first time, which is the smallest event_date in the corresponding player_id, so the idea came out, and you can directly query min(date) with an aggregate query.   
  • How to think of using aggregate query, there are multiple login dates first, but we only need the smallest one, so we thought of compressing through aggregate query, which can be used in combination with aggregate functions. Commonly used aggregate functions are attached as follows

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5p2wMzEy,size_20,color_FFFFFF,t_70,g_se,x_16

select 
    player_id,
    min(event_date) as 'first_login'  
from 
    activity
group by 
    player_id;

Topic 2: Customers with the most orders

586. Customers with the Most Orders

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5p2wMzEy,size_14,color_FFFFFF,t_70,g_se,x_16

  •  Title requirement: Get the customer with the most orders
  • Thinking direction: We need to get the customer with the largest number of orders. The first step is to calculate the number of orders. To calculate the number of orders, we need to aggregate query, count(customer_number) or count(*) to get the number of orders, and then directly according to the number of orders from Sort from big to small. The first item is the number of max orders required by customers (to get the largest, which is the largest, and which is the smallest, you must think of sort + limit paging)
select 
    customer_number
from 
    orders
group by customer_number
order by count(*) desc
limit 0, 1; # 获取第一条记录

Topic 3: Big countries

595. Great country

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5p2wMzEy,size_20,color_FFFFFF,t_70,g_se,x_16

  •  Title requirements: We need to filter out major countries according to the following rules

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5p2wMzEy,size_20,color_FFFFFF,t_70,g_se,x_16

  •  Problem-solving ideas: It is very simple, and it can be achieved by directly querying a where condition to filter large countries.
select 
    name, population, area
from 
    world
where 
    area >= 3000000 or population >= 25000000;
-- where 条件筛选大国

Topic 4: Consecutive Numbers

180. Consecutive Numbers

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5p2wMzEy,size_17,color_FFFFFF,t_70,g_se,x_16

  • watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5p2wMzEy,size_13,color_FFFFFF,t_70,g_se,x_16
  • Item requirements: 1. Occurs continuously 2. Occurs three times
  • Because we need to find from the same table, there is only one table, and the relationship is the repeated num records that appear three times in a row in the table, so it is easy to think that we can query through self-join.    Then there will definitely be repetitions in the query process, because it is equivalent to the same The table is connected to itself twice.        Therefore, a distinct needs to be performed
select distinct
    l1.num as 'ConsecutiveNums'    -- 'consecutivenums'题目要求别名
from 
    logs l1 inner join logs l2 on l1.num = l2.num
    inner join logs l3 on l1.num = l3.num
    # 子连接关系条件满足同一个num
where 
    l1.id = l2.id + 1 && l2.id = l3.id + 1; 
    -- where条件保证连续记录

Topic summary

  • To find the number of repeated records, we need to think of aggregation query, because aggregation query can compress the same keyword aggregation operation.
  • To find the largest or smallest record, we need to think that we can use sort + limit paging
  • The multi-table join operation is not limited to the number of tables, and even three tables and four tables can be connected at the same time, but it is generally not recommended to write in this way after the number is too large, and you need to find another way, because you can't write it down  

Guess you like

Origin blog.csdn.net/weixin_53695360/article/details/124062896